Git Product home page Git Product logo

cached_counts's Introduction

CachedCounts

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 queries

There are many alternatives, 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

Basic usage:

class User < ActiveRecord::Base
  include CachedCounts

  has_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
end

class Following < ActiveRecord::Base
  include CachedCounts

  belongs_to :followee, class_name: 'User' #, ...
end

Note 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 github.com/academia-edu/cached_counts

cached_counts's People

Contributors

nate00 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.