debian-mirror-gitlab/lib/gitlab/discussions_diff/highlight_cache.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

103 lines
3 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
#
module Gitlab
module DiscussionsDiff
class HighlightCache
2020-11-24 15:15:51 +05:30
extend Gitlab::Utils::Gzip
2019-02-15 15:39:39 +05:30
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)
2023-01-13 00:05:48 +05:30
with_redis do |redis|
Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do
2023-07-09 08:55:56 +05:30
redis.pipelined do |pipelined|
2023-01-13 00:05:48 +05:30
mapping.each do |raw_key, value|
key = cache_key_for(raw_key)
2019-02-15 15:39:39 +05:30
2023-07-09 08:55:56 +05:30
pipelined.set(key, gzip_compress(value.to_json), ex: EXPIRATION)
2023-01-13 00:05:48 +05:30
end
2019-02-15 15:39:39 +05:30
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 =
2023-01-13 00:05:48 +05:30
with_redis do |redis|
2020-07-28 23:09:34 +05:30
Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do
2023-07-09 08:55:56 +05:30
if ::Feature.enabled?(:use_pipeline_over_multikey)
redis.pipelined do |pipeline|
keys.each { |key| pipeline.get(key) }
end
else
redis.mget(keys)
end
2020-07-28 23:09:34 +05:30
end
2019-02-15 15:39:39 +05:30
end
content.map! do |lines|
next unless lines
2020-11-24 15:15:51 +05:30
Gitlab::Json.parse(gzip_decompress(lines)).map! do |line|
2020-01-01 13:55:28 +05:30
Gitlab::Diff::Line.safe_init_from_hash(line)
2019-02-15 15:39:39 +05:30
end
end
end
2019-07-31 22:56:46 +05:30
# 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) }
2023-01-13 00:05:48 +05:30
with_redis do |redis|
2020-07-28 23:09:34 +05:30
Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do
2023-07-09 08:55:56 +05:30
if ::Feature.enabled?(:use_pipeline_over_multikey)
redis.pipelined do |pipeline|
keys.each { |key| pipeline.del(key) }
end.sum
else
redis.del(keys)
end
2020-07-28 23:09:34 +05:30
end
end
2019-07-31 22:56:46 +05:30
end
2019-02-15 15:39:39 +05:30
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
2023-01-13 00:05:48 +05:30
def with_redis(&block)
Redis::Cache.with(&block) # rubocop:disable CodeReuse/ActiveRecord
end
2019-02-15 15:39:39 +05:30
end
end
end
end