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
82e0b9c3
Commit
82e0b9c3
authored
Feb 24, 2020
by
Sean Arnold
Committed by
Bob Van Landuyt
Feb 24, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add specs for reactive cache set cache spec
Rename file too
parent
d2166488
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
175 additions
and
23 deletions
+175
-23
lib/gitlab/reactive_cache_set_cache.rb
lib/gitlab/reactive_cache_set_cache.rb
+30
-0
lib/gitlab/repository_set_cache.rb
lib/gitlab/repository_set_cache.rb
+1
-23
lib/gitlab/set_cache.rb
lib/gitlab/set_cache.rb
+55
-0
spec/lib/gitlab/reactive_cache_set_cache_spec.rb
spec/lib/gitlab/reactive_cache_set_cache_spec.rb
+74
-0
spec/support/caching.rb
spec/support/caching.rb
+15
-0
No files found.
lib/gitlab/reactive_cache_set_cache.rb
0 → 100644
View file @
82e0b9c3
# frozen_string_literal: true
# Interface to the Redis-backed cache store to keep track of complete cache keys
# for a ReactiveCache resource.
module
Gitlab
class
ReactiveCacheSetCache
<
Gitlab
::
SetCache
attr_reader
:expires_in
def
initialize
(
expires_in:
10
.
minutes
)
@expires_in
=
expires_in
end
def
clear_cache!
(
key
)
with
do
|
redis
|
keys
=
read
(
key
).
map
{
|
value
|
"
#{
cache_type
}#{
value
}
"
}
keys
<<
cache_key
(
key
)
redis
.
pipelined
do
keys
.
each_slice
(
1000
)
{
|
subset
|
redis
.
del
(
*
subset
)
}
end
end
end
private
def
cache_type
"
#{
Gitlab
::
Redis
::
Cache
::
CACHE_NAMESPACE
}
:"
end
end
end
lib/gitlab/repository_set_cache.rb
View file @
82e0b9c3
...
...
@@ -2,7 +2,7 @@
# Interface to the Redis-backed cache store for keys that use a Redis set
module
Gitlab
class
RepositorySetCache
class
RepositorySetCache
<
Gitlab
::
SetCache
attr_reader
:repository
,
:namespace
,
:expires_in
def
initialize
(
repository
,
extra_namespace:
nil
,
expires_in:
2
.
weeks
)
...
...
@@ -17,18 +17,6 @@ module Gitlab
"
#{
type
}
:
#{
namespace
}
:set"
end
def
expire
(
key
)
with
{
|
redis
|
redis
.
del
(
cache_key
(
key
))
}
end
def
exist?
(
key
)
with
{
|
redis
|
redis
.
exists
(
cache_key
(
key
))
}
end
def
read
(
key
)
with
{
|
redis
|
redis
.
smembers
(
cache_key
(
key
))
}
end
def
write
(
key
,
value
)
full_key
=
cache_key
(
key
)
...
...
@@ -54,15 +42,5 @@ module Gitlab
write
(
key
,
yield
)
end
end
def
include?
(
key
,
value
)
with
{
|
redis
|
redis
.
sismember
(
cache_key
(
key
),
value
)
}
end
private
def
with
(
&
blk
)
Gitlab
::
Redis
::
Cache
.
with
(
&
blk
)
# rubocop:disable CodeReuse/ActiveRecord
end
end
end
lib/gitlab/set_cache.rb
0 → 100644
View file @
82e0b9c3
# frozen_string_literal: true
# Interface to the Redis-backed cache store to keep track of complete cache keys
# for a ReactiveCache resource.
module
Gitlab
class
SetCache
attr_reader
:expires_in
def
initialize
(
expires_in:
2
.
weeks
)
@expires_in
=
expires_in
end
def
cache_key
(
key
)
"
#{
key
}
:set"
end
def
expire
(
key
)
with
{
|
redis
|
redis
.
del
(
cache_key
(
key
))
}
end
def
exist?
(
key
)
with
{
|
redis
|
redis
.
exists
(
cache_key
(
key
))
}
end
def
write
(
key
,
value
)
with
do
|
redis
|
redis
.
pipelined
do
redis
.
sadd
(
cache_key
(
key
),
value
)
redis
.
expire
(
cache_key
(
key
),
expires_in
)
end
end
value
end
def
read
(
key
)
with
{
|
redis
|
redis
.
smembers
(
cache_key
(
key
))
}
end
def
include?
(
key
,
value
)
with
{
|
redis
|
redis
.
sismember
(
cache_key
(
key
),
value
)
}
end
def
ttl
(
key
)
with
{
|
redis
|
redis
.
ttl
(
cache_key
(
key
))
}
end
private
def
with
(
&
blk
)
Gitlab
::
Redis
::
Cache
.
with
(
&
blk
)
# rubocop:disable CodeReuse/ActiveRecord
end
end
end
spec/lib/gitlab/reactive_cache_set_cache_spec.rb
0 → 100644
View file @
82e0b9c3
# frozen_string_literal: true
require
'spec_helper'
describe
Gitlab
::
ReactiveCacheSetCache
,
:clean_gitlab_redis_cache
do
let_it_be
(
:project
)
{
create
(
:project
)
}
let
(
:cache_prefix
)
{
'cache_prefix'
}
let
(
:expires_in
)
{
10
.
minutes
}
let
(
:cache
)
{
described_class
.
new
(
expires_in:
expires_in
)
}
describe
'#cache_key'
do
subject
{
cache
.
cache_key
(
cache_prefix
)
}
it
'includes the suffix'
do
expect
(
subject
).
to
eq
"
#{
cache_prefix
}
:set"
end
end
describe
'#read'
do
subject
{
cache
.
read
(
cache_prefix
)
}
it
{
is_expected
.
to
be_empty
}
context
'after item added'
do
before
do
cache
.
write
(
cache_prefix
,
'test_item'
)
end
it
{
is_expected
.
to
contain_exactly
(
'test_item'
)
}
end
end
describe
'#write'
do
it
'writes the value to the cache'
do
cache
.
write
(
cache_prefix
,
'test_item'
)
expect
(
cache
.
read
(
cache_prefix
)).
to
contain_exactly
(
'test_item'
)
end
it
'sets the expiry of the set'
do
cache
.
write
(
cache_prefix
,
'test_item'
)
expect
(
cache
.
ttl
(
cache_prefix
)).
to
be_within
(
1
).
of
(
expires_in
.
seconds
)
end
end
describe
'#clear_cache!'
,
:use_clean_rails_redis_caching
do
it
'deletes the cached items'
do
# Cached key and value
Rails
.
cache
.
write
(
'test_item'
,
'test_value'
)
# Add key to set
cache
.
write
(
cache_prefix
,
'test_item'
)
expect
(
cache
.
read
(
cache_prefix
)).
to
contain_exactly
(
'test_item'
)
cache
.
clear_cache!
(
cache_prefix
)
expect
(
cache
.
read
(
cache_prefix
)).
to
be_empty
end
end
describe
'#include?'
do
subject
{
cache
.
include?
(
cache_prefix
,
'test_item'
)
}
it
{
is_expected
.
to
be
(
false
)
}
context
'item added'
do
before
do
cache
.
write
(
cache_prefix
,
'test_item'
)
end
it
{
is_expected
.
to
be
(
true
)
}
end
end
end
spec/support/caching.rb
View file @
82e0b9c3
...
...
@@ -21,6 +21,21 @@ RSpec.configure do |config|
ActionController
::
Base
.
cache_store
=
caching_store
end
config
.
around
(
:each
,
:use_clean_rails_redis_caching
)
do
|
example
|
original_null_store
=
Rails
.
cache
caching_config_hash
=
Gitlab
::
Redis
::
Cache
.
params
caching_config_hash
[
:namespace
]
=
Gitlab
::
Redis
::
Cache
::
CACHE_NAMESPACE
Rails
.
cache
=
ActiveSupport
::
Cache
::
RedisCacheStore
.
new
(
caching_config_hash
)
redis_cache_cleanup!
example
.
run
redis_cache_cleanup!
Rails
.
cache
=
original_null_store
end
config
.
around
(
:each
,
:use_sql_query_cache
)
do
|
example
|
ActiveRecord
::
Base
.
cache
do
example
.
run
...
...
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