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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
3edf027a
Commit
3edf027a
authored
May 13, 2021
by
Robert May
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add race_condition_ttl support
Prevents multiple writes under heavy load.
parent
09d74fc0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
2 deletions
+13
-2
lib/api/helpers/caching.rb
lib/api/helpers/caching.rb
+10
-1
spec/lib/api/helpers/caching_spec.rb
spec/lib/api/helpers/caching_spec.rb
+3
-1
No files found.
lib/api/helpers/caching.rb
View file @
3edf027a
...
...
@@ -11,6 +11,11 @@ module API
# @return [ActiveSupport::Duration]
DEFAULT_EXPIRY
=
1
.
day
# @return [Hash]
DEFAULT_CACHE_OPTIONS
=
{
race_condition_ttl:
5
.
seconds
}.
freeze
# @return [ActiveSupport::Cache::Store]
def
cache
Rails
.
cache
...
...
@@ -75,7 +80,7 @@ module API
# @param expires_in [ActiveSupport::Duration, Integer] an expiry time for the cache entry
# @return [Gitlab::Json::PrecompiledJson]
def
cache_action
(
key
,
**
cache_opts
)
json
=
cache
.
fetch
(
key
,
**
cache_opts
)
do
json
=
cache
.
fetch
(
key
,
**
apply_default_cache_options
(
cache_opts
)
)
do
Gitlab
::
Json
.
dump
(
yield
.
as_json
)
end
...
...
@@ -106,6 +111,10 @@ module API
private
def
apply_default_cache_options
(
opts
=
{})
DEFAULT_CACHE_OPTIONS
.
merge
(
opts
)
end
# Optionally uses a `Proc` to add context to a cache key
#
# @param object [Object] must respond to #cache_key
...
...
spec/lib/api/helpers/caching_spec.rb
View file @
3edf027a
...
...
@@ -170,8 +170,10 @@ RSpec.describe API::Helpers::Caching, :use_clean_rails_redis_caching do
end
it
"only calls the expensive action once"
do
expected_kwargs
=
described_class
::
DEFAULT_CACHE_OPTIONS
.
merge
(
kwargs
)
expect
(
expensive_thing
).
to
receive
(
:do_very_expensive_action
).
once
expect
(
instance
.
cache
).
to
receive
(
:fetch
).
with
(
cache_key
,
**
kwargs
).
exactly
(
5
).
times
.
and_call_original
expect
(
instance
.
cache
).
to
receive
(
:fetch
).
with
(
cache_key
,
**
expected_
kwargs
).
exactly
(
5
).
times
.
and_call_original
5
.
times
{
perform
}
end
...
...
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