Class: CacheIt::InstanceDelegate
- Inherits:
-
Object
- Object
- CacheIt::InstanceDelegate
- Defined in:
- lib/cache_it.rb
Instance Method Summary collapse
- #delete ⇒ Object
- #increment(counter, amount = 1) ⇒ Object
- #init_counters ⇒ Object
-
#initialize(base) ⇒ InstanceDelegate
constructor
A new instance of InstanceDelegate.
- #write ⇒ Object
Constructor Details
#initialize(base) ⇒ InstanceDelegate
73 74 75 |
# File 'lib/cache_it.rb', line 73 def initialize(base) @base = base end |
Instance Method Details
#delete ⇒ Object
96 97 98 |
# File 'lib/cache_it.rb', line 96 def delete keys(attributes_before_changes).each {|key| Rails.cache.delete(key)} end |
#increment(counter, amount = 1) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cache_it.rb', line 85 def increment(counter, amount = 1) counter = counter.to_s unless @base.class.cache_it.config.counters.include? counter raise ArgumentError, "#{counter} is not a counter" end primary_key = @base.class.primary_key if key = @base.class.cache_it.key({primary_key => @base[primary_key]}, :counter => counter) @base[counter] = Rails.cache.increment(key, amount, :raw => true) end end |
#init_counters ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/cache_it.rb', line 100 def init_counters primary_key = @base.class.primary_key @base.class.cache_it.config.counters.map do |counter| counter_key = @base.class.cache_it.key({primary_key => @base[primary_key]}, :counter => counter) @base[counter] = Rails.cache.fetch(counter_key, :raw => true) { @base[counter] } end end |
#write ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/cache_it.rb', line 77 def write expires_in = @base.class.cache_it.config.expires_in val = {:attributes => @base.attributes} keys.each {|key| Rails.cache.write(key, val, :expires_in => expires_in)} stale_keys.each {|key| Rails.cache.delete(key)} init_counters end |