Module: Gitlab::Cache::Import::Caching
- Defined in:
- lib/gitlab/cache/import/caching.rb
Constant Summary collapse
- TIMEOUT =
The default timeout of the cache keys.
24.hours.to_i
- WRITE_IF_GREATER_SCRIPT =
<<-EOF.strip_heredoc.freeze local key, value, ttl = KEYS[1], tonumber(ARGV[1]), ARGV[2] local existing = tonumber(redis.call("get", key)) if existing == nil or value > existing then redis.call("set", key, value) redis.call("expire", key, ttl) return true else return false end EOF
Class Method Summary collapse
- .cache_key_for(raw_key) ⇒ Object
-
.expire(raw_key, timeout) ⇒ Object
Sets the expiration time of a key.
-
.increment(raw_key, timeout: TIMEOUT) ⇒ Object
Increment the integer value of a key by one.
-
.read(raw_key, timeout: TIMEOUT) ⇒ Object
Reads a cache key.
-
.read_integer(raw_key, timeout: TIMEOUT) ⇒ Object
Reads an integer from the cache, or returns nil if no value was found.
-
.set_add(raw_key, value, timeout: TIMEOUT) ⇒ Object
Adds a value to a set.
-
.set_includes?(raw_key, value) ⇒ Boolean
Returns true if the given value is present in the set.
-
.write(raw_key, value, timeout: TIMEOUT) ⇒ Object
Sets a cache key to the given value.
-
.write_if_greater(raw_key, value, timeout: TIMEOUT) ⇒ Object
Sets a key to the given integer but only if the existing value is smaller than the given value.
-
.write_multiple(mapping, key_prefix: nil, timeout: TIMEOUT) ⇒ Object
Sets multiple keys to given values.
Class Method Details
.cache_key_for(raw_key) ⇒ Object
165 166 167 |
# File 'lib/gitlab/cache/import/caching.rb', line 165 def self.cache_key_for(raw_key) "#{Redis::Cache::CACHE_NAMESPACE}:#{raw_key}" end |
.expire(raw_key, timeout) ⇒ Object
Sets the expiration time of a key.
raw_key - The key for which to change the timeout. timeout - The new timeout.
137 138 139 140 141 142 143 |
# File 'lib/gitlab/cache/import/caching.rb', line 137 def self.expire(raw_key, timeout) key = cache_key_for(raw_key) Redis::Cache.with do |redis| redis.expire(key, timeout) end end |
.increment(raw_key, timeout: TIMEOUT) ⇒ Object
Increment the integer value of a key by one. Sets the value to zero if missing before incrementing
key - The cache key to increment. timeout - The time after which the cache key should expire.
79 80 81 82 83 84 85 86 |
# File 'lib/gitlab/cache/import/caching.rb', line 79 def self.increment(raw_key, timeout: TIMEOUT) key = cache_key_for(raw_key) Redis::Cache.with do |redis| redis.incr(key) redis.expire(key, timeout) end end |
.read(raw_key, timeout: TIMEOUT) ⇒ Object
Reads a cache key.
If the key exists and has a non-empty value its TTL is refreshed automatically.
raw_key - The cache key to read. timeout - The new timeout of the key if the key is to be refreshed.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/gitlab/cache/import/caching.rb', line 30 def self.read(raw_key, timeout: TIMEOUT) key = cache_key_for(raw_key) value = Redis::Cache.with { |redis| redis.get(key) } if value.present? # We refresh the expiration time so frequently used keys stick # around, removing the need for querying the database as much as # possible. # # A key may be empty when we looked up a GitHub user (for example) but # did not find a matching GitLab user. In that case we _don't_ want to # refresh the TTL so we automatically pick up the right data when said # user were to register themselves on the GitLab instance. Redis::Cache.with { |redis| redis.expire(key, timeout) } end value end |
.read_integer(raw_key, timeout: TIMEOUT) ⇒ Object
Reads an integer from the cache, or returns nil if no value was found.
See Caching.read for more information.
52 53 54 55 56 |
# File 'lib/gitlab/cache/import/caching.rb', line 52 def self.read_integer(raw_key, timeout: TIMEOUT) value = read(raw_key, timeout: timeout) value.to_i if value.present? end |
.set_add(raw_key, value, timeout: TIMEOUT) ⇒ Object
Adds a value to a set.
raw_key - The key of the set to add the value to. value - The value to add to the set. timeout - The new timeout of the key.
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/gitlab/cache/import/caching.rb', line 93 def self.set_add(raw_key, value, timeout: TIMEOUT) key = cache_key_for(raw_key) Redis::Cache.with do |redis| redis.multi do |m| m.sadd(key, value) m.expire(key, timeout) end end end |
.set_includes?(raw_key, value) ⇒ Boolean
Returns true if the given value is present in the set.
raw_key - The key of the set to check. value - The value to check for.
108 109 110 111 112 113 114 |
# File 'lib/gitlab/cache/import/caching.rb', line 108 def self.set_includes?(raw_key, value) key = cache_key_for(raw_key) Redis::Cache.with do |redis| redis.sismember(key, value) end end |
.write(raw_key, value, timeout: TIMEOUT) ⇒ Object
Sets a cache key to the given value.
key - The cache key to write. value - The value to set. timeout - The time after which the cache key should expire.
63 64 65 66 67 68 69 70 71 |
# File 'lib/gitlab/cache/import/caching.rb', line 63 def self.write(raw_key, value, timeout: TIMEOUT) key = cache_key_for(raw_key) Redis::Cache.with do |redis| redis.set(key, value, ex: timeout) end value end |
.write_if_greater(raw_key, value, timeout: TIMEOUT) ⇒ Object
Sets a key to the given integer but only if the existing value is smaller than the given value.
This method uses a Lua script to ensure the read and write are atomic.
raw_key - The key to set. value - The new value for the key. timeout - The key timeout in seconds.
Returns true when the key was overwritten, false otherwise.
155 156 157 158 159 160 161 162 163 |
# File 'lib/gitlab/cache/import/caching.rb', line 155 def self.write_if_greater(raw_key, value, timeout: TIMEOUT) key = cache_key_for(raw_key) val = Redis::Cache.with do |redis| redis .eval(WRITE_IF_GREATER_SCRIPT, keys: [key], argv: [value, timeout]) end val ? true : false end |
.write_multiple(mapping, key_prefix: nil, timeout: TIMEOUT) ⇒ Object
Sets multiple keys to given values.
mapping - A Hash mapping the cache keys to their values. key_prefix - prefix inserted before each key timeout - The time after which the cache key should expire.
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/gitlab/cache/import/caching.rb', line 121 def self.write_multiple(mapping, key_prefix: nil, timeout: TIMEOUT) Redis::Cache.with do |redis| redis.pipelined do |multi| mapping.each do |raw_key, value| key = cache_key_for("#{key_prefix}#{raw_key}") multi.set(key, value, ex: timeout) end end end end |