Module: ActsAsCached::InstanceMethods
- Defined in:
- lib/acts_as_cached/cache_methods.rb
Class Method Summary collapse
Instance Method Summary collapse
- #cache_id(key = nil) ⇒ Object
- #cached?(key = nil) ⇒ Boolean
- #caches(method, options = {}) ⇒ Object (also: #cached)
- #expire_cache(key = nil) ⇒ Object (also: #clear_cache)
-
#expire_cache_with_associations(*associations_to_sweep) ⇒ Object
Lourens Naud.
- #get_cache(key = nil, options = {}, &block) ⇒ Object
- #reset_cache(key = nil) ⇒ Object
- #set_cache(options = nil) ⇒ Object
-
#set_cache_with_associations ⇒ Object
Ryan King.
Class Method Details
.included(base) ⇒ Object
190 191 192 193 |
# File 'lib/acts_as_cached/cache_methods.rb', line 190 def self.included(base) base.send :delegate, :cache_config, :to => 'self.class' base.send :delegate, :cache_options, :to => 'self.class' end |
Instance Method Details
#cache_id(key = nil) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/acts_as_cached/cache_methods.rb', line 216 def cache_id(key = nil) cid = case when new_record? "new" when = self[:updated_at] = .utc.to_s(:number) "#{id}-#{}" else id.to_s end key.nil? ? cid : "#{cid}/#{key}" end |
#cached?(key = nil) ⇒ Boolean
212 213 214 |
# File 'lib/acts_as_cached/cache_methods.rb', line 212 def cached?(key = nil) self.class.cached? cache_id(key) end |
#caches(method, options = {}) ⇒ Object Also known as: cached
229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/acts_as_cached/cache_methods.rb', line 229 def caches(method, = {}) key = "#{self.cache_key}/#{method}" if .keys.include?(:with) with = .delete(:with) self.class.get_cache("#{key}/#{with}", ) { send(method, with) } elsif withs = .delete(:withs) self.class.get_cache("#{key}/#{withs}", ) { send(method, *withs) } else self.class.get_cache(key, ) { send(method) } end end |
#expire_cache(key = nil) ⇒ Object Also known as: clear_cache
207 208 209 |
# File 'lib/acts_as_cached/cache_methods.rb', line 207 def expire_cache(key = nil) self.class.expire_cache(cache_id(key)) end |
#expire_cache_with_associations(*associations_to_sweep) ⇒ Object
Lourens Naud
251 252 253 254 255 256 |
# File 'lib/acts_as_cached/cache_methods.rb', line 251 def expire_cache_with_associations(*associations_to_sweep) (Array([:include]) + associations_to_sweep).flatten.uniq.compact.each do |assoc| Array(send(assoc)).compact.each { |item| item.expire_cache if item.respond_to?(:expire_cache) } end expire_cache end |
#get_cache(key = nil, options = {}, &block) ⇒ Object
195 196 197 |
# File 'lib/acts_as_cached/cache_methods.rb', line 195 def get_cache(key = nil, = {}, &block) self.class.get_cache(cache_id(key), , &block) end |
#reset_cache(key = nil) ⇒ Object
203 204 205 |
# File 'lib/acts_as_cached/cache_methods.rb', line 203 def reset_cache(key = nil) self.class.reset_cache(cache_id(key)) end |
#set_cache(options = nil) ⇒ Object
199 200 201 |
# File 'lib/acts_as_cached/cache_methods.rb', line 199 def set_cache( = nil) self.class.set_cache(cache_id, self, ) end |
#set_cache_with_associations ⇒ Object
Ryan King
243 244 245 246 247 248 |
# File 'lib/acts_as_cached/cache_methods.rb', line 243 def set_cache_with_associations Array([:include]).each do |assoc| send(assoc).reload end if [:include] set_cache end |