Module: Cachet::Cacheable::ClassMethods

Defined in:
lib/cachet/cacheable.rb

Instance Method Summary collapse

Instance Method Details

#cache_invalidator(cached_method, entity) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cachet/cacheable.rb', line 20

def cache_invalidator (cached_method, entity)
  define_method("#{cached_method}_with_cache_invalidation") { |*args|
    cache_key = block_given? ? yield(*args) : cached_method.to_s
    method("#{cached_method}_without_cache_invalidation".to_sym).call(*args)
    (entity.kind_of?(Array) ? entity : [entity]).each do |ent|
      Cachet.invalidate(ent, cache_key)
    end
  }
  alias_method_chain "#{cached_method}".to_sym, :cache_invalidation
  Cachet.logger.debug "Aliasing method : #{cached_method} to invalidate the #{entity} entities"
end

#cacheable(cached_method, entity) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/cachet/cacheable.rb', line 9

def cacheable (cached_method, entity)
  define_method("#{cached_method}_with_cache") { |*args|
    cache_key = block_given? ? yield(*args) : cached_method.to_s
    Cachet.cache(entity, cache_key) do
      method("#{cached_method}_without_cache".to_sym).call(*args)
    end
  }
  alias_method_chain "#{cached_method}".to_sym, :cache
  Cachet.logger.debug "Aliasing method : #{cached_method} to cache the result as #{entity} entities"
end