Module: ActAsCached::Mixin::ClassMethods
- Defined in:
- lib/act_as_cached/mixin.rb
Instance Method Summary collapse
-
#act_as_cached(*args) ⇒ Object
Enable cache, in the current class.
- #cache_mod ⇒ Object
- #enabled_cache? ⇒ Boolean
- #observer ⇒ Object
Instance Method Details
#act_as_cached(*args) ⇒ Object
Enable cache, in the current class.
Example
class Topic < ActiveRecord::Base
act_as_cached expires_time: 1.day
has_many :comments
belongs_to :user
end Topic.find(1)
Topic Load (0.5ms) SELECT `topics`.* FROM `topics` WHERE `topics`.`id` = 1 LIMIT 1
> #<Topic id: 1 title: ‘foo’>
Topic.find(1)
> #<Topic id: 1 title: ‘foo’>
Topic.find(1).comments
Comment Load (1.1ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`topic_id` = 1
> [#<Comment id: 1, content: ‘xxxx’>,#<Comment id: 2, content: “ooooooo”>]
Topic.find(1).comments
> [#<Comment id: 1, content: ‘xxxx’>,#<Comment id: 2, content: “ooooooo”>]
Topic.find(1).save Topic.find(1)
Topic Load (0.5ms) SELECT `topics`.* FROM `topics` WHERE `topics`.`id` = 1 LIMIT 1
> #<Topic id: 1 title: ‘foo’>
Topic.find(1)
> #<Topic id: 1 title: ‘foo’>
Options
- :prefix
-
Namespace with act_as_cached, default is
aac
- :cache_store
-
Storage engine with act_as_cached
- :logger
-
Logger with act_as_cached
- :expires_time
-
Set cache expires time with activerecord associations and
ActiveRecord::Query#find
of current class.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/act_as_cached/mixin.rb', line 44 def act_as_cached(*args) observer.send(:add_observer!,klass = self) @cache_mod = Module.new do mattr_accessor :cache_options,:klass self. = args self.klass = klass extend ActAsCached::Helpers end @cache_mod.send(:include,ActAsCached::CacheOptions) delegate :options,:name,:prefix,:store,:logger,:time, to: :cache_mod , prefix: "cache" delegate :write_cache,:read_cache,:fetch_cache,to: :cache_mod end |
#cache_mod ⇒ Object
58 59 60 |
# File 'lib/act_as_cached/mixin.rb', line 58 def cache_mod @cache_mod end |
#enabled_cache? ⇒ Boolean
62 63 64 |
# File 'lib/act_as_cached/mixin.rb', line 62 def enabled_cache? !cache_mod.nil? end |
#observer ⇒ Object
66 67 68 |
# File 'lib/act_as_cached/mixin.rb', line 66 def observer ActAsCached::ActiveRecord::Actobserver.instance end |