Module Cachetastic::Cacheable
In: lib/cachetastic.rb
lib/cachetastic_cacheable.rb

Include this module into an Object to achieve simplistic Object level caching. When including this module 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 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

    def always_the_same(x, y)
      cacher("always_the_same") do
        x + y
      end
    end

  end

Methods

Classes and Modules

Module Cachetastic::Cacheable::ClassAndInstanceMethods
Module Cachetastic::Cacheable::ClassOnlyMethods

Public Instance methods

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 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"

[Source]

     # File lib/cachetastic_cacheable.rb, line 143
143:     def cache_self
144:       cache_class.set(self.cachetastic_key, self) unless self.cachetastic_key.nil?
145:     end

Unless the object‘s cachetastic_key method returns nil this method will delete 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 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"
  p.uncache_self
  Person.get_from_cache("Mark Bates") # => nil

[Source]

     # File lib/cachetastic_cacheable.rb, line 169
169:     def uncache_self
170:       cache_class.delete(self.cachetastic_key) unless self.cachetastic_key.nil?
171:     end

[Validate]