Module: TinyCache::ActiveRecord::Base::ClassMethods
- Defined in:
- lib/tiny_cache/active_record/base.rb
Instance Attribute Summary collapse
-
#tiny_cache_options ⇒ Object
readonly
Returns the value of attribute tiny_cache_options.
Instance Method Summary collapse
- #acts_as_tiny_cached(*args) ⇒ Object
- #cache_store ⇒ Object
- #expire_tiny_cache(id) ⇒ Object
- #logger ⇒ Object
- #read_tiny_cache(id) ⇒ Object
-
#tiny_cache_enabled? ⇒ Boolean
是否启用cache.
- #tiny_cache_key(id) ⇒ Object
- #tiny_cache_key_prefix ⇒ Object
- #tiny_cache_version ⇒ Object
- #update_counters_with_tiny_cache(id, counters) ⇒ Object
-
#without_tiny_cache ⇒ Object
不启用cache.
Instance Attribute Details
#tiny_cache_options ⇒ Object (readonly)
Returns the value of attribute tiny_cache_options.
20 21 22 |
# File 'lib/tiny_cache/active_record/base.rb', line 20 def end |
Instance Method Details
#acts_as_tiny_cached(*args) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/tiny_cache/active_record/base.rb', line 22 def acts_as_tiny_cached(*args) = args. @tiny_cache_enabled = true = [:expires_in] ||= 1.week [:version] ||= 0 after_commit :expire_tiny_cache, :on => :destroy after_commit :update_tiny_cache, :on => :update after_commit :write_tiny_cache, :on => :create end |
#cache_store ⇒ Object
55 56 57 |
# File 'lib/tiny_cache/active_record/base.rb', line 55 def cache_store ::TinyCache::Config.cache_store end |
#expire_tiny_cache(id) ⇒ Object
79 80 81 |
# File 'lib/tiny_cache/active_record/base.rb', line 79 def expire_tiny_cache(id) TinyCache.cache_store.delete(tiny_cache_key(id)) if self.tiny_cache_enabled? end |
#logger ⇒ Object
59 60 61 |
# File 'lib/tiny_cache/active_record/base.rb', line 59 def logger ::TinyCache::Config.logger end |
#read_tiny_cache(id) ⇒ Object
75 76 77 |
# File 'lib/tiny_cache/active_record/base.rb', line 75 def read_tiny_cache(id) RecordMarshal.load(TinyCache.cache_store.read(tiny_cache_key(id))) if self.tiny_cache_enabled? end |
#tiny_cache_enabled? ⇒ Boolean
是否启用cache
42 43 44 |
# File 'lib/tiny_cache/active_record/base.rb', line 42 def tiny_cache_enabled? !!@tiny_cache_enabled end |
#tiny_cache_key(id) ⇒ Object
71 72 73 |
# File 'lib/tiny_cache/active_record/base.rb', line 71 def tiny_cache_key(id) "#{tiny_cache_key_prefix}/models/#{self.name}/#{id}/#{tiny_cache_version}" end |
#tiny_cache_key_prefix ⇒ Object
63 64 65 |
# File 'lib/tiny_cache/active_record/base.rb', line 63 def tiny_cache_key_prefix ::TinyCache::Config.cache_key_prefix end |
#tiny_cache_version ⇒ Object
67 68 69 |
# File 'lib/tiny_cache/active_record/base.rb', line 67 def tiny_cache_version [:version] end |
#update_counters_with_tiny_cache(id, counters) ⇒ Object
35 36 37 38 39 |
# File 'lib/tiny_cache/active_record/base.rb', line 35 def update_counters_with_tiny_cache(id, counters) update_counters_without_tiny_cache(id, counters).tap do Array(id).each{|i| expire_tiny_cache(i)} end end |
#without_tiny_cache ⇒ Object
不启用cache
47 48 49 50 51 52 53 |
# File 'lib/tiny_cache/active_record/base.rb', line 47 def without_tiny_cache old, @tiny_cache_enabled = @tiny_cache_enabled, false yield if block_given? ensure @tiny_cache_enabled = old end |