Commit d0db4080 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'counters_cache_invalidation' into 'master'

Invalidate cache for issue and MR counters separately

See merge request !11394
parents 42814500 a6c0eb54
......@@ -930,10 +930,18 @@ class User < ActiveRecord::Base
end
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'])
end
def invalidate_merge_request_cache_counts
Rails.cache.delete(['users', id, 'assigned_open_merge_requests_count'])
end
def todos_done_count(force: false)
Rails.cache.fetch(['users', id, 'todos_done_count'], force: force) do
TodosFinder.new(self, state: :done).execute.count
......
......@@ -179,6 +179,7 @@ class IssuableBaseService < BaseService
issuable.create_cross_references!(current_user)
execute_hooks(issuable)
issuable.assignees.each(&:invalidate_cache_counts)
invalidate_cache_counts(issuable.assignees, issuable)
end
issuable
......@@ -237,7 +238,7 @@ class IssuableBaseService < BaseService
if old_assignees != issuable.assignees
assignees = old_assignees + issuable.assignees.to_a
assignees.compact.each(&:invalidate_cache_counts)
invalidate_cache_counts(assignees.compact, issuable)
end
after_update(issuable)
......@@ -330,4 +331,10 @@ class IssuableBaseService < BaseService
create_labels_note(issuable, old_labels) if issuable.labels != old_labels
end
def invalidate_cache_counts(users, issuable)
users.each do |user|
user.public_send("invalidate_#{issuable.model_name.singular}_cache_counts")
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
expect(user.preferred_language).to eq('en')
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
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