module RailsSimpleCaching::Caching::ClassMethods

Public Instance Methods

cache_expires_in(time) click to toggle source
# File lib/rails_simple_caching/caching/class_methods.rb, line 6
def cache_expires_in(time)
  @expire_time = time
end
cached_attributes() click to toggle source
# File lib/rails_simple_caching/caching/class_methods.rb, line 26
def cached_attributes
  @cached_attributes
end
caches(attribute, expires_in: expire_time) click to toggle source
# File lib/rails_simple_caching/caching/class_methods.rb, line 10
def caches(attribute, expires_in: expire_time)
  @cached_attributes ||= []
  @cached_attributes << attribute

  # `cache_key_with_version` generates a string based on the
  # model's class name, `id`, and `updated_at` attributes.
  # This is a common convention and has the benefit of invalidating
  # the cache whenever the product is updated.
  define_method("cached_#{attribute}") do
    key = "#{cache_key_with_version}/#{attribute}"
    Rails.cache.fetch(key, expires_in: expires_in) do
      update_cached(attribute)
    end
  end
end

Private Instance Methods

expire_time() click to toggle source
# File lib/rails_simple_caching/caching/class_methods.rb, line 32
def expire_time
  # The application requires the gem before initializing
  @expire_time || Rails&.configuration&.default_expire_time || 24.hours
end