Module: AutoClearCache

Extended by:
ActiveSupport::Concern
Defined in:
lib/app/models/concerns/auto_clear_cache.rb

Overview

Clear the Rails.cache when this object is saved. This prevents a number of caching issues over time where we've put it in cache, but the update doesn't appear until that cache expires.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Add the call backs to the class being included.



14
15
16
17
18
19
20
# File 'lib/app/models/concerns/auto_clear_cache.rb', line 14

def self.included(base)
  base.class_eval do
    # Add the call back to the class
    after_save :clear_cache
    before_destroy :clear_cache
  end
end

Instance Method Details

#clear_cache ⇒ Object

Clear the cache



25
26
27
28
29
30
31
32
33
# File 'lib/app/models/concerns/auto_clear_cache.rb', line 25

def clear_cache
  Rails.cache.delete_matched "*#{id}*"
  return unless respond_to?(:account) && .present? && .is_a?()

  Rails.cache.delete_matched "*#{account.id}*"
  true # Force a return of true so that we don't break the callback chain.
rescue StandardError
  false
end