Class: Gitlab::SetCache
- Inherits:
-
Object
show all
- Defined in:
- lib/gitlab/set_cache.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(expires_in: 2.weeks) ⇒ SetCache
Returns a new instance of SetCache.
9
10
11
|
# File 'lib/gitlab/set_cache.rb', line 9
def initialize(expires_in: 2.weeks)
@expires_in = expires_in
end
|
Instance Attribute Details
#expires_in ⇒ Object
Returns the value of attribute expires_in.
7
8
9
|
# File 'lib/gitlab/set_cache.rb', line 7
def expires_in
@expires_in
end
|
Instance Method Details
#cache_key(key) ⇒ Object
13
14
15
|
# File 'lib/gitlab/set_cache.rb', line 13
def cache_key(key)
"#{cache_namespace}:#{key}:set"
end
|
#count(key) ⇒ Object
75
76
77
|
# File 'lib/gitlab/set_cache.rb', line 75
def count(key)
with { |redis| redis.scard(cache_key(key)) }
end
|
#exist?(key) ⇒ Boolean
34
35
36
|
# File 'lib/gitlab/set_cache.rb', line 34
def exist?(key)
with { |redis| redis.exists?(cache_key(key)) } end
|
#expire(*keys) ⇒ Object
Returns the number of keys deleted by Redis
#include?(key, value) ⇒ Boolean
54
55
56
|
# File 'lib/gitlab/set_cache.rb', line 54
def include?(key, value)
with { |redis| redis.sismember(cache_key(key), value) }
end
|
#read(key) ⇒ Object
50
51
52
|
# File 'lib/gitlab/set_cache.rb', line 50
def read(key)
with { |redis| redis.smembers(cache_key(key)) }
end
|
#try_include?(key, value) ⇒ Boolean
Like include?, but also tells us if the cache was populated when it ran by returning two booleans: [member_exists, set_exists]
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/gitlab/set_cache.rb', line 60
def try_include?(key, value)
full_key = cache_key(key)
with do |redis|
redis.multi do |multi|
multi.sismember(full_key, value)
multi.exists?(full_key) end
end
end
|
#ttl(key) ⇒ Object
71
72
73
|
# File 'lib/gitlab/set_cache.rb', line 71
def ttl(key)
with { |redis| redis.ttl(cache_key(key)) }
end
|
#write(key, value) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/gitlab/set_cache.rb', line 38
def write(key, value)
with do |redis|
redis.pipelined do |pipeline|
pipeline.sadd?(cache_key(key), value)
pipeline.expire(cache_key(key), expires_in)
end
end
value
end
|