Module: ActAsCached::Mixin::ClassMethods

Defined in:
lib/act_as_cached/mixin.rb

Instance Method Summary collapse

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.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/act_as_cached/mixin.rb', line 43

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_modObject



57
58
59
# File 'lib/act_as_cached/mixin.rb', line 57

def cache_mod
  @cache_mod
end

#enabled_cache?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/act_as_cached/mixin.rb', line 61

def enabled_cache?
  !cache_mod.nil?
end

#observerObject



65
66
67
# File 'lib/act_as_cached/mixin.rb', line 65

def observer
  ActAsCached::ActiveRecord::Actobserver.instance
end