module Covercache
Motivation¶ ↑
-
Quickly use
Rails.cache
for some tasks without rubbish code in models. -
Don’t worrying about generation and storing cache keys, but sometimes will be able to adjust it.
-
Don’t be limited with indexes, queries or scopes.
Usage¶ ↑
Add covers_with_cache
in your model class and use define_cached
or covercache
helpers.
Wrapping with define_cached
¶ ↑
You can easily wrap methods with the define_cached
helper.<br /> Note, that Relation objects couldn’t be cached, so wrapped method or block should return an array or single instance of your model.
To wrap instance methods you should use a name of already defined method or set block with the first argument as record:
class Post < ActiveRecord::Base def all_comments comments.all end # Wrap instance method Post#all_comments to Post#cached_all_comments: define_cached :all_comments # You can add arguments like for covercache: [keys], debug: true, expires_in: 10.minutes define_cached :all_comments, expires_in: 1.minute define_cached :all_comments_authors, expires_in: 1.minute do |record| record.author end # ... end post = Post.find(1) post.cached_all_comments
To wrap class methods:
class_define_cached :for_post_ids, debug: true, expires_in: 10.minutes do |post_ids| where(post_id: post_ids).all end post_ids = (1..10).to_a Comments.cached_for_post_ids post_ids, cache_key: post_ids.hash
Constants
- VERSION
Public Class Methods
default_logger()
click to toggle source
# File lib/covercache.rb, line 65 def self.default_logger require 'logger' l = Logger.new(STDOUT) l.level = Logger::DEBUG l end
logger()
click to toggle source
General helper method (ex cache
helper in PackRat)
# File lib/covercache.rb, line 56 def self.logger @logger ||= rails_logger || default_logger end
logger=(logger)
click to toggle source
# File lib/covercache.rb, line 72 def self.logger=(logger) @logger = logger end
rails_logger()
click to toggle source
# File lib/covercache.rb, line 60 def self.rails_logger (defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger) || (defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER.respond_to?(:debug) && RAILS_DEFAULT_LOGGER) end
Public Instance Methods
covers_with_cache()
click to toggle source
module CoversWithCache add Covercache
supporting to model
# File lib/covercache.rb, line 265 def covers_with_cache caller_source = caller.first[/[^:]+/] class_eval do @covercache_caller_source = caller_source include Covercache::ModelConcern extend Covercache::DefiningHelper end end