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.3"
Instance Method Summary collapse
-
#covers_with_cache ⇒ Object
module CoversWithCache add Covercache supporting to model.
Instance Method Details
#covers_with_cache ⇒ Object
module CoversWithCache add Covercache supporting to model
216 217 218 219 220 221 222 223 224 |
# File 'lib/covercache.rb', line 216 def covers_with_cache caller_source = caller.first[/[^:]+/] class_eval do @covercache_caller_source = caller_source include Covercache::ModelConcern extend Covercache::DefiningHelper end end |