module Covercache::Base

Private Instance Methods

covercache(*keys, &block) click to toggle source

Arguments:

[1..]              any count of custom *keys
:debug             #=>
:without_auto_key  #=> don't generate cache_key
 any option for Rails.cache.fetch

Example:

scope :published, -> { where published: true }
has_many :comments

def cached_comments
  covercache do
    comments.all
  end
end

def self.cached_published
  covercache debug:true do
    published.all
  end
end
# File lib/covercache.rb, line 102
def covercache(*keys, &block)
  props = keys.extract_options!
  options = props.slice! :debug, :without_auto_key
  
  keys.prepend get_auto_cache_key(caller) unless props.fetch(:without_auto_key){false}
  keys.flatten!.compact!

  Covercache.logger.debug %([covercache] #{get_class_name} class generate cache key: #{keys.inspect}) if props.fetch(:debug){false}
  
  Rails.cache.fetch keys, options do
    push_covercache_key keys.join('/')
    block.call
  end
end
extract_cache_key(*args) click to toggle source
# File lib/covercache.rb, line 132
def extract_cache_key(*args)
  [*(args.last.delete :cache_key if args.last.is_a?(Hash))]
end
get_auto_cache_key(_caller) click to toggle source
# File lib/covercache.rb, line 121
def get_auto_cache_key(_caller)
  caller_method = _caller.map {|c| c[/`([^']*)'/, 1] }.detect {|m| !m.start_with?('block') }
  keys = [get_class_name, covercache_model_digest, caller_method]
  keys << cache_key if respond_to?(:cache_key)
  keys
end
get_class_name() click to toggle source
# File lib/covercache.rb, line 128
def get_class_name
  self.instance_of?(Class) ? name : self.class.name
end
push_covercache_key(key) click to toggle source
# File lib/covercache.rb, line 117
def push_covercache_key(key)
  self.covercache_keys = (covercache_keys << key).uniq
end