Module: CachedCounts::ClassMethods
- Defined in:
- lib/cached_counts.rb
Instance Method Summary collapse
-
#caches_count_of(attribute_name, options = {}) ⇒ Object
Cache the count for an association in memcached.
-
#caches_count_where(attribute_name, options = {}) ⇒ Object
Cache the count for a scope in memcached.
Instance Method Details
#caches_count_of(attribute_name, options = {}) ⇒ Object
Cache the count for an association in memcached.
e.g.
User.caches_count_of :friends
> User.first.friends_count # Users.first.friends.count, but cached
Automatically adds after_commit hooks to the associated class which increment/decrement the value in memcached when needed. Queries the db on cache miss.
94 95 96 97 98 99 100 |
# File 'lib/cached_counts.rb', line 94 def caches_count_of(attribute_name, = {}) # Delay actual run to work around circular dependencies klass = self ActiveSupport.on_load :cached_counts do klass.send :caches_count_of!, attribute_name, end end |
#caches_count_where(attribute_name, options = {}) ⇒ Object
Cache the count for a scope in memcached.
e.g.
User.caches_count_where :confirmed
> User.confirmed_count # User.confirmed.count, but cached
Automatically adds after_commit hooks which increment/decrement the value in memcached when needed. Queries the db on cache miss.
49 50 51 52 53 54 55 |
# File 'lib/cached_counts.rb', line 49 def caches_count_where(attribute_name, = {}) # Delay actual run to work around circular dependencies klass = self ActiveSupport.on_load :cached_counts do klass.send :caches_count_where!, attribute_name, end end |