Git Product home page Git Product logo

activerecord-slotted_counters's Introduction

Gem Version Build

Active Record slotted counters

This gem adds slotted counters support to Active Record counter cache. Slotted counters help to reduce contention on a single row update in case you have many concurrent operations (like updating a page views counter during traffic spikes).

Read more about slotted counters in this post.

Sponsored by Evil Martians

Installation

Add to your project:

# Gemfile
gem "activerecord-slotted_counters"

Supported Ruby versions

  • Ruby (MRI) >= 2.7.0

Usage

First, add and apply the required migration(-s):

bin/rails generate slotted_counters:install
bin/rails db:migrate

Then, add the following line to the model to add a slotted counter:

class User < ApplicationRecord
  has_slotted_counter :comments
end

Now you can use all the common counter cache APIs as before:

# Manipulating the counter explicitly
user = User.first

User.increment_counter(:comments_count, user.id)
User.decrement_counter(:comments_count, user.id)
User.reset_counters(user.id, :comments)
# etc.

# Reading the value
user.comments_count

Under the hood, a row in the slotted_counters table is created associated with the record.

NOTE: Reading the current value performs SQL query once:

user.comments_count #=> select * from slotted_counters where ...
user.comments_count #=> no sql

If you want to preload counters for multiple records, you can use a convenient #with_slotted_counters method:

User.all.with_slotted_counters(:comments).find_each do
  _1.comments_count #=> no sql
end

Using counter_cache: true on belongs_to associations also works as expected.

Limitations / TODO

  • Gem supports only PostgreSQL for Rails 6

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/evilmartians/activerecord-slotted_counters.

Credis

This gem is generated via new-gem-generator.

License

The gem is available as open source under the terms of the MIT License.

activerecord-slotted_counters's People

Contributors

egor-lukin avatar palkan avatar danielwestendorf avatar latyshev avatar yaroslav 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.