Add a method in Project to return a cached value of total count of projects
This is in preparation to address the DB load caused by the counting in gitlab-com/infrastructure#303.
This commit is contained in:
parent
936729e5be
commit
1e6316172b
3 changed files with 27 additions and 0 deletions
|
@ -378,6 +378,12 @@ class Project < ActiveRecord::Base
|
||||||
|
|
||||||
joins(join_body).reorder('join_note_counts.amount DESC')
|
joins(join_body).reorder('join_note_counts.amount DESC')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def cached_count
|
||||||
|
Rails.cache.fetch('total_project_count', expires_in: 5.minutes) do
|
||||||
|
Project.count
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def repository_storage_path
|
def repository_storage_path
|
||||||
|
|
|
@ -714,6 +714,20 @@ describe Project, models: true do
|
||||||
it { expect(project.builds_enabled?).to be_truthy }
|
it { expect(project.builds_enabled?).to be_truthy }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '.cached_count', caching: true do
|
||||||
|
let(:group) { create(:group, :public) }
|
||||||
|
let!(:project1) { create(:empty_project, :public, group: group) }
|
||||||
|
let!(:project2) { create(:empty_project, :public, group: group) }
|
||||||
|
|
||||||
|
it 'returns total project count' do
|
||||||
|
expect(Project).to receive(:count).once.and_call_original
|
||||||
|
|
||||||
|
3.times do
|
||||||
|
expect(Project.cached_count).to eq(2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '.trending' do
|
describe '.trending' do
|
||||||
let(:group) { create(:group, :public) }
|
let(:group) { create(:group, :public) }
|
||||||
let(:project1) { create(:empty_project, :public, group: group) }
|
let(:project1) { create(:empty_project, :public, group: group) }
|
||||||
|
|
|
@ -42,6 +42,13 @@ RSpec.configure do |config|
|
||||||
config.before(:suite) do
|
config.before(:suite) do
|
||||||
TestEnv.init
|
TestEnv.init
|
||||||
end
|
end
|
||||||
|
|
||||||
|
config.around(:each, :caching) do |example|
|
||||||
|
caching_store = Rails.cache
|
||||||
|
Rails.cache = ActiveSupport::Cache::MemoryStore.new if example.metadata[:caching]
|
||||||
|
example.run
|
||||||
|
Rails.cache = caching_store
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
FactoryGirl::SyntaxRunner.class_eval do
|
FactoryGirl::SyntaxRunner.class_eval do
|
||||||
|
|
Loading…
Reference in a new issue