Merge branch 'use-project-id-in-repo-cache' into 'master'

Use project ID in repository cache to prevent stale data from persisting across projects

See merge request !5460
This commit is contained in:
Yorick Peterse 2016-07-25 13:16:19 +00:00
commit 8987257498
4 changed files with 15 additions and 10 deletions

View File

@ -9,6 +9,9 @@ v 8.11.0 (unreleased)
- Load project invited groups and members eagerly in ProjectTeam#fetch_members
- Add GitLab Workhorse version to admin dashboard (Katarzyna Kobierska Ula Budziszewska)
v 8.10.2 (unreleased)
- Use project ID in repository cache to prevent stale data from persisting across projects
v 8.10.1 (unreleased)
- Fix Error 500 when creating Wiki pages with hyphens or spaces
- Ignore invalid trusted proxies in X-Forwarded-For header

View File

@ -1033,7 +1033,7 @@ class Repository
private
def cache
@cache ||= RepositoryCache.new(path_with_namespace)
@cache ||= RepositoryCache.new(path_with_namespace, @project.id)
end
def head_exists?

View File

@ -1,14 +1,15 @@
# Interface to the Redis-backed cache store used by the Repository model
class RepositoryCache
attr_reader :namespace, :backend
attr_reader :namespace, :backend, :project_id
def initialize(namespace, backend = Rails.cache)
def initialize(namespace, project_id, backend = Rails.cache)
@namespace = namespace
@backend = backend
@project_id = project_id
end
def cache_key(type)
"#{type}:#{namespace}"
"#{type}:#{namespace}:#{project_id}"
end
def expire(key)

View File

@ -1,33 +1,34 @@
require_relative '../../lib/repository_cache'
require 'spec_helper'
describe RepositoryCache, lib: true do
let(:project) { create(:project) }
let(:backend) { double('backend').as_null_object }
let(:cache) { RepositoryCache.new('example', backend) }
let(:cache) { RepositoryCache.new('example', project.id, backend) }
describe '#cache_key' do
it 'includes the namespace' do
expect(cache.cache_key(:foo)).to eq 'foo:example'
expect(cache.cache_key(:foo)).to eq "foo:example:#{project.id}"
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')
expect(backend).to have_received(:delete).with("foo:example:#{project.id}")
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')
expect(backend).to have_received(:fetch).with("bar:example:#{project.id}")
end
it 'accepts a block' do
p = -> {}
cache.fetch(:baz, &p)
expect(backend).to have_received(:fetch).with('baz:example', &p)
expect(backend).to have_received(:fetch).with("baz:example:#{project.id}", &p)
end
end
end