Module: ActiveRecordCacheExtension
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/active_record_cache_extension.rb
Overview
To understand our caching conventions, have a look at our wiki page: github.com/fiedl/wingolfsplattform/wiki/Caching
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #bulk_delete_cached(method_name, objects) ⇒ Object
- #cache_created_at(method_name, arguments = nil) ⇒ Object
-
#cached(method_name = nil, arguments = nil, &block) ⇒ Object
Case 1: Use it to call a cached method result.
- #delete_cache ⇒ Object
- #delete_cached(method_name) ⇒ Object
- #invalidate_cache ⇒ Object
-
#uncached(method_name, args = nil) ⇒ Object
This method ensures that no app cache is used to produce the result.
Instance Method Details
#bulk_delete_cached(method_name, objects) ⇒ Object
105 106 107 108 109 110 |
# File 'app/models/active_record_cache_extension.rb', line 105 def bulk_delete_cached(method_name, objects) ids = objects.map &:id regex = /.*\/(#{ids.join('|')})(-.*|)\/#{method_name}.*/ # p "DEBUG BULK DELETE CACHE #{regex}" Rails.cache.delete_regex regex end |
#cache_created_at(method_name, arguments = nil) ⇒ Object
117 118 119 120 |
# File 'app/models/active_record_cache_extension.rb', line 117 def cache_created_at(method_name, arguments = nil) CacheAdditions Rails.cache.created_at [self, method_name, arguments] end |
#cached(method_name = nil, arguments = nil, &block) ⇒ Object
Case 1: Use it to call a cached method result.
user.cached(:name)
Case 2: Use it to call a cached method with arguments. Use this with care, since there is a cache for each argument!
user.cached(:membership_in, group)
Case 3: Use it with a block within a method definition. This moves the responsibility to cache into the model itself.
class User
def name
cached { "#{first_name} #{last_name}" }
end
end
user.name # already uses the cache!
28 29 30 31 32 33 34 |
# File 'app/models/active_record_cache_extension.rb', line 28 def cached(method_name = nil, arguments = nil, &block) if method_name cached_method(method_name, arguments) else cached_block(&block) end end |
#delete_cache ⇒ Object
112 113 114 115 |
# File 'app/models/active_record_cache_extension.rb', line 112 def delete_cache # p "DEBUG DELETE CACHE #{self}" Rails.cache.delete_matched "#{self.cache_key}/*" end |
#delete_cached(method_name) ⇒ Object
99 100 101 102 103 |
# File 'app/models/active_record_cache_extension.rb', line 99 def delete_cached(method_name) # p "DEBUG DELETE CACHED #{self} #{method_name}" Rails.cache.delete [self, method_name] Rails.cache.delete_matched "#{self.cache_key}/#{method_name}/*" end |
#invalidate_cache ⇒ Object
94 95 96 97 |
# File 'app/models/active_record_cache_extension.rb', line 94 def invalidate_cache # Be careful in specs. This takes one second to count as invalid. self.touch end |
#uncached(method_name, args = nil) ⇒ Object
This method ensures that no app cache is used to produce the result. If you call
user.uncached :title
this calls ‘user.title` but makes sure, no app cache is used at all. Note: This does not prevent the sql cache to be used.
You could use this in specs:
user.cached(:title).should == user.uncached(:title)
## What about user.title?
Usually, user.title returns the uncached version; but if cached methods are used in the implementation of ‘User#title` then `user.title` does use these caches. If you call `user.uncached(:title)`, all app caches are ignored.
140 141 142 143 144 145 146 147 148 |
# File 'app/models/active_record_cache_extension.rb', line 140 def uncached(method_name, args = nil) Rails.cache.uncached do if args self.send method_name, *args else self.send method_name end end end |