diff options
author | Alejandro RodrÃguez <alejorro70@gmail.com> | 2018-03-07 03:12:29 +0300 |
---|---|---|
committer | Alejandro RodrÃguez <alejorro70@gmail.com> | 2018-03-07 03:12:29 +0300 |
commit | 5171e2f3d4fdc681a58e11f9615afa968324a278 (patch) | |
tree | 256a12ed77a7a6aabff58bc267a70d476cc4dfa7 /lib/gitlab/repository_cache.rb | |
parent | 35f6efaee05835b75e605e1f269e57a8d6daf3fa (diff) |
Refactor RepositoryCache to make it usable in other classes
Diffstat (limited to 'lib/gitlab/repository_cache.rb')
-rw-r--r-- | lib/gitlab/repository_cache.rb | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/gitlab/repository_cache.rb b/lib/gitlab/repository_cache.rb new file mode 100644 index 00000000000..b7650f6ed81 --- /dev/null +++ b/lib/gitlab/repository_cache.rb @@ -0,0 +1,32 @@ +# Interface to the Redis-backed cache store +module Gitlab + class RepositoryCache + attr_reader :repository, :namespace, :backend + + def initialize(repository, backend = Rails.cache) + @repository = repository + @namespace = "#{repository.full_path}:#{repository.project.id}" + @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 + end +end |