Module: CacheMachine::Cache::Map::InstanceMethods

Defined in:
lib/cache_machine/cache/map.rb

Instance Method Summary collapse

Instance Method Details

#cache_key_of(_member, options = {}) ⇒ Object

Returns cache key of _member. TODO: describe options.



109
110
111
# File 'lib/cache_machine/cache/map.rb', line 109

def cache_key_of _member, options = {}
  [self.class.name, self.to_param, _member, options[:format], options[:page] || 1].compact.join '_'
end

#delete_all_cachesObject

Removes all caches using map.



142
143
144
145
146
# File 'lib/cache_machine/cache/map.rb', line 142

def delete_all_caches
  self.class.cache_map.keys.each do |cached_collection|
    delete_cache_of cached_collection
  end
end

#delete_cache_of(_member) ⇒ Object

Recursively deletes cache by map for _member.



149
150
151
152
153
154
# File 'lib/cache_machine/cache/map.rb', line 149

def delete_cache_of _member
  delete_cache_of_only _member
  if chain = self.class.cache_map[_member]
    [*chain].each &method(:delete_cache_of)
  end
end

#delete_cache_of_only(_member) ⇒ Object

Deletes cache of only _member ignoring cache map.



157
158
159
160
161
162
163
164
165
# File 'lib/cache_machine/cache/map.rb', line 157

def delete_cache_of_only _member
  CacheMachine::Cache.formats.each do |cache_format|
    page_nr = 0; begin
      cache_key = cache_key_of(_member, {:format => cache_format, :page => page_nr += 1})
      CacheMachine::Logger.info "CACHE_MACHINE (delete_cache_of_only): deleting '#{cache_key}'"
    end while Rails.cache.delete(cache_key)
  end
  reset_timestamp_of _member
end

#fetch_cache_of(_member, options = {}, &block) ⇒ Object

Fetches cache of _member from cache map. TODO: Describe options. TODO: Describe timestamp features (we can pass methods or fields as timestamps too).

Or we can use define_timestamp +:expires_in => 20.hours+.


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cache_machine/cache/map.rb', line 117

def fetch_cache_of _member, options = {}, &block
  cache_key = if timestamp = options[:timestamp]
    # Make key dependent on collection timestamp and optional timestamp.
    [timestamped_key_of(_member, options), instance_eval(&timestamp)].join '_'
  else
    cache_key_of(_member, options)
  end

  expires_in = if expires_at = options[:expires_at]
    expires_at = expires_at.call if expires_at.kind_of? Proc

    if expires_at.kind_of? Time
      expires_at - Time.now
    else
      raise ArgumentError, "expires_at is not a Time"
    end
  else
    options[:expires_in]
  end

  CacheMachine::Logger.info "CACHE_MACHINE (fetch_cache_of): reading '#{cache_key}'."
  Rails.cache.fetch(cache_key, :expires_in => expires_in, &block)
end

#reset_timestamp_of(anything) ⇒ Object

Deletes cache of anything from memory.



185
186
187
188
189
# File 'lib/cache_machine/cache/map.rb', line 185

def reset_timestamp_of anything
  cache_key = timestamp_key_of anything
  CacheMachine::Logger.info "CACHE_MACHINE (reset_timestamp_of): deleting '#{cache_key}'."
  Rails.cache.delete(cache_key)
end

#timestamp_key_of(anything) ⇒ Object

Returns timestamp cache key for anything.



168
169
170
# File 'lib/cache_machine/cache/map.rb', line 168

def timestamp_key_of anything
  [self.class.name, self.to_param, anything, 'timestamp'].join '_'
end

#timestamp_of(anything) ⇒ Object

Returns timestamp of anything from memcached.



173
174
175
176
177
# File 'lib/cache_machine/cache/map.rb', line 173

def timestamp_of anything
  key = timestamp_key_of anything
  CacheMachine::Logger.info "CACHE_MACHINE (timestamp_of): reading timestamp '#{key}'."
  Rails.cache.fetch(key) { Time.now.to_i.to_s }
end

#timestamped_key_of(anything, options = {}) ⇒ Object

Returns cache key of anything with timestamp attached.



180
181
182
# File 'lib/cache_machine/cache/map.rb', line 180

def timestamped_key_of anything, options = {}
  [cache_key_of(anything, options), timestamp_of(anything)].join '_'
end