Module: Ultracache::Cached

Extended by:
ActiveSupport::Concern
Includes:
Macro
Defined in:
lib/ultracache/cached.rb

Overview

This class should be included to model classes related to cache. It adds primitive methods, attributes and callbacks required for caching.

Example:

class Person < ActiveRecord::Base
  include Ultracache::Cached

  # Cache definitions ...
end

Including this class adds a Relationships object which is stored into Ultracache::Configurations. Every relationship added to the class is represented by a Relationship object inside Relationships object regarding the class.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroy_cacheObject

Remove all cache related to the object from the storage. ‘destroy_cache` is registered as `after_destroy` callback for ActiveModel classes which mixin `Ultracache::Cached`.



67
68
69
70
71
# File 'lib/ultracache/cached.rb', line 67

def destroy_cache
  self.class.relationships.each do |name, relationship|
    relationship.destroy_cache(self)
  end
end

#read_cache(name, options = {}) ⇒ Object

Read cache from storage. Cache to read is specified by the ‘name` parameter. Relationship object should exist in Relationships related to the class.



39
40
41
42
# File 'lib/ultracache/cached.rb', line 39

def read_cache(name, options={})
  relationship = self.class.relationships.find(name.to_sym)
  strings = relationship.read_cache(self, options)
end

#save_cache(options = {}) ⇒ Object

Save caches of all relationships assigned to the class. If ‘:only` option is provided, only caches specified in the option are saved.

Example

p = Person.find params[:id]
p.save_cache # Saves all caches related to Person class
p.save_cache :only => [:cached_name] # Saves cached_name only

‘save_cache` is registered as `after_save` callback for ActiveModel classes which mixin `Ultracache::Cached`.



55
56
57
58
59
60
61
62
# File 'lib/ultracache/cached.rb', line 55

def save_cache(options = {})
  target = self.class.relationships.keys
  target &= options[:only] if options[:only]

  target.each do |name|
    self.class.relationships[name].save_cache(self)
  end
end

#update_cache(options = {}) ⇒ Object

Updates all cache related to the class, like ‘save_cache` does. `:only` option also can be used for this method. This method is registered as `after_update` callback.



76
77
78
79
80
81
82
83
# File 'lib/ultracache/cached.rb', line 76

def update_cache(options = {})
  target = self.class.relationships.keys
  target &= options[:only] if options[:only]

  target.each do |name|
    self.class.relationships[name].update_cache(self)
  end
end