fix sub account cache invalidation

Change-Id: Icb7f6ef46f1c913f3e0a0018e43ba3a59c12012a
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/250387
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Rob Orton <rob@instructure.com>
QA-Review: Cody Cutrer <cody@instructure.com>
Product-Review: Cody Cutrer <cody@instructure.com>
This commit is contained in:
Cody Cutrer 2020-10-18 22:05:39 -06:00
parent df951e5689
commit a2059d44fe
3 changed files with 24 additions and 8 deletions

View File

@ -732,13 +732,13 @@ class Account < ActiveRecord::Base
end
def invalidate_caches_if_changed
if changed?
connection.after_transaction_commit do
if root_account?
Account.invalidate_cache(id)
else
shard.activate do
Rails.cache.delete(["account"/ id].cache_key)
if saved_changes?
shard.activate do
self.class.connection.after_transaction_commit do
if root_account?
Account.invalidate_cache(id)
else
Rails.cache.delete(["account2", id].cache_key)
end
end
end

View File

@ -20,7 +20,7 @@ module Canvas
module AccountCacher
class CacheAccountOnAssociation < ActiveRecord::Associations::BelongsToAssociation
def find_target
key = ["account", owner._read_attribute(reflection.foreign_key)].cache_key
key = ["account2", owner._read_attribute(reflection.foreign_key)].cache_key
return RequestCache.cache([Switchman::Shard.current.id, key].cache_key) { Rails.cache.fetch(key) { super } }
end
end

View File

@ -2116,4 +2116,20 @@ describe Account do
).to eq %w[AccountAdmin AssistantGrader DesignerEnrollment TeacherEnrollment]
end
end
describe '#invalidate_caches_if_changed' do
it 'works for root accounts' do
Account.default.name = 'Something new'
expect(Account).to receive(:invalidate_cache).with(Account.default.id).at_least(1)
Account.default.save!
end
it 'works for sub accounts' do
a = Account.default.manually_created_courses_account
a.name = 'something else'
expect(Rails.cache).to receive(:delete).with("short_name_lookup/account_#{a.id}").ordered
expect(Rails.cache).to receive(:delete).with(["account2", a.id].cache_key).ordered
a.save!
end
end
end