Module: CacheMachine::Cache::ClassMethods

Defined in:
lib/cache_machine/cache.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_cache_machine_for(*associations) ⇒ Object Also known as: cache_map

Initializes tracking associations to write and reset cache. associations parameter is to represent cache map with hash.

Examples

# Cache associated collections
acts_as_cache_machine_for :cats, :dogs

# Cache result of method to be expired when collection changes.
acts_as_cache_machine_for :cats => :cat_ids

# Cache and expire dependent collections (_mouse_ change invalidates all other collection caches by chain)
acts_as_cache_machine_for :mouses => :cats, :cats => [:dogs, :bears], :dogs, :bears


36
37
38
39
# File 'lib/cache_machine/cache.rb', line 36

def acts_as_cache_machine_for *associations
  include CacheMachine::Cache::Map
  cache_associated(associations)
end

#reset_timestamp(format = nil) ⇒ Object

Resets timestamp of class collection.



58
59
60
61
62
# File 'lib/cache_machine/cache.rb', line 58

def reset_timestamp format = nil
  cache_key = timestamp_key format
  CacheMachine::Logger.info "CACHE_MACHINE (reset_timestamp): deleting '#{cache_key}'."
  Rails.cache.delete(cache_key)
end

#reset_timestampsObject



64
65
66
# File 'lib/cache_machine/cache.rb', line 64

def reset_timestamps
  CacheMachine::Cache.formats.each { |format| reset_timestamp format }
end

#timestamp(format = nil) ⇒ Object

Returns timestamp of class collection.



43
44
45
# File 'lib/cache_machine/cache.rb', line 43

def timestamp format = nil
  Rails.cache.fetch(timestamp_key format) { Time.now.to_i.to_s }
end

#timestamp_key(format = nil) ⇒ Object

Returns cache key to fetch timestamp from memcached.



48
49
50
# File 'lib/cache_machine/cache.rb', line 48

def timestamp_key format = nil
  [self.name, format, 'timestamp'].join '_'
end

#timestamped_key(format = nil) ⇒ Object

Returns cache key of anything with timestamp attached.



53
54
55
# File 'lib/cache_machine/cache.rb', line 53

def timestamped_key format = nil
  [timestamp_key(format), timestamp(format)].join '_'
end