Commit a3c2873f authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge pull request #8676 from tsigo/rs-repository-cache

Refactor Repository to use new RepositoryCache class
parents 5ec36902 4eafc188
...@@ -30,7 +30,7 @@ class Repository ...@@ -30,7 +30,7 @@ class Repository
commit = Gitlab::Git::Commit.find(raw_repository, id) commit = Gitlab::Git::Commit.find(raw_repository, id)
commit = Commit.new(commit) if commit commit = Commit.new(commit) if commit
commit commit
rescue Rugged::OdbError => ex rescue Rugged::OdbError
nil nil
end end
...@@ -61,25 +61,25 @@ class Repository ...@@ -61,25 +61,25 @@ class Repository
end end
def add_branch(branch_name, ref) def add_branch(branch_name, ref)
Rails.cache.delete(cache_key(:branch_names)) cache.expire(:branch_names)
gitlab_shell.add_branch(path_with_namespace, branch_name, ref) gitlab_shell.add_branch(path_with_namespace, branch_name, ref)
end end
def add_tag(tag_name, ref, message = nil) def add_tag(tag_name, ref, message = nil)
Rails.cache.delete(cache_key(:tag_names)) cache.expire(:tag_names)
gitlab_shell.add_tag(path_with_namespace, tag_name, ref, message) gitlab_shell.add_tag(path_with_namespace, tag_name, ref, message)
end end
def rm_branch(branch_name) def rm_branch(branch_name)
Rails.cache.delete(cache_key(:branch_names)) cache.expire(:branch_names)
gitlab_shell.rm_branch(path_with_namespace, branch_name) gitlab_shell.rm_branch(path_with_namespace, branch_name)
end end
def rm_tag(tag_name) def rm_tag(tag_name)
Rails.cache.delete(cache_key(:tag_names)) cache.expire(:tag_names)
gitlab_shell.rm_tag(path_with_namespace, tag_name) gitlab_shell.rm_tag(path_with_namespace, tag_name)
end end
...@@ -97,19 +97,15 @@ class Repository ...@@ -97,19 +97,15 @@ class Repository
end end
def branch_names def branch_names
Rails.cache.fetch(cache_key(:branch_names)) do cache.fetch(:branch_names) { raw_repository.branch_names }
raw_repository.branch_names
end
end end
def tag_names def tag_names
Rails.cache.fetch(cache_key(:tag_names)) do cache.fetch(:tag_names) { raw_repository.tag_names }
raw_repository.tag_names
end
end end
def commit_count def commit_count
Rails.cache.fetch(cache_key(:commit_count)) do cache.fetch(:commit_count) do
begin begin
raw_repository.commit_count(self.root_ref) raw_repository.commit_count(self.root_ref)
rescue rescue
...@@ -121,26 +117,19 @@ class Repository ...@@ -121,26 +117,19 @@ class Repository
# Return repo size in megabytes # Return repo size in megabytes
# Cached in redis # Cached in redis
def size def size
Rails.cache.fetch(cache_key(:size)) do cache.fetch(:size) { raw_repository.size }
raw_repository.size
end
end end
def expire_cache def expire_cache
Rails.cache.delete(cache_key(:size)) %i(size branch_names tag_names commit_count graph_log
Rails.cache.delete(cache_key(:branch_names)) readme version contribution_guide).each do |key|
Rails.cache.delete(cache_key(:tag_names)) cache.expire(key)
Rails.cache.delete(cache_key(:commit_count)) end
Rails.cache.delete(cache_key(:graph_log))
Rails.cache.delete(cache_key(:readme))
Rails.cache.delete(cache_key(:version))
Rails.cache.delete(cache_key(:contribution_guide))
end end
def graph_log def graph_log
Rails.cache.fetch(cache_key(:graph_log)) do cache.fetch(:graph_log) do
commits = raw_repository.log(limit: 6000, commits = raw_repository.log(limit: 6000, skip_merges: true,
skip_merges: true,
ref: root_ref) ref: root_ref)
commits.map do |rugged_commit| commits.map do |rugged_commit|
...@@ -176,10 +165,6 @@ class Repository ...@@ -176,10 +165,6 @@ class Repository
end end
end end
def cache_key(type)
"#{type}:#{path_with_namespace}"
end
def method_missing(m, *args, &block) def method_missing(m, *args, &block)
raw_repository.send(m, *args, &block) raw_repository.send(m, *args, &block)
end end
...@@ -199,13 +184,11 @@ class Repository ...@@ -199,13 +184,11 @@ class Repository
end end
def readme def readme
Rails.cache.fetch(cache_key(:readme)) do cache.fetch(:readme) { tree(:head).readme }
tree(:head).readme
end
end end
def version def version
Rails.cache.fetch(cache_key(:version)) do cache.fetch(:version) do
tree(:head).blobs.find do |file| tree(:head).blobs.find do |file|
file.name.downcase == 'version' file.name.downcase == 'version'
end end
...@@ -213,9 +196,7 @@ class Repository ...@@ -213,9 +196,7 @@ class Repository
end end
def contribution_guide def contribution_guide
Rails.cache.fetch(cache_key(:contribution_guide)) do cache.fetch(:contribution_guide) { tree(:head).contribution_guide }
tree(:head).contribution_guide
end
end end
def head_commit def head_commit
...@@ -351,4 +332,10 @@ class Repository ...@@ -351,4 +332,10 @@ class Repository
[] []
end end
end end
private
def cache
@cache ||= RepositoryCache.new(path_with_namespace)
end
end end
# Interface to the Redis-backed cache store used by the Repository model
class RepositoryCache
attr_reader :namespace
def initialize(namespace, backend = Rails.cache)
@namespace = namespace
@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
private
attr_reader :backend
end
require 'rspec'
require_relative '../../lib/repository_cache'
describe RepositoryCache do
let(:backend) { double('backend').as_null_object }
let(:cache) { RepositoryCache.new('example', backend) }
describe '#cache_key' do
it 'includes the namespace' do
expect(cache.cache_key(:foo)).to eq 'foo:example'
end
end
describe '#expire' do
it 'expires the given key from the cache' do
cache.expire(:foo)
expect(backend).to have_received(:delete).with('foo:example')
end
end
describe '#fetch' do
it 'fetches the given key from the cache' do
cache.fetch(:bar)
expect(backend).to have_received(:fetch).with('bar:example')
end
it 'accepts a block' do
p = -> {}
cache.fetch(:baz, &p)
expect(backend).to have_received(:fetch).with('baz:example', &p)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment