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
febd38fc
Commit
febd38fc
authored
May 06, 2021
by
Robert May
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add caching to branches API
Adds caching around entity generation for branches on the API list endpoint.
parent
b04fba39
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
43 additions
and
9 deletions
+43
-9
config/feature_flags/development/api_caching_branches.yml
config/feature_flags/development/api_caching_branches.yml
+8
-0
lib/api/branches.rb
lib/api/branches.rb
+19
-7
lib/gitlab/git/branch.rb
lib/gitlab/git/branch.rb
+4
-0
spec/lib/gitlab/git/branch_spec.rb
spec/lib/gitlab/git/branch_spec.rb
+10
-0
spec/requests/api/branches_spec.rb
spec/requests/api/branches_spec.rb
+2
-2
No files found.
config/feature_flags/development/api_caching_branches.yml
0 → 100644
View file @
febd38fc
---
name
:
api_caching_branches
introduced_by_url
:
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/61157
rollout_issue_url
:
https://gitlab.com/gitlab-org/gitlab/-/issues/330371
milestone
:
'
13.12'
type
:
development
group
:
group::source code
default_enabled
:
false
lib/api/branches.rb
View file @
febd38fc
...
...
@@ -47,13 +47,25 @@ module API
merged_branch_names
=
repository
.
merged_branch_names
(
branches
.
map
(
&
:name
))
present
(
branches
,
with:
Entities
::
Branch
,
current_user:
current_user
,
project:
user_project
,
merged_branch_names:
merged_branch_names
)
if
Feature
.
enabled?
(
:api_caching_branches
,
user_project
,
type: :development
,
default_enabled: :yaml
)
present_cached
(
branches
,
with:
Entities
::
Branch
,
current_user:
current_user
,
project:
user_project
,
merged_branch_names:
merged_branch_names
,
expires_in:
10
.
minutes
,
cache_context:
->
(
branch
)
{
[
current_user
&
.
cache_key
,
merged_branch_names
.
include?
(
branch
.
name
)]
}
)
else
present
(
branches
,
with:
Entities
::
Branch
,
current_user:
current_user
,
project:
user_project
,
merged_branch_names:
merged_branch_names
)
end
end
resource
':id/repository/branches/:branch'
,
requirements:
BRANCH_ENDPOINT_REQUIREMENTS
do
...
...
lib/gitlab/git/branch.rb
View file @
febd38fc
...
...
@@ -28,6 +28,10 @@ module Gitlab
def
state
active?
?
:active
:
:stale
end
def
cache_key
"branch:"
+
Digest
::
SHA1
.
hexdigest
([
name
,
target
,
dereferenced_target
&
.
sha
].
join
(
':'
))
end
end
end
end
spec/lib/gitlab/git/branch_spec.rb
View file @
febd38fc
...
...
@@ -44,6 +44,16 @@ RSpec.describe Gitlab::Git::Branch, :seed_helper do
end
end
describe
"#cache_key"
do
subject
{
repository
.
branches
.
first
}
it
"returns a cache key that changes based on changeable values"
do
digest
=
Digest
::
SHA1
.
hexdigest
([
subject
.
name
,
subject
.
target
,
subject
.
dereferenced_target
.
sha
].
join
(
":"
))
expect
(
subject
.
cache_key
).
to
eq
(
"branch:
#{
digest
}
"
)
end
end
describe
'#size'
do
subject
{
super
().
size
}
...
...
spec/requests/api/branches_spec.rb
View file @
febd38fc
...
...
@@ -53,7 +53,7 @@ RSpec.describe API::Branches do
end
it
'determines only a limited number of merged branch names'
do
expect
(
API
::
Entities
::
Branch
).
to
receive
(
:represent
).
with
(
anything
,
has_up_to_merged_branch_names_count
(
2
)).
and_call_original
expect
(
API
::
Entities
::
Branch
).
to
receive
(
:represent
).
with
(
anything
,
has_up_to_merged_branch_names_count
(
2
)).
a
t_least
(
:once
).
a
nd_call_original
get
api
(
route
,
current_user
),
params:
base_params
.
merge
(
per_page:
2
)
...
...
@@ -111,7 +111,7 @@ RSpec.describe API::Branches do
end
it
'determines only a limited number of merged branch names'
do
expect
(
API
::
Entities
::
Branch
).
to
receive
(
:represent
).
with
(
anything
,
has_up_to_merged_branch_names_count
(
2
)).
and_call_original
expect
(
API
::
Entities
::
Branch
).
to
receive
(
:represent
).
with
(
anything
,
has_up_to_merged_branch_names_count
(
2
)).
a
t_least
(
:once
).
a
nd_call_original
get
api
(
route
,
current_user
),
params:
base_params
.
merge
(
per_page:
2
)
...
...
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