2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Interface to the Redis-backed cache store for keys that use a Redis set
|
|
|
|
module Gitlab
|
2020-04-08 14:13:33 +05:30
|
|
|
class RepositorySetCache < Gitlab::SetCache
|
2019-12-04 20:38:33 +05:30
|
|
|
attr_reader :repository, :namespace, :expires_in
|
|
|
|
|
|
|
|
def initialize(repository, extra_namespace: nil, expires_in: 2.weeks)
|
|
|
|
@repository = repository
|
2020-03-13 15:44:24 +05:30
|
|
|
@namespace = "#{repository.full_path}"
|
|
|
|
@namespace += ":#{repository.project.id}" if repository.project
|
2019-12-04 20:38:33 +05:30
|
|
|
@namespace = "#{@namespace}:#{extra_namespace}" if extra_namespace
|
|
|
|
@expires_in = expires_in
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def cache_key(type)
|
2021-09-04 01:27:46 +05:30
|
|
|
super("#{type}:#{namespace}")
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def write(key, value)
|
|
|
|
full_key = cache_key(key)
|
|
|
|
|
|
|
|
with do |redis|
|
2022-10-11 01:57:18 +05:30
|
|
|
redis.multi do |multi|
|
|
|
|
multi.unlink(full_key)
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
# Splitting into groups of 1000 prevents us from creating a too-long
|
|
|
|
# Redis command
|
2022-10-11 01:57:18 +05:30
|
|
|
value.each_slice(1000) { |subset| multi.sadd(full_key, subset) }
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
multi.expire(full_key, expires_in)
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch(key, &block)
|
2021-04-29 21:17:54 +05:30
|
|
|
full_key = cache_key(key)
|
|
|
|
|
|
|
|
smembers, exists = with do |redis|
|
2022-10-11 01:57:18 +05:30
|
|
|
redis.multi do |multi|
|
|
|
|
multi.smembers(full_key)
|
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
|
|
|
|
|
|
|
|
return smembers if exists
|
|
|
|
|
|
|
|
write(key, yield)
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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|
|
2022-11-25 23:54:43 +05:30
|
|
|
exists = redis.exists?(full_key) # rubocop:disable CodeReuse/ActiveRecord
|
2021-04-29 21:17:54 +05:30
|
|
|
write(key, yield) unless exists
|
|
|
|
|
|
|
|
redis.sscan_each(full_key, match: pattern)
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def cache
|
|
|
|
if Feature.enabled?(:use_primary_and_secondary_stores_for_repository_cache) ||
|
|
|
|
Feature.enabled?(:use_primary_store_as_default_for_repository_cache)
|
|
|
|
Gitlab::Redis::RepositoryCache
|
|
|
|
else
|
|
|
|
Gitlab::Redis::Cache
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def with(&blk)
|
|
|
|
cache.with(&blk) # rubocop:disable CodeReuse/ActiveRecord
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|