From fc90d9e5896cdcccedb697fd4536f126d10f3f8e Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Thu, 3 Mar 2016 17:59:47 +0100 Subject: Tell clients/proxies to cache raw blob requests --- app/helpers/blob_helper.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'app/helpers/blob_helper.rb') diff --git a/app/helpers/blob_helper.rb b/app/helpers/blob_helper.rb index 7f63a2e2cb4..adb56e49c62 100644 --- a/app/helpers/blob_helper.rb +++ b/app/helpers/blob_helper.rb @@ -152,4 +152,29 @@ module BlobHelper 'application/octet-stream' end end + + def set_cache_headers + if @project.visibility_level == Project::PUBLIC + cache_control = 'public, ' + else + cache_control = 'private, ' + end + + if @ref && @commit && @ref == @commit.id + # This is a link to a commit by its commit SHA. That means that the blob + # is immutable. + cache_control << 'max-age=600' # 10 minutes + else + # A branch or tag points at this blob. That means that the expected blob + # value may change over time. + cache_control << 'max-age=60' # 1 minute + end + + headers['Cache-Control'] = cache_control + headers['ETag'] = @blob.id + end + + def check_etag! + stale?(etag: @blob.id) + end end -- cgit v1.2.1