Module: Covercache
- Defined in:
- lib/covercache.rb,
lib/covercache/version.rb
Overview
Motivation
-
Quickly use
Rails.cachefor 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.
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
Defined Under Namespace
Modules: Base, DefiningHelper, ModelConcern
Constant Summary collapse
- VERSION =
"0.5.0"
Class Method Summary collapse
- .default_logger ⇒ Object
-
.logger ⇒ Object
General helper method (ex
cachehelper in PackRat). - .logger=(logger) ⇒ Object
- .rails_logger ⇒ Object
Instance Method Summary collapse
-
#covers_with_cache ⇒ Object
module CoversWithCache add Covercache supporting to model.
Class Method Details
.default_logger ⇒ Object
65 66 67 68 69 70 |
# File 'lib/covercache.rb', line 65 def self.default_logger require 'logger' l = Logger.new(STDOUT) l.level = Logger::DEBUG l end |
.logger ⇒ Object
General helper method (ex cache helper in PackRat)
56 57 58 |
# File 'lib/covercache.rb', line 56 def self.logger @logger ||= rails_logger || default_logger end |
.logger=(logger) ⇒ Object
72 73 74 |
# File 'lib/covercache.rb', line 72 def self.logger=(logger) @logger = logger end |
.rails_logger ⇒ Object
60 61 62 63 |
# 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 |
Instance Method Details
#covers_with_cache ⇒ Object
module CoversWithCache add Covercache supporting to model
265 266 267 268 269 270 271 272 273 |
# 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 |