module ForgetMeNot::Cacheable::ClassMethods
Public Instance Methods
cache_results(*methods)
click to toggle source
# File lib/forget-me-not/cacheable.rb, line 17 def cache_results(*methods) options = methods.last.is_a?(Hash) ? methods.pop : {} methods.each { |m| cache_method(m, options) } end
cache_warm(*args)
click to toggle source
# File lib/forget-me-not/cacheable.rb, line 22 def cache_warm(*args) # Classes can call the methods necessary to warm the cache end
Private Instance Methods
cache_method(method_name, options)
click to toggle source
# File lib/forget-me-not/cacheable.rb, line 27 def cache_method(method_name, options) method = instance_method(method_name) visibility = method_visibility(method_name) define_cache_method(method, options) send(visibility, method_name) end
define_cache_method(method, options)
click to toggle source
# File lib/forget-me-not/cacheable.rb, line 34 def define_cache_method(method, options) method_name = method.name.to_sym key_prefix = "/cached_method_result/#{self.name}" instance_key = get_instance_key_proc(options[:include]) if options.has_key?(:include) undef_method(method_name) define_method(method_name) do |*args, &block| raise 'Cannot pass blocks to cached methods' if block cache_key = [ key_prefix, (instance_key && instance_key.call(self)), method_name, args.to_s, ].compact.join '/' cache_key_hash = Digest::SHA1.hexdigest(cache_key) cache_hit = true result = Cacheable.cache_fetch(cache_key_hash) do cache_hit = false method.bind(self).call(*args) end if Cacheable.log_activity Cacheable.logger.info "Cache #{cache_hit ? 'hit' : 'miss'} for #{cache_key} (#{cache_key_hash})" end result end end
get_instance_key_proc(instance_key_methods)
click to toggle source
# File lib/forget-me-not/cacheable.rb, line 66 def get_instance_key_proc(instance_key_methods) instance_keys = Array.new(instance_key_methods).flatten Proc.new do |instance| instance_keys.map { |key| instance.send(key) } end end
method_visibility(method)
click to toggle source
# File lib/forget-me-not/cacheable.rb, line 73 def method_visibility(method) if private_method_defined?(method) :private elsif protected_method_defined?(method) :protected else :public end end