Method: Cachetastic::Cacheable#cache_self

Defined in:
lib/cachetastic/cacheable.rb

#cache_selfObject

Unless the object’s cachetastic_key method returns nil this method will store the object in the cache using the object’s cachetastic_key as the key. You MUST create an instance level method called cachetastic_key and have it return a valid key! If you return nil from the cachetastic_key method or you will not be able to use the cache_self and uncache_self methods.

Example:

class Person
  include Cachetastic::Cacheable
  attr_accessor :name
  def cachetastic_key
    self.name
  end
end

Person.get_from_cache("Mark Bates") # => nil
p = Person.new
p.name = "Mark Bates"
p.cache_self
Person.get_from_cache("Mark Bates") # => "Mark Bates"


147
148
149
150
# File 'lib/cachetastic/cacheable.rb', line 147

def cache_self
  cache_class.set(self.cachetastic_key, self) unless self.cachetastic_key.nil?
  return self
end