No commit activity in last 3 years
No release in over 3 years
Provides volatile counter cache logic to ActiveRecord::Base.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 10.0

Runtime

 Project Readme

VolatileCounterCache

Provides volatile counter cache logic to ActiveRecord::Base.

Usage

  1. favorites_count tries to read counter cache from Rails.cache.
  2. If there is no cache yet, it calls favorites.size and store the result.
  3. When favorite is created or destroyed, the cache is purged.
class Tweet < ActiveRecord::Base
  has_many :favorites

  volatile_counter_cache :favorites, cache: Rails.cache
end

Tweet.first.favorites_count #=> 42
Tweet.first.favorites.first.destroy
Tweet.first.favorites_count #=> 41

options

  • cache [ActiveSupport::Cache::Store] A store object to store count for each key.
  • cache_options [Hash] Options for cache store.
  • counter_method_name [String] An optional String to change counter method name.
  • foreign_key [Symbol] An optional Symbol to change cache key at callback.