summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2017-08-25 15:26:22 +0200
committerYorick Peterse <yorickpeterse@gmail.com>2017-08-30 15:49:40 +0200
commitc158a22fd3cc5c3d60b5e0e1bf44f8aa75617d70 (patch)
tree666ae3be074ad07b8b056d5b19e931600526a39b
parent9e7e0496ff639d1eec65dcbf1b51edb2262456e2 (diff)
downloadgitlab-ce-sidebar-cache-updates.tar.gz
Only update the sidebar count caches when neededsidebar-cache-updates
This ensures the issues/MR cache of the sidebar is only updated when the state or confidential flags changes, instead of changing this for every update.
-rw-r--r--app/models/issue.rb6
-rw-r--r--app/models/merge_request.rb6
-rw-r--r--changelogs/unreleased/sidebar-cache-updates.yml5
-rw-r--r--spec/models/issue_spec.rb18
-rw-r--r--spec/models/merge_request_spec.rb12
5 files changed, 47 insertions, 0 deletions
diff --git a/app/models/issue.rb b/app/models/issue.rb
index b9aa937d2f9..dfcd4030ec3 100644
--- a/app/models/issue.rb
+++ b/app/models/issue.rb
@@ -269,7 +269,13 @@ class Issue < ActiveRecord::Base
end
end
+ def update_project_counter_caches?
+ state_changed? || confidential_changed?
+ end
+
def update_project_counter_caches
+ return unless update_project_counter_caches?
+
Projects::OpenIssuesCountService.new(project).refresh_cache
end
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 7f73de67625..764bc08923e 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -942,7 +942,13 @@ class MergeRequest < ActiveRecord::Base
true
end
+ def update_project_counter_caches?
+ state_changed?
+ end
+
def update_project_counter_caches
+ return unless update_project_counter_caches?
+
Projects::OpenMergeRequestsCountService.new(target_project).refresh_cache
end
diff --git a/changelogs/unreleased/sidebar-cache-updates.yml b/changelogs/unreleased/sidebar-cache-updates.yml
new file mode 100644
index 00000000000..aebe53ba5b2
--- /dev/null
+++ b/changelogs/unreleased/sidebar-cache-updates.yml
@@ -0,0 +1,5 @@
+---
+title: Only update the sidebar count caches when needed
+merge_request:
+author:
+type: other
diff --git a/spec/models/issue_spec.rb b/spec/models/issue_spec.rb
index de86788d142..e547da0cfbe 100644
--- a/spec/models/issue_spec.rb
+++ b/spec/models/issue_spec.rb
@@ -769,4 +769,22 @@ describe Issue do
expect(described_class.public_only).to eq([public_issue])
end
end
+
+ describe '#update_project_counter_caches?' do
+ it 'returns true when the state changes' do
+ subject.state = 'closed'
+
+ expect(subject.update_project_counter_caches?).to eq(true)
+ end
+
+ it 'returns true when the confidential flag changes' do
+ subject.confidential = true
+
+ expect(subject.update_project_counter_caches?).to eq(true)
+ end
+
+ it 'returns false when the state or confidential flag did not change' do
+ expect(subject.update_project_counter_caches?).to eq(false)
+ end
+ end
end
diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb
index 92cf15a5a51..09f3b97ec58 100644
--- a/spec/models/merge_request_spec.rb
+++ b/spec/models/merge_request_spec.rb
@@ -1700,4 +1700,16 @@ describe MergeRequest do
.to change { project.open_merge_requests_count }.from(1).to(0)
end
end
+
+ describe '#update_project_counter_caches?' do
+ it 'returns true when the state changes' do
+ subject.state = 'closed'
+
+ expect(subject.update_project_counter_caches?).to eq(true)
+ end
+
+ it 'returns false when the state did not change' do
+ expect(subject.update_project_counter_caches?).to eq(false)
+ end
+ end
end