Commit e65e1842 authored by Douwe Maan's avatar Douwe Maan

Merge branch '3062-improve-project-cache' into 'master'

Change project cache key to depend on ID instead of full path

Closes #42191

See merge request gitlab-org/gitlab-ce!23135
parents 646ba241 7d629787
...@@ -1049,11 +1049,19 @@ class Repository ...@@ -1049,11 +1049,19 @@ class Repository
end end
def cache def cache
@cache ||= Gitlab::RepositoryCache.new(self) @cache ||= if is_wiki
Gitlab::RepositoryCache.new(self, extra_namespace: 'wiki')
else
Gitlab::RepositoryCache.new(self)
end
end end
def request_store_cache def request_store_cache
@request_store_cache ||= Gitlab::RepositoryCache.new(self, backend: Gitlab::SafeRequestStore) @request_store_cache ||= if is_wiki
Gitlab::RepositoryCache.new(self, extra_namespace: 'wiki', backend: Gitlab::SafeRequestStore)
else
Gitlab::RepositoryCache.new(self, backend: Gitlab::SafeRequestStore)
end
end end
def tags_sorted_by_committed_date def tags_sorted_by_committed_date
......
...@@ -42,7 +42,7 @@ module Gitlab ...@@ -42,7 +42,7 @@ module Gitlab
end end
def self.cache_key_for_project(project) def self.cache_key_for_project(project)
"#{Gitlab::Redis::Cache::CACHE_NAMESPACE}:projects/#{project.id}/pipeline_status/#{project.commit&.sha}" "#{Gitlab::Redis::Cache::CACHE_NAMESPACE}:project:#{project.id}:pipeline_status:#{project.commit&.sha}"
end end
def self.update_for_pipeline(pipeline) def self.update_for_pipeline(pipeline)
......
...@@ -7,13 +7,13 @@ module Gitlab ...@@ -7,13 +7,13 @@ module Gitlab
def initialize(repository, extra_namespace: nil, backend: Rails.cache) def initialize(repository, extra_namespace: nil, backend: Rails.cache)
@repository = repository @repository = repository
@namespace = "#{repository.full_path}:#{repository.project.id}" @namespace = "project:#{repository.project.id}"
@namespace = "#{@namespace}:#{extra_namespace}" if extra_namespace @namespace = "#{@namespace}:#{extra_namespace}" if extra_namespace
@backend = backend @backend = backend
end end
def cache_key(type) def cache_key(type)
"#{type}:#{namespace}" "#{namespace}:#{type}"
end end
def expire(key) def expire(key)
......
...@@ -4,14 +4,14 @@ describe Gitlab::RepositoryCache do ...@@ -4,14 +4,14 @@ describe Gitlab::RepositoryCache do
let(:backend) { double('backend').as_null_object } let(:backend) { double('backend').as_null_object }
let(:project) { create(:project) } let(:project) { create(:project) }
let(:repository) { project.repository } let(:repository) { project.repository }
let(:namespace) { "#{repository.full_path}:#{project.id}" } let(:namespace) { "project:#{project.id}" }
let(:cache) { described_class.new(repository, backend: backend) } let(:cache) { described_class.new(repository, backend: backend) }
describe '#cache_key' do describe '#cache_key' do
subject { cache.cache_key(:foo) } subject { cache.cache_key(:foo) }
it 'includes the namespace' do it 'includes the namespace' do
expect(subject).to eq "foo:#{namespace}" expect(subject).to eq "#{namespace}:foo"
end end
context 'with a given namespace' do context 'with a given namespace' do
...@@ -22,7 +22,7 @@ describe Gitlab::RepositoryCache do ...@@ -22,7 +22,7 @@ describe Gitlab::RepositoryCache do
end end
it 'includes the full namespace' do it 'includes the full namespace' do
expect(subject).to eq "foo:#{namespace}:#{extra_namespace}" expect(subject).to eq "#{namespace}:#{extra_namespace}:foo"
end end
end end
end end
...@@ -30,21 +30,21 @@ describe Gitlab::RepositoryCache do ...@@ -30,21 +30,21 @@ describe Gitlab::RepositoryCache do
describe '#expire' do describe '#expire' do
it 'expires the given key from the cache' do it 'expires the given key from the cache' do
cache.expire(:foo) cache.expire(:foo)
expect(backend).to have_received(:delete).with("foo:#{namespace}") expect(backend).to have_received(:delete).with("#{namespace}:foo")
end end
end end
describe '#fetch' do describe '#fetch' do
it 'fetches the given key from the cache' do it 'fetches the given key from the cache' do
cache.fetch(:bar) cache.fetch(:bar)
expect(backend).to have_received(:fetch).with("bar:#{namespace}") expect(backend).to have_received(:fetch).with("#{namespace}:bar")
end end
it 'accepts a block' do it 'accepts a block' do
p = -> {} p = -> {}
cache.fetch(:baz, &p) cache.fetch(:baz, &p)
expect(backend).to have_received(:fetch).with("baz:#{namespace}", &p) expect(backend).to have_received(:fetch).with("#{namespace}:baz", &p)
end end
end end
...@@ -67,7 +67,7 @@ describe Gitlab::RepositoryCache do ...@@ -67,7 +67,7 @@ describe Gitlab::RepositoryCache do
end end
it 'caches the value' do it 'caches the value' do
expect(backend).to receive(:write).with("#{key}:#{namespace}", true) expect(backend).to receive(:write).with("#{namespace}:#{key}", true)
cache.fetch_without_caching_false(key) { true } cache.fetch_without_caching_false(key) { true }
end end
...@@ -83,7 +83,7 @@ describe Gitlab::RepositoryCache do ...@@ -83,7 +83,7 @@ describe Gitlab::RepositoryCache do
end end
it 'does not cache the value' do it 'does not cache the value' do
expect(backend).not_to receive(:write).with("#{key}:#{namespace}", true) expect(backend).not_to receive(:write).with("#{namespace}:#{key}", true)
cache.fetch_without_caching_false(key, &p) cache.fetch_without_caching_false(key, &p)
end end
...@@ -92,7 +92,7 @@ describe Gitlab::RepositoryCache do ...@@ -92,7 +92,7 @@ describe Gitlab::RepositoryCache do
context 'when the cached value is truthy' do context 'when the cached value is truthy' do
before do before do
backend.write("#{key}:#{namespace}", true) backend.write("#{namespace}:#{key}", true)
end end
it 'returns the cached value' do it 'returns the cached value' do
...@@ -116,7 +116,7 @@ describe Gitlab::RepositoryCache do ...@@ -116,7 +116,7 @@ describe Gitlab::RepositoryCache do
context 'when the cached value is falsey' do context 'when the cached value is falsey' do
before do before do
backend.write("#{key}:#{namespace}", false) backend.write("#{namespace}:#{key}", false)
end end
it 'returns the result of the block' do it 'returns the result of the block' do
...@@ -126,7 +126,7 @@ describe Gitlab::RepositoryCache do ...@@ -126,7 +126,7 @@ describe Gitlab::RepositoryCache do
end end
it 'writes the truthy value to the cache' do it 'writes the truthy value to the cache' do
expect(backend).to receive(:write).with("#{key}:#{namespace}", 'block result') expect(backend).to receive(:write).with("#{namespace}:#{key}", 'block result')
cache.fetch_without_caching_false(key) { 'block result' } cache.fetch_without_caching_false(key) { 'block result' }
end end
......
...@@ -2301,4 +2301,22 @@ describe Repository do ...@@ -2301,4 +2301,22 @@ describe Repository do
repository.merge_base('master', 'fix') repository.merge_base('master', 'fix')
end end
end end
describe '#cache' do
subject(:cache) { repository.send(:cache) }
it 'returns a RepositoryCache' do
expect(subject).to be_kind_of Gitlab::RepositoryCache
end
it 'when is_wiki it includes wiki as part of key' do
allow(repository).to receive(:is_wiki) { true }
expect(subject.namespace).to include('wiki')
end
it 'when is_wiki is false extra_namespace is nil' do
expect(subject.namespace).not_to include('wiki')
end
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