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
d0b76d06
Commit
d0b76d06
authored
Jul 03, 2019
by
Rémy Coutable
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cache PerformanceBar.allowed_user_ids list locally and in Redis
Signed-off-by:
Rémy Coutable
<
remy@rymai.me
>
parent
ea2f2ab0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
9 deletions
+46
-9
lib/gitlab/performance_bar.rb
lib/gitlab/performance_bar.rb
+20
-8
spec/lib/gitlab/performance_bar_spec.rb
spec/lib/gitlab/performance_bar_spec.rb
+26
-1
No files found.
lib/gitlab/performance_bar.rb
View file @
d0b76d06
...
@@ -3,7 +3,8 @@
...
@@ -3,7 +3,8 @@
module
Gitlab
module
Gitlab
module
PerformanceBar
module
PerformanceBar
ALLOWED_USER_IDS_KEY
=
'performance_bar_allowed_user_ids:v2'
.
freeze
ALLOWED_USER_IDS_KEY
=
'performance_bar_allowed_user_ids:v2'
.
freeze
EXPIRY_TIME
=
5
.
minutes
EXPIRY_TIME_L1_CACHE
=
1
.
minute
EXPIRY_TIME_L2_CACHE
=
5
.
minutes
def
self
.
enabled?
(
user
=
nil
)
def
self
.
enabled?
(
user
=
nil
)
return
true
if
Rails
.
env
.
development?
return
true
if
Rails
.
env
.
development?
...
@@ -19,20 +20,31 @@ module Gitlab
...
@@ -19,20 +20,31 @@ module Gitlab
# rubocop: disable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def
self
.
allowed_user_ids
def
self
.
allowed_user_ids
Rails
.
cache
.
fetch
(
ALLOWED_USER_IDS_KEY
,
expires_in:
EXPIRY_TIME
)
do
l1_cache_backend
.
fetch
(
ALLOWED_USER_IDS_KEY
,
expires_in:
EXPIRY_TIME_L1_CACHE
)
do
group
=
Group
.
find_by_id
(
allowed_group_id
)
l2_cache_backend
.
fetch
(
ALLOWED_USER_IDS_KEY
,
expires_in:
EXPIRY_TIME_L2_CACHE
)
do
group
=
Group
.
find_by_id
(
allowed_group_id
)
if
group
if
group
GroupMembersFinder
.
new
(
group
).
execute
.
pluck
(
:user_id
)
GroupMembersFinder
.
new
(
group
).
execute
.
pluck
(
:user_id
)
else
else
[]
[]
end
end
end
end
end
end
end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: enable CodeReuse/ActiveRecord
def
self
.
expire_allowed_user_ids_cache
def
self
.
expire_allowed_user_ids_cache
Rails
.
cache
.
delete
(
ALLOWED_USER_IDS_KEY
)
l1_cache_backend
.
delete
(
ALLOWED_USER_IDS_KEY
)
l2_cache_backend
.
delete
(
ALLOWED_USER_IDS_KEY
)
end
def
self
.
l1_cache_backend
Gitlab
::
ThreadMemoryCache
.
cache_backend
end
def
self
.
l2_cache_backend
Rails
.
cache
end
end
end
end
end
end
spec/lib/gitlab/performance_bar_spec.rb
View file @
d0b76d06
...
@@ -3,17 +3,42 @@ require 'spec_helper'
...
@@ -3,17 +3,42 @@ require 'spec_helper'
describe
Gitlab
::
PerformanceBar
do
describe
Gitlab
::
PerformanceBar
do
shared_examples
'allowed user IDs are cached'
do
shared_examples
'allowed user IDs are cached'
do
before
do
before
do
# Warm the
Redis cache
# Warm the
caches
described_class
.
enabled?
(
user
)
described_class
.
enabled?
(
user
)
end
end
it
'caches the allowed user IDs in cache'
,
:use_clean_rails_memory_store_caching
do
it
'caches the allowed user IDs in cache'
,
:use_clean_rails_memory_store_caching
do
expect
do
expect
do
expect
(
described_class
.
l1_cache_backend
).
to
receive
(
:fetch
).
and_call_original
expect
(
described_class
.
l2_cache_backend
).
not_to
receive
(
:fetch
)
expect
(
described_class
.
enabled?
(
user
)).
to
be_truthy
expect
(
described_class
.
enabled?
(
user
)).
to
be_truthy
end
.
not_to
exceed_query_limit
(
0
)
end
.
not_to
exceed_query_limit
(
0
)
end
end
it
'caches the allowed user IDs in L1 cache for 1 minute'
,
:use_clean_rails_memory_store_caching
do
Timecop
.
travel
2
.
minutes
do
expect
do
expect
(
described_class
.
l1_cache_backend
).
to
receive
(
:fetch
).
and_call_original
expect
(
described_class
.
l2_cache_backend
).
to
receive
(
:fetch
).
and_call_original
expect
(
described_class
.
enabled?
(
user
)).
to
be_truthy
end
.
not_to
exceed_query_limit
(
0
)
end
end
it
'caches the allowed user IDs in L2 cache for 5 minutes'
,
:use_clean_rails_memory_store_caching
do
Timecop
.
travel
6
.
minutes
do
expect
do
expect
(
described_class
.
l1_cache_backend
).
to
receive
(
:fetch
).
and_call_original
expect
(
described_class
.
l2_cache_backend
).
to
receive
(
:fetch
).
and_call_original
expect
(
described_class
.
enabled?
(
user
)).
to
be_truthy
end
.
not_to
exceed_query_limit
(
2
)
end
end
end
end
it
{
expect
(
described_class
.
l1_cache_backend
).
to
eq
(
Gitlab
::
ThreadMemoryCache
.
cache_backend
)
}
it
{
expect
(
described_class
.
l2_cache_backend
).
to
eq
(
Rails
.
cache
)
}
describe
'.enabled?'
do
describe
'.enabled?'
do
let
(
:user
)
{
create
(
:user
)
}
let
(
:user
)
{
create
(
:user
)
}
...
...
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