Class: Gitlab::DiscussionsDiff::HighlightCache
- Inherits:
-
Object
- Object
- Gitlab::DiscussionsDiff::HighlightCache
- Extended by:
- Utils::Gzip
- Defined in:
- lib/gitlab/discussions_diff/highlight_cache.rb
Constant Summary collapse
- VERSION =
1
- EXPIRATION =
1.week
Class Method Summary collapse
- .cache_key_for(raw_key) ⇒ Object
-
.clear_multiple(raw_keys) ⇒ Object
Clears multiple cache keys at once.
-
.read_multiple(raw_keys) ⇒ Object
Reads multiple cache keys at once.
-
.write_multiple(mapping) ⇒ Object
Sets multiple keys to a given value.
Methods included from Utils::Gzip
gzip_compress, gzip_decompress
Class Method Details
.cache_key_for(raw_key) ⇒ Object
72 73 74 |
# File 'lib/gitlab/discussions_diff/highlight_cache.rb', line 72 def cache_key_for(raw_key) "#{cache_key_prefix}:#{raw_key}" end |
.clear_multiple(raw_keys) ⇒ Object
Clears multiple cache keys at once.
raw_keys - An Array of unique cache keys, without namespaces.
It returns the number of cache keys cleared. Ex.: 42
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/gitlab/discussions_diff/highlight_cache.rb', line 60 def clear_multiple(raw_keys) return [] if raw_keys.empty? keys = raw_keys.map { |id| cache_key_for(id) } Redis::Cache.with do |redis| Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do redis.del(keys) end end end |
.read_multiple(raw_keys) ⇒ Object
Reads multiple cache keys at once.
raw_keys - An Array of unique cache keys, without namespaces.
It returns a list of deserialized diff lines. Ex.:
- [Gitlab::Diff::Line, …], [Gitlab::Diff::Line]
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/gitlab/discussions_diff/highlight_cache.rb', line 34 def read_multiple(raw_keys) return [] if raw_keys.empty? keys = raw_keys.map { |id| cache_key_for(id) } content = Redis::Cache.with do |redis| Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do redis.mget(keys) end end content.map! do |lines| next unless lines Gitlab::Json.parse(gzip_decompress(lines)).map! do |line| Gitlab::Diff::Line.safe_init_from_hash(line) end end end |
.write_multiple(mapping) ⇒ Object
Sets multiple keys to a given value. The value is serialized as JSON.
mapping - Write multiple cache values at once
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gitlab/discussions_diff/highlight_cache.rb', line 16 def write_multiple(mapping) Redis::Cache.with do |redis| redis.multi do |multi| mapping.each do |raw_key, value| key = cache_key_for(raw_key) multi.set(key, gzip_compress(value.to_json), ex: EXPIRATION) end end end end |