feat: expiry for cache keys [CW-3038] (#8793)
* feat: set cache keys for 3 days only * feat: invalidate should set the latest timestamp * refactor: cache_keys concern * remove invalidate_cache method * refactor reset to set to new value instead of delete * ensure only one event is dispatched * feat: set expiry to 24 hours * chore: make expiry 48 hours * feat: include destroy event * feat: set expiry to 72 days * fix: typo * test: cache update after `touch` * test: update cache keys * refactor: remove after_touch, it's already handled in commit
This commit is contained in:
@@ -2,7 +2,7 @@ module AccountCacheRevalidator
|
|||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
included do
|
included do
|
||||||
after_commit :update_account_cache, on: [:create, :update]
|
after_commit :update_account_cache, on: [:create, :update, :destroy]
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_account_cache
|
def update_account_cache
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ module CacheKeys
|
|||||||
include CacheKeysHelper
|
include CacheKeysHelper
|
||||||
include Events::Types
|
include Events::Types
|
||||||
|
|
||||||
|
CACHE_KEYS_EXPIRY = 72.hours
|
||||||
|
|
||||||
included do
|
included do
|
||||||
class_attribute :cacheable_models
|
class_attribute :cacheable_models
|
||||||
self.cacheable_models = [Label, Inbox, Team]
|
self.cacheable_models = [Label, Inbox, Team]
|
||||||
@@ -18,26 +20,26 @@ module CacheKeys
|
|||||||
keys
|
keys
|
||||||
end
|
end
|
||||||
|
|
||||||
def invalidate_cache_key_for(key)
|
|
||||||
prefixed_cache_key = get_prefixed_cache_key(id, key)
|
|
||||||
Redis::Alfred.delete(prefixed_cache_key)
|
|
||||||
dispatch_cache_update_event
|
|
||||||
end
|
|
||||||
|
|
||||||
def update_cache_key(key)
|
def update_cache_key(key)
|
||||||
prefixed_cache_key = get_prefixed_cache_key(id, key)
|
update_cache_key_for_account(id, key)
|
||||||
Redis::Alfred.set(prefixed_cache_key, Time.now.utc.to_i)
|
|
||||||
dispatch_cache_update_event
|
dispatch_cache_update_event
|
||||||
end
|
end
|
||||||
|
|
||||||
def reset_cache_keys
|
def reset_cache_keys
|
||||||
self.class.cacheable_models.each do |model|
|
self.class.cacheable_models.each do |model|
|
||||||
invalidate_cache_key_for(model.name.underscore)
|
update_cache_key_for_account(id, model.name.underscore)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
dispatch_cache_update_event
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def update_cache_key_for_account(account_id, key)
|
||||||
|
prefixed_cache_key = get_prefixed_cache_key(account_id, key)
|
||||||
|
Redis::Alfred.setex(prefixed_cache_key, Time.now.utc.to_i, CACHE_KEYS_EXPIRY)
|
||||||
|
end
|
||||||
|
|
||||||
def dispatch_cache_update_event
|
def dispatch_cache_update_event
|
||||||
Rails.configuration.dispatcher.dispatch(ACCOUNT_CACHE_INVALIDATED, Time.zone.now, cache_keys: cache_keys, account: self)
|
Rails.configuration.dispatcher.dispatch(ACCOUNT_CACHE_INVALIDATED, Time.zone.now, cache_keys: cache_keys, account: self)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -43,10 +43,13 @@ RSpec.describe 'Super Admin accounts API', type: :request do
|
|||||||
it 'shows the list of accounts' do
|
it 'shows the list of accounts' do
|
||||||
expect(account.cache_keys.keys).to contain_exactly(:inbox, :label, :team)
|
expect(account.cache_keys.keys).to contain_exactly(:inbox, :label, :team)
|
||||||
sign_in(super_admin, scope: :super_admin)
|
sign_in(super_admin, scope: :super_admin)
|
||||||
|
|
||||||
|
now_timestamp = Time.now.utc.to_i
|
||||||
post "/super_admin/accounts/#{account.id}/reset_cache"
|
post "/super_admin/accounts/#{account.id}/reset_cache"
|
||||||
expect(response).to have_http_status(:redirect)
|
expect(response).to have_http_status(:redirect)
|
||||||
expect(flash[:notice]).to eq('Cache keys cleared')
|
expect(flash[:notice]).to eq('Cache keys cleared')
|
||||||
expect(account.reload.cache_keys.values.map(&:to_i)).to eq([0, 0, 0])
|
|
||||||
|
expect(account.reload.cache_keys.values.all? { |v| v.to_i == now_timestamp }).to be(true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ RSpec.describe CacheKeys do
|
|||||||
before do
|
before do
|
||||||
allow(Redis::Alfred).to receive(:delete)
|
allow(Redis::Alfred).to receive(:delete)
|
||||||
allow(Redis::Alfred).to receive(:set)
|
allow(Redis::Alfred).to receive(:set)
|
||||||
|
allow(Redis::Alfred).to receive(:setex)
|
||||||
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -27,28 +28,11 @@ RSpec.describe CacheKeys do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#invalidate_cache_key_for' do
|
|
||||||
it 'deletes the cache key' do
|
|
||||||
test_model.invalidate_cache_key_for('label')
|
|
||||||
expect(Redis::Alfred).to have_received(:delete).with('idb-cache-key-account-1-label')
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'dispatches a cache update event' do
|
|
||||||
test_model.invalidate_cache_key_for('label')
|
|
||||||
expect(Rails.configuration.dispatcher).to have_received(:dispatch).with(
|
|
||||||
CacheKeys::ACCOUNT_CACHE_INVALIDATED,
|
|
||||||
kind_of(ActiveSupport::TimeWithZone),
|
|
||||||
cache_keys: test_model.cache_keys,
|
|
||||||
account: test_model
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#update_cache_key' do
|
describe '#update_cache_key' do
|
||||||
it 'updates the cache key' do
|
it 'updates the cache key' do
|
||||||
allow(Time).to receive(:now).and_return(Time.parse('2023-05-29 00:00:00 UTC'))
|
allow(Time).to receive(:now).and_return(Time.parse('2023-05-29 00:00:00 UTC'))
|
||||||
test_model.update_cache_key('label')
|
test_model.update_cache_key('label')
|
||||||
expect(Redis::Alfred).to have_received(:set).with('idb-cache-key-account-1-label', Time.now.utc.to_i)
|
expect(Redis::Alfred).to have_received(:setex).with('idb-cache-key-account-1-label', kind_of(Integer), CacheKeys::CACHE_KEYS_EXPIRY)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'dispatches a cache update event' do
|
it 'dispatches a cache update event' do
|
||||||
@@ -66,8 +50,20 @@ RSpec.describe CacheKeys do
|
|||||||
it 'invalidates all cache keys for cacheable models' do
|
it 'invalidates all cache keys for cacheable models' do
|
||||||
test_model.reset_cache_keys
|
test_model.reset_cache_keys
|
||||||
test_model.class.cacheable_models.each do |model|
|
test_model.class.cacheable_models.each do |model|
|
||||||
expect(Redis::Alfred).to have_received(:delete).with("idb-cache-key-account-1-#{model.name.underscore}")
|
expect(Redis::Alfred).to have_received(:setex).with("idb-cache-key-account-1-#{model.name.underscore}", kind_of(Integer),
|
||||||
|
CacheKeys::CACHE_KEYS_EXPIRY)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'dispatches a cache update event' do
|
||||||
|
test_model.reset_cache_keys
|
||||||
|
|
||||||
|
expect(Rails.configuration.dispatcher).to have_received(:dispatch).with(
|
||||||
|
CacheKeys::ACCOUNT_CACHE_INVALIDATED,
|
||||||
|
kind_of(ActiveSupport::TimeWithZone),
|
||||||
|
cache_keys: test_model.cache_keys,
|
||||||
|
account: test_model
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -250,5 +250,15 @@ RSpec.describe Inbox do
|
|||||||
cache_keys: inbox.account.cache_keys
|
cache_keys: inbox.account.cache_keys
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'updates the cache key after update' do
|
||||||
|
expect(inbox.account).to receive(:update_cache_key).with('inbox')
|
||||||
|
inbox.update(name: 'New Name')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'updates the cache key after touch' do
|
||||||
|
expect(inbox.account).to receive(:update_cache_key).with('inbox')
|
||||||
|
inbox.touch # rubocop:disable Rails/SkipsModelValidations
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user