debian-mirror-gitlab/lib/gitlab/set_cache.rb

78 lines
1.7 KiB
Ruby
Raw Normal View History

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
def cache_key(key)
"#{key}:set"
end
# Returns the number of keys deleted by Redis
def expire(*keys)
return 0 if keys.empty?
with do |redis|
keys = keys.map { |key| cache_key(key) }
2020-07-28 23:09:34 +05:30
Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do
2020-10-24 23:57:45 +05:30
redis.unlink(*keys)
2020-07-28 23:09:34 +05:30
end
2020-04-08 14:13:33 +05:30
end
end
def exist?(key)
with { |redis| redis.exists(cache_key(key)) }
end
def write(key, value)
with do |redis|
redis.pipelined do
redis.sadd(cache_key(key), value)
redis.expire(cache_key(key), expires_in)
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|
redis.multi do
redis.sismember(full_key, value)
redis.exists(full_key)
end
end
end
2020-04-08 14:13:33 +05:30
def ttl(key)
with { |redis| redis.ttl(cache_key(key)) }
end
private
def with(&blk)
Gitlab::Redis::Cache.with(&blk) # rubocop:disable CodeReuse/ActiveRecord
end
end
end