Module: CachedCounts::ClassMethods

Defined in:
lib/cached_counts.rb

Instance Method Summary collapse

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.

Parameters:

  • attribute_name (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :association (Symbol)

    Name of the association to count. Defaults to the attribute_name (the required argument to caches_count_of).

  • :alias (String, Array<String>)

    Alias(es) for the count attribute. Useful with join tables. e.g.

    caches_count_of :user_departments, alias: 'users'
    > Department.first.users_count
    
  • :expires_in (Integer)

    Expiry for the cached value.

  • :if (Proc)

    proc passed through to the after_commit hooks on the counted class; decides whether an object counts towards the association total.

  • :scope (Proc)

    proc used like an ActiveRecord scope on the counted class on cache misses.

  • :version (Integer, #to_s)

    Cache version - bump if you change the definition of a count.



94
95
96
97
98
99
100
# File 'lib/cached_counts.rb', line 94

def caches_count_of(attribute_name, options = {})
  # Delay actual run to work around circular dependencies
  klass = self
  ActiveSupport.on_load :cached_counts do
    klass.send :caches_count_of!, attribute_name, options
  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.

Parameters:

  • attribute_name (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :scope (String)

    Name of the scope to count. Defaults to the attribute_name (the required argument to caches_count_where).

  • :alias (String, Array<String>)

    Alias(es) for the count attribute. e.g.

    caches_count_where :confirmed, alias: 'sitemap'
    > User.sitemap_count
    
  • :expires_in (Integer)

    Expiry for the cached value.

  • :if (Proc)

    proc passed through to the after_commit hooks; decides whether an object counts towards the association total.

  • :version (Integer, #to_s)

    Cache version - bump if you change the definition of a count.

  • :race_condition_fallback (Proc)

    Fallback to the result of this proc if the cache is empty, while loading the actual value from the db. Works similarly to race_condition_ttl but for empty caches rather than expired values. Meant to prevent a thundering-herd scenario, if for example a memcached instance goes away. Can be nil; defaults to using a value grabbed from the cache or DB at startup.



49
50
51
52
53
54
55
# File 'lib/cached_counts.rb', line 49

def caches_count_where(attribute_name, options = {})
  # Delay actual run to work around circular dependencies
  klass = self
  ActiveSupport.on_load :cached_counts do
    klass.send :caches_count_where!, attribute_name, options
  end
end