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 = {}) ⇒ String

Returns cache key of the member.

Parameters:

  • _member (Symbol)
  • options (Hash) (defaults to: {})

Returns:

  • (String)


136
137
138
139
140
141
142
143
144
145
# File 'lib/cache_machine/cache/map.rb', line 136

def cache_key_of _member, options = {}
  timestamp = instance_eval(&options[:timestamp]) if options.has_key? :timestamp

  [ self.class.name,
    self.to_param,
    _member,
    options[:format],
    options[:page] || 1,
    timestamp ].flatten.compact.join '/'
end

#delete_all_cachesObject

Removes all caches using map.



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

def delete_all_caches
  self.class.cache_map.to_a.flatten.uniq.each &method(:delete_cache_of)
end

#delete_cache_of(_member) ⇒ Object

Recursively deletes cache by map starting from the member.

Parameters:

  • _member (Symbol)


188
189
190
191
192
193
# File 'lib/cache_machine/cache/map.rb', line 188

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 the only member ignoring cache map.

Parameters:

  • _member (Symbol)


198
199
200
201
202
203
204
205
206
# File 'lib/cache_machine/cache/map.rb', line 198

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) ⇒ *

Fetches cache of the member.

Examples:

Fetch cache of associated collection to be refreshed every hour.

@instance.fetch_cache_of :association, :timestamp => lambda { custom_instance_method },
                                       :expires_in => 1.hour

Parameters:

  • _member (Symbol)
  • options (Hash) (defaults to: {})

Returns:

  • (*)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/cache_machine/cache/map.rb', line 157

def fetch_cache_of _member, options = {}, &block
  if CacheMachine::Cache::enabled?
    expires_in = if expires_at = options[:expires_at]
      if expires_at.kind_of? Proc
        expires_at = expires_at.call
      end

      if expires_at.kind_of? Time
        expires_at - Time.zone.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_of(_member, options), :expires_in => expires_in, &block)
  else
    yield
  end
end

#reset_timestamp_of(anything) ⇒ Object

Deletes cache of anything from memory.



240
241
242
243
244
# File 'lib/cache_machine/cache/map.rb', line 240

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) ⇒ String

Returns timestamp cache key for anything.

Parameters:

  • anything (String, Symbol)

Returns:

  • (String)


213
214
215
# File 'lib/cache_machine/cache/map.rb', line 213

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

#timestamp_of(anything) ⇒ String

Returns timestamp of anything from memcached.

Parameters:

  • anything (String, Symbol)

Returns:

  • (String)


222
223
224
225
226
227
228
229
230
# File 'lib/cache_machine/cache/map.rb', line 222

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

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

Returns cache key of anything with timestamp attached.

Returns:

  • (String)


235
236
237
# File 'lib/cache_machine/cache/map.rb', line 235

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