52 lines
1.5 KiB
Ruby
52 lines
1.5 KiB
Ruby
module CacheKeys
|
|
extend ActiveSupport::Concern
|
|
|
|
include CacheKeysHelper
|
|
include Events::Types
|
|
|
|
# Self-healing bound: if a write path ever changes cached data without
|
|
# bumping its key, expiry forces the sentinel and every client refetches.
|
|
# 7 days caps that staleness while sparing quiet models (labels, teams)
|
|
# from a spurious full refetch after every idle weekend.
|
|
CACHE_KEYS_EXPIRY = 7.days
|
|
|
|
included do
|
|
class_attribute :cacheable_models
|
|
self.cacheable_models = [Label, Inbox, Team, CannedResponse, AccountUser, CustomAttributeDefinition]
|
|
end
|
|
|
|
def cache_keys
|
|
keys = {}
|
|
self.class.cacheable_models.each do |model|
|
|
keys[model.name.underscore.to_sym] = fetch_value_for_key(id, model.name.underscore)
|
|
end
|
|
|
|
keys
|
|
end
|
|
|
|
def update_cache_key(key)
|
|
update_cache_key_for_account(id, key)
|
|
dispatch_cache_update_event
|
|
end
|
|
|
|
def reset_cache_keys
|
|
self.class.cacheable_models.each do |model|
|
|
update_cache_key_for_account(id, model.name.underscore)
|
|
end
|
|
|
|
::Conversations::UnreadCounts::Store.clear_account!(id)
|
|
dispatch_cache_update_event
|
|
end
|
|
|
|
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
|
|
Rails.configuration.dispatcher.dispatch(ACCOUNT_CACHE_INVALIDATED, Time.zone.now, cache_keys: cache_keys, account: self)
|
|
end
|
|
end
|