Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
iv
gitlab-ce
Commits
41bc9c46
Commit
41bc9c46
authored
Mar 07, 2016
by
Jacob Vosmaer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor caching code
parent
a215e2ee
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
18 additions
and
11 deletions
+18
-11
app/controllers/projects/avatars_controller.rb
app/controllers/projects/avatars_controller.rb
+3
-2
app/controllers/projects/raw_controller.rb
app/controllers/projects/raw_controller.rb
+2
-2
app/helpers/blob_helper.rb
app/helpers/blob_helper.rb
+10
-7
app/models/blob.rb
app/models/blob.rb
+3
-0
No files found.
app/controllers/projects/avatars_controller.rb
View file @
41bc9c46
...
@@ -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
)
...
...
app/controllers/projects/raw_controller.rb
View file @
41bc9c46
...
@@ -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
...
...
app/helpers/blob_helper.rb
View file @
41bc9c46
...
@@ -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
app/models/blob.rb
View file @
41bc9c46
# 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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment