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
09595fed
Commit
09595fed
authored
Jun 22, 2018
by
GitLab Bot
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'upstream/master' into ce-to-ee-2018-06-22
parents
4e7d3451
19300e7e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
70 deletions
+43
-70
doc/development/utilities.md
doc/development/utilities.md
+41
-0
lib/gitlab/cache/request_cache.rb
lib/gitlab/cache/request_cache.rb
+1
-36
lib/gitlab/git/repository.rb
lib/gitlab/git/repository.rb
+1
-34
No files found.
doc/development/utilities.md
View file @
09595fed
...
...
@@ -135,3 +135,44 @@ We developed a number of utilities to ease development.
Find.new.clear_memoization(:result)
```
## [`RequestCache`](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/lib/gitlab/cache/request_cache.rb)
This module provides a simple way to cache values in RequestStore,
and the cache key would be based on the class name, method name,
optionally customized instance level values, optionally customized
method level values, and optional method arguments.
A simple example that only uses the instance level customised values:
```
ruby
class
UserAccess
extend
Gitlab
::
Cache
::
RequestCache
request_cache_key
do
[
user
&
.
id
,
project
&
.
id
]
end
request_cache
def
can_push_to_branch?
(
ref
)
# ...
end
end
```
This way, the result of
`can_push_to_branch?`
would be cached in
`RequestStore.store`
based on the cache key. If
`RequestStore`
is not
currently active, then it would be stored in a hash saved in an
instance variable, so the cache logic would be the same.
We can also set different strategies for different methods:
```
ruby
class
Commit
extend
Gitlab
::
Cache
::
RequestCache
def
author
User
.
find_by_any_email
(
author_email
.
downcase
)
end
request_cache
(
:author
)
{
author_email
.
downcase
}
end
```
lib/gitlab/cache/request_cache.rb
View file @
09595fed
module
Gitlab
module
Cache
# This module provides a simple way to cache values in RequestStore,
# and the cache key would be based on the class name, method name,
# optionally customized instance level values, optionally customized
# method level values, and optional method arguments.
#
# A simple example:
#
# class UserAccess
# extend Gitlab::Cache::RequestCache
#
# request_cache_key do
# [user&.id, project&.id]
# end
#
# request_cache def can_push_to_branch?(ref)
# # ...
# end
# end
#
# This way, the result of `can_push_to_branch?` would be cached in
# `RequestStore.store` based on the cache key. If RequestStore is not
# currently active, then it would be stored in a hash saved in an
# instance variable, so the cache logic would be the same.
# Here's another example using customized method level values:
#
# class Commit
# extend Gitlab::Cache::RequestCache
#
# def author
# User.find_by_any_email(author_email.downcase)
# end
# request_cache(:author) { author_email.downcase }
# end
#
# So that we could have different strategies for different methods
#
# See https://docs.gitlab.com/ee/development/utilities.html#requestcache
module
RequestCache
def
self
.
extended
(
klass
)
return
if
klass
<
self
...
...
lib/gitlab/git/repository.rb
View file @
09595fed
...
...
@@ -946,13 +946,7 @@ module Gitlab
#
# Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/327
def
ls_files
(
ref
)
gitaly_migrate
(
:ls_files
)
do
|
is_enabled
|
if
is_enabled
gitaly_ls_files
(
ref
)
else
git_ls_files
(
ref
)
end
end
gitaly_commit_client
.
ls_files
(
ref
)
end
# Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/328
...
...
@@ -1836,33 +1830,6 @@ module Gitlab
gitaly_repository_client
.
repository_size
end
def
gitaly_ls_files
(
ref
)
gitaly_commit_client
.
ls_files
(
ref
)
end
def
git_ls_files
(
ref
)
actual_ref
=
ref
||
root_ref
begin
sha_from_ref
(
actual_ref
)
rescue
Rugged
::
OdbError
,
Rugged
::
InvalidError
,
Rugged
::
ReferenceError
# Return an empty array if the ref wasn't found
return
[]
end
cmd
=
%W(ls-tree -r --full-tree --full-name --
#{
actual_ref
}
)
raw_output
,
_status
=
run_git
(
cmd
)
lines
=
raw_output
.
split
(
"
\n
"
).
map
do
|
f
|
stuff
,
path
=
f
.
split
(
"
\t
"
)
_mode
,
type
,
_sha
=
stuff
.
split
(
" "
)
path
if
type
==
"blob"
# Contain only blob type
end
lines
.
compact
end
# Returns true if the given ref name exists
#
# Ref names must start with `refs/`.
...
...
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