diff options
author | Robert Speicher <robert@gitlab.com> | 2016-08-09 23:49:12 +0000 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2016-08-10 14:31:15 +0200 |
commit | 813e0bb78868075f30ccde95bd91fad571b6e55e (patch) | |
tree | 16496adb15d2c7bc1f5bb594c78b6beafa9675ee | |
parent | 7bdcc712337b4c6b7d424b902828e6d63b66c665 (diff) | |
download | gitlab-ce-813e0bb78868075f30ccde95bd91fad571b6e55e.tar.gz |
Merge branch 'add-caching-for-project-count' into 'master'
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.
See merge request !5746
-rw-r--r-- | app/models/project.rb | 6 | ||||
-rw-r--r-- | spec/models/project_spec.rb | 14 | ||||
-rw-r--r-- | spec/spec_helper.rb | 7 |
3 files changed, 27 insertions, 0 deletions
diff --git a/app/models/project.rb b/app/models/project.rb index ae4065f91cb..c5ca58b9697 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -378,6 +378,12 @@ class Project < ActiveRecord::Base joins(join_body).reorder('join_note_counts.amount DESC') end + + def cached_count + Rails.cache.fetch('total_project_count', expires_in: 5.minutes) do + Project.count + end + end end def repository_storage_path diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index c884bb31199..96280aa5ab6 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -667,6 +667,20 @@ describe Project, models: true do it { expect(project.builds_enabled?).to be_truthy } 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 let(:group) { create(:group, :public) } let(:project1) { create(:empty_project, :public, group: group) } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3638dcbb2d3..d2cbd10b7dd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -44,6 +44,13 @@ RSpec.configure do |config| config.before(:suite) do TestEnv.init 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 FactoryGirl::SyntaxRunner.class_eval do |