Commit a6c0eb54 authored by Valery Sizov's avatar Valery Sizov

Invalidate cache for issue and MR counters more granularly

parent e261b4b8
...@@ -930,10 +930,18 @@ class User < ActiveRecord::Base ...@@ -930,10 +930,18 @@ class User < ActiveRecord::Base
end end
def invalidate_cache_counts def invalidate_cache_counts
Rails.cache.delete(['users', id, 'assigned_open_merge_requests_count']) invalidate_issue_cache_counts
invalidate_merge_request_cache_counts
end
def invalidate_issue_cache_counts
Rails.cache.delete(['users', id, 'assigned_open_issues_count']) Rails.cache.delete(['users', id, 'assigned_open_issues_count'])
end end
def invalidate_merge_request_cache_counts
Rails.cache.delete(['users', id, 'assigned_open_merge_requests_count'])
end
def todos_done_count(force: false) def todos_done_count(force: false)
Rails.cache.fetch(['users', id, 'todos_done_count'], force: force) do Rails.cache.fetch(['users', id, 'todos_done_count'], force: force) do
TodosFinder.new(self, state: :done).execute.count TodosFinder.new(self, state: :done).execute.count
......
...@@ -179,6 +179,7 @@ class IssuableBaseService < BaseService ...@@ -179,6 +179,7 @@ class IssuableBaseService < BaseService
issuable.create_cross_references!(current_user) issuable.create_cross_references!(current_user)
execute_hooks(issuable) execute_hooks(issuable)
issuable.assignees.each(&:invalidate_cache_counts) issuable.assignees.each(&:invalidate_cache_counts)
invalidate_cache_counts(issuable.assignees, issuable)
end end
issuable issuable
...@@ -237,7 +238,7 @@ class IssuableBaseService < BaseService ...@@ -237,7 +238,7 @@ class IssuableBaseService < BaseService
if old_assignees != issuable.assignees if old_assignees != issuable.assignees
assignees = old_assignees + issuable.assignees.to_a assignees = old_assignees + issuable.assignees.to_a
assignees.compact.each(&:invalidate_cache_counts) invalidate_cache_counts(assignees.compact, issuable)
end end
after_update(issuable) after_update(issuable)
...@@ -330,4 +331,10 @@ class IssuableBaseService < BaseService ...@@ -330,4 +331,10 @@ class IssuableBaseService < BaseService
create_labels_note(issuable, old_labels) if issuable.labels != old_labels create_labels_note(issuable, old_labels) if issuable.labels != old_labels
end end
def invalidate_cache_counts(users, issuable)
users.each do |user|
user.public_send("invalidate_#{issuable.model_name.singular}_cache_counts")
end
end
end end
---
title: Invalidate cache for issue and MR counters more granularly
merge_request:
author:
...@@ -1777,4 +1777,32 @@ describe User, models: true do ...@@ -1777,4 +1777,32 @@ describe User, models: true do
expect(user.preferred_language).to eq('en') expect(user.preferred_language).to eq('en')
end end
end end
context '#invalidate_issue_cache_counts' do
let(:user) { build_stubbed(:user) }
it 'invalidates cache for issue counter' do
cache_mock = double
expect(cache_mock).to receive(:delete).with(['users', user.id, 'assigned_open_issues_count'])
allow(Rails).to receive(:cache).and_return(cache_mock)
user.invalidate_issue_cache_counts
end
end
context '#invalidate_merge_request_cache_counts' do
let(:user) { build_stubbed(:user) }
it 'invalidates cache for Merge Request counter' do
cache_mock = double
expect(cache_mock).to receive(:delete).with(['users', user.id, 'assigned_open_merge_requests_count'])
allow(Rails).to receive(:cache).and_return(cache_mock)
user.invalidate_merge_request_cache_counts
end
end
end end
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