Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/academia-edu/cached_counts
A replacement for Rails' counter caches, using memcached
https://github.com/academia-edu/cached_counts
Last synced: about 2 months ago
JSON representation
A replacement for Rails' counter caches, using memcached
- Host: GitHub
- URL: https://github.com/academia-edu/cached_counts
- Owner: academia-edu
- License: mit
- Created: 2016-05-08T17:59:05.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-07-14T14:58:48.000Z (about 2 years ago)
- Last Synced: 2024-07-12T14:45:45.127Z (2 months ago)
- Language: Ruby
- Size: 367 KB
- Stars: 3
- Watchers: 32
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.rdoc
- License: MIT-LICENSE
Awesome Lists containing this project
README
= CachedCounts
{}[https://circleci.com/gh/academia-edu/cached_counts]
A replacement for Rails' counter caches, using memcached.
Caches counts of models in a scope or association in memcached, and keeps the
cache up to date using incr/decr operations.You might prefer this gem over Rails' built-in counter caches when:
* You want to cache the total number of models for a class or scope
* You have a large association (e.g. followers of a popular figure) and want to avoid contention on a database row
* You want your caches to occasionally expire and be refreshed to mitigate the risk of getting out of sync
* You don't need to use counts as part of more complex SQL queriesThere are {many alternatives}[https://rubygems.org/search?utf8=%E2%9C%93&query=counter+cache], but this gem appears to be unique in its approach. Other gems solve the contention issue by pushing updates to a background job, which has the advantage of continuing to populate a column that can be queried via SQL, but is still more costly than our approach, while introducing a synchronization delay.
= Usage
{Documentation}[http://academia-edu.github.io/cached_counts/]
Basic usage:
class User < ActiveRecord::Base
include CachedCountshas_many :followers, class_name: 'Following' #, ...
scope :confirmed, ->{ where(confirmed: true) }# creates cached class method `User.confirmed_count`
caches_count_where :confirmed, if: :confirmed?# creates cached instance method `user.followers_count`
caches_count_of :followers
endclass Following < ActiveRecord::Base
include CachedCountsbelongs_to :followee, class_name: 'User' #, ...
endNote that +CachedCounts+ must be included in the counted class, but no class methods need be called there.
For full options, see docs for +CachedCounts.caches_count_of+ and +CachedCounts.caches_count_where+.
= Licence
MIT
Copyright Academia.edu
= Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/academia-edu/cached_counts