Commit f5f2644d authored by Gary Holtz's avatar Gary Holtz Committed by Andy Soiron

Caching the protected branch check

Changelog: performance
parent 1f1e125f
...@@ -26,8 +26,10 @@ class ProtectedBranch < ApplicationRecord ...@@ -26,8 +26,10 @@ class ProtectedBranch < ApplicationRecord
def self.protected?(project, ref_name) def self.protected?(project, ref_name)
return true if project.empty_repo? && project.default_branch_protected? return true if project.empty_repo? && project.default_branch_protected?
Rails.cache.fetch("protected_ref-#{ref_name}-#{project.cache_key}") do
self.matching(ref_name, protected_refs: protected_refs(project)).present? self.matching(ref_name, protected_refs: protected_refs(project)).present?
end end
end
def self.allow_force_push?(project, ref_name) def self.allow_force_push?(project, ref_name)
project.protected_branches.allowing_force_push.matching(ref_name).any? project.protected_branches.allowing_force_push.matching(ref_name).any?
......
...@@ -162,6 +162,30 @@ RSpec.describe ProtectedBranch do ...@@ -162,6 +162,30 @@ RSpec.describe ProtectedBranch do
expect(described_class.protected?(project, 'staging/some-branch')).to eq(false) expect(described_class.protected?(project, 'staging/some-branch')).to eq(false)
end end
context 'with caching', :use_clean_rails_memory_store_caching do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:protected_branch) { create(:protected_branch, project: project, name: "jawn") }
before do
allow(described_class).to receive(:matching).once.and_call_original
# the original call works and warms the cache
described_class.protected?(project, 'jawn')
end
it 'correctly invalidates a cache' do
expect(described_class).to receive(:matching).once.and_call_original
create(:protected_branch, project: project, name: "bar")
# the cache is invalidated because the project has been "updated"
expect(described_class.protected?(project, 'jawn')).to eq(true)
end
it 'correctly uses the cached version' do
expect(described_class).not_to receive(:matching)
expect(described_class.protected?(project, 'jawn')).to eq(true)
end
end
end end
context 'new project' do context 'new project' do
......
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