Module: RailsSimpleCaching::Caching::ClassMethods

Defined in:
lib/rails_simple_caching/caching/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#cache_expires_in(time) ⇒ Object



6
7
8
# File 'lib/rails_simple_caching/caching/class_methods.rb', line 6

def cache_expires_in(time)
  @expire_time = time
end

#cached_attributesObject



26
27
28
# File 'lib/rails_simple_caching/caching/class_methods.rb', line 26

def cached_attributes
  @cached_attributes
end

#caches(attribute, expires_in: expire_time) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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