Module: CacheMachine::Cache::ClassMethods
- Defined in:
- lib/cache_machine/cache.rb
Instance Method Summary collapse
-
#acts_as_cache_machine_for(*associations) ⇒ Object
(also: #cache_map)
Initializes tracking associations to write and reset cache.
-
#reset_timestamp(format = nil) ⇒ Object
Resets timestamp of class collection.
- #reset_timestamps ⇒ Object
-
#timestamp(format = nil) ⇒ Object
Returns timestamp of class collection.
-
#timestamp_key(format = nil) ⇒ Object
Returns cache key to fetch timestamp from memcached.
-
#timestamped_key(format = nil) ⇒ Object
Returns cache key of
anything
with timestamp attached.
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 format = nil cache_key = format CacheMachine::Logger.info "CACHE_MACHINE (reset_timestamp): deleting '#{cache_key}'." Rails.cache.delete(cache_key) end |
#reset_timestamps ⇒ Object
64 65 66 |
# File 'lib/cache_machine/cache.rb', line 64 def CacheMachine::Cache.formats.each { |format| format } end |
#timestamp(format = nil) ⇒ Object
Returns timestamp of class collection.
43 44 45 |
# File 'lib/cache_machine/cache.rb', line 43 def format = nil Rails.cache.fetch( 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 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 format = nil [(format), (format)].join '_' end |