module ActAsCached::Mixin::ClassMethods

Public Instance Methods

act_as_cached(*args) click to toggle source

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.

# 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.cache_options = 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() click to toggle source
# File lib/act_as_cached/mixin.rb, line 58
def cache_mod
  @cache_mod
end
enabled_cache?() click to toggle source
# File lib/act_as_cached/mixin.rb, line 62
def enabled_cache?
  !cache_mod.nil?
end
observer() click to toggle source
# File lib/act_as_cached/mixin.rb, line 66
def observer
  ActAsCached::ActiveRecord::Actobserver.instance
end