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
0c00fa10
Commit
0c00fa10
authored
Nov 11, 2019
by
Kerri Miller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cache redis loading and streamline method calls
parent
dc6522b8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
24 deletions
+39
-24
lib/gitlab/diff/highlight_cache.rb
lib/gitlab/diff/highlight_cache.rb
+24
-17
spec/lib/gitlab/diff/highlight_cache_spec.rb
spec/lib/gitlab/diff/highlight_cache_spec.rb
+15
-7
No files found.
lib/gitlab/diff/highlight_cache.rb
View file @
0c00fa10
...
@@ -11,6 +11,7 @@ module Gitlab
...
@@ -11,6 +11,7 @@ module Gitlab
def
initialize
(
diff_collection
,
backend:
Rails
.
cache
)
def
initialize
(
diff_collection
,
backend:
Rails
.
cache
)
@backend
=
backend
@backend
=
backend
@diff_collection
=
diff_collection
@diff_collection
=
diff_collection
@redis_key
=
diffable
.
cache_key
if
Feature
.
enabled?
(
:redis_diff_caching
)
end
end
# - Reads from cache
# - Reads from cache
...
@@ -58,32 +59,30 @@ module Gitlab
...
@@ -58,32 +59,30 @@ module Gitlab
# ...it will write/update a Redis hash (HSET)
# ...it will write/update a Redis hash (HSET)
#
#
def
write_to_redis_hash
(
hash
)
def
write_to_redis_hash
(
hash
)
key
=
diffable
.
cache_key
return
unless
@redis_key
if
key
Redis
::
Cache
.
with
do
|
redis
|
Redis
::
Cache
.
with
do
|
redis
|
redis
.
multi
do
|
multi
|
redis
.
multi
do
|
multi
|
hash
.
each
do
|
diff_file_id
,
highlighted_diff_lines_hash
|
hash
.
each
do
|
diff_file_id
,
highlighted_diff_lines_hash
|
multi
.
hset
(
@redis_key
,
diff_file_id
,
highlighted_diff_lines_hash
.
to_json
)
multi
.
hset
(
key
,
diff_file_id
,
highlighted_diff_lines_hash
.
to_json
)
# HSETs have to have their expiration date manually updated
# HSETs have to have their expiration date manually updated
#
#
multi
.
expire
(
@redis_key
,
EXPIRATION
)
multi
.
expire
(
key
,
EXPIRATION
)
end
end
end
end
end
end
end
end
end
def
read_entire_redis_hash
(
key
)
def
read_entire_redis_hash
Redis
::
Cache
.
with
do
|
redis
|
Redis
::
Cache
.
with
do
|
redis
|
redis
.
hgetall
(
key
)
redis
.
hgetall
(
@redis_
key
)
end
end
end
end
def
read_single_entry_from_redis_hash
(
key
,
diff_file_id
)
def
read_single_entry_from_redis_hash
(
diff_file_id
)
Redis
::
Cache
.
with
do
|
redis
|
Redis
::
Cache
.
with
do
|
redis
|
redis
.
hget
(
key
,
diff_file_id
)
redis
.
hget
(
@redis_
key
,
diff_file_id
)
end
end
end
end
...
@@ -106,7 +105,15 @@ module Gitlab
...
@@ -106,7 +105,15 @@ module Gitlab
end
end
def
cached_content
def
cached_content
@cached_content
||=
cache
.
read
(
key
)
||
{}
@cached_content
||=
populate_cached_content
||
{}
end
def
populate_cached_content
if
Feature
.
enabled?
(
:redis_diff_caching
)
read_entire_redis_hash
else
cache
.
read
(
key
)
end
end
end
def
cacheable?
(
diff_file
)
def
cacheable?
(
diff_file
)
...
...
spec/lib/gitlab/diff/highlight_cache_spec.rb
View file @
0c00fa10
...
@@ -53,6 +53,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
...
@@ -53,6 +53,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
fallback_diff_refs:
diffs
.
fallback_diff_refs
)
fallback_diff_refs:
diffs
.
fallback_diff_refs
)
end
end
it
'does not calculate highlighting when reading from cache'
do
it
'does not calculate highlighting when reading from cache'
do
cache
.
write_if_empty
cache
.
write_if_empty
cache
.
decorate
(
diff_file
)
cache
.
decorate
(
diff_file
)
...
@@ -69,11 +70,18 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
...
@@ -69,11 +70,18 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
expect
(
diff_file
.
highlighted_diff_lines
.
size
).
to
be
>
5
expect
(
diff_file
.
highlighted_diff_lines
.
size
).
to
be
>
5
end
end
it
'submits a single reading from the cache'
do
context
'when :redis_diff_caching is not enabled'
do
cache
.
decorate
(
diff_file
)
before
do
cache
.
decorate
(
diff_file
)
expect
(
Feature
).
to
receive
(
:enabled?
).
with
(
:redis_diff_caching
).
and_return
(
false
)
end
it
'submits a single reading from the cache'
do
expect
(
Feature
).
to
receive
(
:enabled?
).
with
(
:redis_diff_caching
).
at_least
(
:once
).
and_return
(
false
)
2
.
times
{
cache
.
decorate
(
diff_file
)
}
expect
(
backend
).
to
have_received
(
:read
).
with
(
cache
.
key
).
once
expect
(
backend
).
to
have_received
(
:read
).
with
(
cache
.
key
).
once
end
end
end
end
end
...
@@ -94,7 +102,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
...
@@ -94,7 +102,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
context
'when :redis_diff_caching is not enabled'
do
context
'when :redis_diff_caching is not enabled'
do
before
do
before
do
expect
(
Feature
).
to
receive
(
:enabled?
).
with
(
:redis_diff_caching
).
and_return
(
false
)
expect
(
Feature
).
to
receive
(
:enabled?
).
with
(
:redis_diff_caching
).
a
t_least
(
:once
).
a
nd_return
(
false
)
end
end
it
'submits a single writing to the cache'
do
it
'submits a single writing to the cache'
do
...
@@ -126,7 +134,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
...
@@ -126,7 +134,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
end
end
it
'returns the entire contents of a Redis hash as JSON'
do
it
'returns the entire contents of a Redis hash as JSON'
do
result
=
cache
.
read_entire_redis_hash
(
cache_key
)
result
=
cache
.
read_entire_redis_hash
expect
(
result
.
values
.
first
).
to
eq
(
diff_hash
.
values
.
first
.
to_json
)
expect
(
result
.
values
.
first
).
to
eq
(
diff_hash
.
values
.
first
.
to_json
)
end
end
...
@@ -142,7 +150,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
...
@@ -142,7 +150,7 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do
it
'returns highlighted diff content for a single file as JSON'
do
it
'returns highlighted diff content for a single file as JSON'
do
diff_hash
.
each
do
|
file_path
,
value
|
diff_hash
.
each
do
|
file_path
,
value
|
found
=
cache
.
read_single_entry_from_redis_hash
(
cache_key
,
file_path
)
found
=
cache
.
read_single_entry_from_redis_hash
(
file_path
)
expect
(
found
).
to
eq
(
value
.
to_json
)
expect
(
found
).
to
eq
(
value
.
to_json
)
end
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