Module: Caching::ClassMethods

Defined in:
lib/caching.rb

Instance Method Summary collapse

Instance Method Details

#cache_method(method) ⇒ Object



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

def cache_method(method)
  alias_method "#{method}_without_cache", method

  define_method method do |*args, &block|
    args_key = args.any? ? "_#{Base64.encode64(Marshal.dump(args))}" : ''
    method_key = "#{method}#{args_key}"
    cached_methods_keys[method] << method_key
    cache_storage.fetch(method_key) { send "#{method}_without_cache", *args, &block }
  end
end