Commit 41bc9c46 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Refactor caching code

parent a215e2ee
...@@ -7,8 +7,9 @@ class Projects::AvatarsController < Projects::ApplicationController ...@@ -7,8 +7,9 @@ class Projects::AvatarsController < Projects::ApplicationController
@blob = @repository.blob_at_branch('master', @project.avatar_in_git) @blob = @repository.blob_at_branch('master', @project.avatar_in_git)
if @blob if @blob
headers['X-Content-Type-Options'] = 'nosniff' headers['X-Content-Type-Options'] = 'nosniff'
set_cache_headers
check_etag! return if cached_blob?
headers.store(*Gitlab::Workhorse.send_git_blob(@repository, @blob)) headers.store(*Gitlab::Workhorse.send_git_blob(@repository, @blob))
headers['Content-Disposition'] = 'inline' headers['Content-Disposition'] = 'inline'
headers['Content-Type'] = safe_content_type(@blob) headers['Content-Type'] = safe_content_type(@blob)
......
...@@ -12,8 +12,8 @@ class Projects::RawController < Projects::ApplicationController ...@@ -12,8 +12,8 @@ class Projects::RawController < Projects::ApplicationController
if @blob if @blob
headers['X-Content-Type-Options'] = 'nosniff' headers['X-Content-Type-Options'] = 'nosniff'
check_etag!
set_cache_headers return if cached_blob?
if @blob.lfs_pointer? if @blob.lfs_pointer?
send_lfs_object send_lfs_object
......
...@@ -153,7 +153,10 @@ module BlobHelper ...@@ -153,7 +153,10 @@ module BlobHelper
end end
end end
def set_cache_headers def cached_blob?
stale = stale?(etag: @blob.id) # The #stale? method sets cache headers.
# Because we are opionated we set the cache headers ourselves.
if @project.visibility_level == Project::PUBLIC if @project.visibility_level == Project::PUBLIC
cache_control = 'public, ' cache_control = 'public, '
else else
...@@ -162,19 +165,19 @@ module BlobHelper ...@@ -162,19 +165,19 @@ module BlobHelper
if @ref && @commit && @ref == @commit.id if @ref && @commit && @ref == @commit.id
# This is a link to a commit by its commit SHA. That means that the blob # This is a link to a commit by its commit SHA. That means that the blob
# is immutable. # is immutable. The only reason to invalidate the cache is if the commit
cache_control << 'max-age=600' # 10 minutes # was deleted or if the user lost access to the repository.
max_age = Blob::CACHE_TIME_IMMUTABLE
else else
# A branch or tag points at this blob. That means that the expected blob # A branch or tag points at this blob. That means that the expected blob
# value may change over time. # value may change over time.
cache_control << 'max-age=60' # 1 minute max_age = Blob::CACHE_TIME
end end
cache_control << "max-age=#{max_age}"
headers['Cache-Control'] = cache_control headers['Cache-Control'] = cache_control
headers['ETag'] = @blob.id headers['ETag'] = @blob.id
end
def check_etag! !stale
stale?(etag: @blob.id)
end end
end end
# Blob is a Rails-specific wrapper around Gitlab::Git::Blob objects # Blob is a Rails-specific wrapper around Gitlab::Git::Blob objects
class Blob < SimpleDelegator class Blob < SimpleDelegator
CACHE_TIME = 60 # Cache raw blobs referred to by a (mutable) ref for 1 minute
CACHE_TIME_IMMUTABLE = 3600 # Cache blobs referred to by an immutable reference for 1 hour
# Wrap a Gitlab::Git::Blob object, or return nil when given nil # Wrap a Gitlab::Git::Blob object, or return nil when given nil
# #
# This method prevents the decorated object from evaluating to "truthy" when # This method prevents the decorated object from evaluating to "truthy" when
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment