2018-10-22 07:00:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-07 00:12:29 +00:00
|
|
|
# Interface to the Redis-backed cache store
|
|
|
|
module Gitlab
|
|
|
|
class RepositoryCache
|
|
|
|
attr_reader :repository, :namespace, :backend
|
|
|
|
|
2018-03-07 01:25:12 +00:00
|
|
|
def initialize(repository, extra_namespace: nil, backend: Rails.cache)
|
2018-03-07 00:12:29 +00:00
|
|
|
@repository = repository
|
2018-11-16 03:56:27 +00:00
|
|
|
@namespace = "project:#{repository.project.id}"
|
2018-10-22 07:00:50 +00:00
|
|
|
@namespace = "#{@namespace}:#{extra_namespace}" if extra_namespace
|
2018-03-07 00:12:29 +00:00
|
|
|
@backend = backend
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_key(type)
|
2018-11-16 03:56:27 +00:00
|
|
|
"#{namespace}:#{type}"
|
2018-03-07 00:12:29 +00:00
|
|
|
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-09-25 17:12:51 +00:00
|
|
|
|
|
|
|
def write(key, value)
|
|
|
|
backend.write(cache_key(key), value)
|
|
|
|
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
|
2018-03-07 00:12:29 +00:00
|
|
|
end
|
|
|
|
end
|