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
|
|
|
|
|
|
|
|
def cache_key(type)
|
2019-09-10 09:09:55 -04:00
|
|
|
"#{type}:#{namespace}:set"
|
2019-08-29 13:17:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
2019-11-04 13:06:28 -05:00
|
|
|
if exist?(key)
|
|
|
|
read(key)
|
|
|
|
else
|
|
|
|
write(key, yield)
|
|
|
|
end
|
2019-08-29 13:17:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|