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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

57 lines
1.2 KiB
Ruby
Raw Permalink Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-03-27 19:54:05 +05:30
# Interface to the Redis-backed cache store
module Gitlab
class RepositoryCache
attr_reader :repository, :namespace, :backend
2023-03-17 16:20:25 +05:30
def initialize(repository, extra_namespace: nil, backend: self.class.store)
2018-03-27 19:54:05 +05:30
@repository = repository
2020-03-13 15:44:24 +05:30
@namespace = "#{repository.full_path}"
@namespace += ":#{repository.project.id}" if repository.project
2018-12-13 13:39:08 +05:30
@namespace = "#{@namespace}:#{extra_namespace}" if extra_namespace
2018-03-27 19:54:05 +05:30
@backend = backend
end
def cache_key(type)
"#{type}:#{namespace}"
end
def expire(key)
backend.delete(cache_key(key))
end
def fetch(key, &block)
backend.fetch(cache_key(key), &block)
end
def exist?(key)
backend.exist?(cache_key(key))
end
def read(key)
backend.read(cache_key(key))
end
2018-12-05 23:21:45 +05:30
2020-03-13 15:44:24 +05:30
def write(key, value, *args)
backend.write(cache_key(key), value, *args)
2018-12-05 23:21:45 +05:30
end
def fetch_without_caching_false(key, &block)
value = read(key)
return value if value
value = yield
# Don't cache false values
write(key, value) if value
value
end
2023-03-17 16:20:25 +05:30
def self.store
2023-04-23 21:23:45 +05:30
Gitlab::Redis::RepositoryCache.cache_store
2023-03-17 16:20:25 +05:30
end
2018-03-27 19:54:05 +05:30
end
end