2020-04-08 14:13:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Interface to the Redis-backed cache store to keep track of complete cache keys
|
|
|
|
# for a ReactiveCache resource.
|
|
|
|
module Gitlab
|
|
|
|
class SetCache
|
|
|
|
attr_reader :expires_in
|
|
|
|
|
|
|
|
def initialize(expires_in: 2.weeks)
|
|
|
|
@expires_in = expires_in
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def cache_key(key)
|
2021-09-04 01:27:46 +05:30
|
|
|
"#{cache_namespace}:#{key}:set"
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
# Returns the number of keys deleted by Redis
|
|
|
|
def expire(*keys)
|
|
|
|
return 0 if keys.empty?
|
|
|
|
|
|
|
|
with do |redis|
|
2021-09-04 01:27:46 +05:30
|
|
|
keys_to_expire = keys.map { |key| cache_key(key) }
|
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_to_expire.each { |key| pipeline.unlink(key) }
|
|
|
|
end.sum
|
|
|
|
else
|
|
|
|
redis.unlink(*keys_to_expire)
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def exist?(key)
|
2022-11-25 23:54:43 +05:30
|
|
|
with { |redis| redis.exists?(cache_key(key)) } # rubocop:disable CodeReuse/ActiveRecord
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def write(key, value)
|
|
|
|
with do |redis|
|
2022-10-11 01:57:18 +05:30
|
|
|
redis.pipelined do |pipeline|
|
2023-01-13 00:05:48 +05:30
|
|
|
pipeline.sadd?(cache_key(key), value)
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
pipeline.expire(cache_key(key), expires_in)
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
|
|
|
def read(key)
|
|
|
|
with { |redis| redis.smembers(cache_key(key)) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def include?(key, value)
|
|
|
|
with { |redis| redis.sismember(cache_key(key), value) }
|
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
# Like include?, but also tells us if the cache was populated when it ran
|
|
|
|
# by returning two booleans: [member_exists, set_exists]
|
|
|
|
def try_include?(key, value)
|
|
|
|
full_key = cache_key(key)
|
|
|
|
|
|
|
|
with do |redis|
|
2022-10-11 01:57:18 +05:30
|
|
|
redis.multi do |multi|
|
|
|
|
multi.sismember(full_key, value)
|
2022-11-25 23:54:43 +05:30
|
|
|
multi.exists?(full_key) # rubocop:disable CodeReuse/ActiveRecord
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def ttl(key)
|
|
|
|
with { |redis| redis.ttl(cache_key(key)) }
|
|
|
|
end
|
|
|
|
|
2022-09-01 20:07:04 +05:30
|
|
|
def count(key)
|
|
|
|
with { |redis| redis.scard(cache_key(key)) }
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def with(&blk)
|
|
|
|
Gitlab::Redis::Cache.with(&blk) # rubocop:disable CodeReuse/ActiveRecord
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
def cache_namespace
|
|
|
|
Gitlab::Redis::Cache::CACHE_NAMESPACE
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
end
|