2018-12-16 11:00:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
#
|
|
|
|
module Gitlab
|
|
|
|
module DiscussionsDiff
|
|
|
|
class HighlightCache
|
2020-09-01 23:10:30 -04:00
|
|
|
extend Gitlab::Utils::Gzip
|
|
|
|
|
2018-12-16 11:00:43 -05:00
|
|
|
class << self
|
|
|
|
VERSION = 1
|
|
|
|
EXPIRATION = 1.week
|
|
|
|
|
|
|
|
# Sets multiple keys to a given value. The value
|
|
|
|
# is serialized as JSON.
|
|
|
|
#
|
|
|
|
# mapping - Write multiple cache values at once
|
|
|
|
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)
|
|
|
|
|
2020-09-01 23:10:30 -04:00
|
|
|
multi.set(key, gzip_compress(value.to_json), ex: EXPIRATION)
|
2018-12-16 11:00:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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]]
|
|
|
|
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|
|
2020-06-24 17:08:46 -04:00
|
|
|
Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do
|
|
|
|
redis.mget(keys)
|
|
|
|
end
|
2018-12-16 11:00:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
content.map! do |lines|
|
|
|
|
next unless lines
|
|
|
|
|
2020-09-01 23:10:30 -04:00
|
|
|
Gitlab::Json.parse(gzip_decompress(lines)).map! do |line|
|
2019-12-11 10:07:38 -05:00
|
|
|
Gitlab::Diff::Line.safe_init_from_hash(line)
|
2018-12-16 11:00:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-25 10:29:51 -04:00
|
|
|
# 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
|
|
|
|
def clear_multiple(raw_keys)
|
|
|
|
return [] if raw_keys.empty?
|
|
|
|
|
|
|
|
keys = raw_keys.map { |id| cache_key_for(id) }
|
|
|
|
|
2020-06-24 17:08:46 -04:00
|
|
|
Redis::Cache.with do |redis|
|
|
|
|
Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do
|
|
|
|
redis.del(keys)
|
|
|
|
end
|
|
|
|
end
|
2019-03-25 10:29:51 -04:00
|
|
|
end
|
|
|
|
|
2018-12-16 11:00:43 -05:00
|
|
|
def cache_key_for(raw_key)
|
|
|
|
"#{cache_key_prefix}:#{raw_key}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def cache_key_prefix
|
|
|
|
"#{Redis::Cache::CACHE_NAMESPACE}:#{VERSION}:discussion-highlight"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|