2019-08-29 13:17:30 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Interface to the Redis-backed cache store for keys that use a Redis set
|
|
|
|
module Gitlab
|
2020-02-24 04:08:51 -05:00
|
|
|
class RepositorySetCache < Gitlab::SetCache
|
2019-08-29 13:17:30 -04:00
|
|
|
attr_reader :repository, :namespace, :expires_in
|
|
|
|
|
|
|
|
def initialize(repository, extra_namespace: nil, expires_in: 2.weeks)
|
|
|
|
@repository = repository
|
2019-12-27 10:08:16 -05:00
|
|
|
@namespace = "#{repository.full_path}"
|
|
|
|
@namespace += ":#{repository.project.id}" if repository.project
|
2019-08-29 13:17:30 -04:00
|
|
|
@namespace = "#{@namespace}:#{extra_namespace}" if extra_namespace
|
|
|
|
@expires_in = expires_in
|
|
|
|
end
|
|
|
|
|
2021-06-28 11:08:03 -04:00
|
|
|
# NOTE Remove as part of #331319
|
|
|
|
def old_cache_key(type)
|
2019-09-10 09:09:55 -04:00
|
|
|
"#{type}:#{namespace}:set"
|
2019-08-29 13:17:30 -04:00
|
|
|
end
|
|
|
|
|
2021-06-28 11:08:03 -04:00
|
|
|
def cache_key(type)
|
2021-05-25 11:10:33 -04:00
|
|
|
super("#{type}:#{namespace}")
|
|
|
|
end
|
|
|
|
|
2019-08-29 13:17:30 -04:00
|
|
|
def write(key, value)
|
|
|
|
full_key = cache_key(key)
|
|
|
|
|
|
|
|
with do |redis|
|
|
|
|
redis.multi do
|
2020-08-28 11:10:21 -04:00
|
|
|
redis.unlink(full_key)
|
2019-08-29 13:17:30 -04:00
|
|
|
|
|
|
|
# Splitting into groups of 1000 prevents us from creating a too-long
|
|
|
|
# Redis command
|
2019-09-10 09:09:55 -04:00
|
|
|
value.each_slice(1000) { |subset| redis.sadd(full_key, subset) }
|
2019-08-29 13:17:30 -04:00
|
|
|
|
|
|
|
redis.expire(full_key, expires_in)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch(key, &block)
|
2021-03-26 20:09:34 -04:00
|
|
|
full_key = cache_key(key)
|
|
|
|
|
|
|
|
smembers, exists = with do |redis|
|
|
|
|
redis.multi do
|
|
|
|
redis.smembers(full_key)
|
|
|
|
redis.exists(full_key)
|
|
|
|
end
|
2019-11-04 13:06:28 -05:00
|
|
|
end
|
2021-03-26 20:09:34 -04:00
|
|
|
|
|
|
|
return smembers if exists
|
|
|
|
|
|
|
|
write(key, yield)
|
2019-08-29 13:17:30 -04:00
|
|
|
end
|
2021-04-07 14:09:45 -04:00
|
|
|
|
|
|
|
# Searches the cache set using SSCAN with the MATCH option. The MATCH
|
|
|
|
# parameter is the pattern argument.
|
|
|
|
# See https://redis.io/commands/scan#the-match-option for more information.
|
|
|
|
# Returns an Enumerator that enumerates all SSCAN hits.
|
|
|
|
def search(key, pattern, &block)
|
|
|
|
full_key = cache_key(key)
|
|
|
|
|
|
|
|
with do |redis|
|
|
|
|
exists = redis.exists(full_key)
|
|
|
|
write(key, yield) unless exists
|
|
|
|
|
|
|
|
redis.sscan_each(full_key, match: pattern)
|
|
|
|
end
|
|
|
|
end
|
2019-08-29 13:17:30 -04:00
|
|
|
end
|
|
|
|
end
|