{"id":13878322,"url":"https://github.com/evilmartians/activerecord-slotted_counters","last_synced_at":"2025-04-07T08:14:36.982Z","repository":{"id":63973053,"uuid":"532965436","full_name":"evilmartians/activerecord-slotted_counters","owner":"evilmartians","description":"Active Record extension providing slotted counters support","archived":false,"fork":false,"pushed_at":"2025-03-06T01:33:02.000Z","size":108,"stargazers_count":107,"open_issues_count":0,"forks_count":8,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-31T07:04:17.156Z","etag":null,"topics":["activerecord","postgresql","rails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/evilmartians.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-05T15:49:07.000Z","updated_at":"2025-03-06T01:31:44.000Z","dependencies_parsed_at":"2023-02-10T10:30:32.972Z","dependency_job_id":"2dc1e8dd-0fa5-4756-a004-435a9b65e488","html_url":"https://github.com/evilmartians/activerecord-slotted_counters","commit_stats":{"total_commits":71,"total_committers":6,"mean_commits":"11.833333333333334","dds":0.323943661971831,"last_synced_commit":"c40736dea816f3ee5da1ec8e1dc0698707587fc1"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evilmartians%2Factiverecord-slotted_counters","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evilmartians%2Factiverecord-slotted_counters/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evilmartians%2Factiverecord-slotted_counters/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evilmartians%2Factiverecord-slotted_counters/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/evilmartians","download_url":"https://codeload.github.com/evilmartians/activerecord-slotted_counters/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247615377,"owners_count":20967184,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","postgresql","rails"],"created_at":"2024-08-06T08:01:46.320Z","updated_at":"2025-04-07T08:14:36.965Z","avatar_url":"https://github.com/evilmartians.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"[![Gem Version](https://badge.fury.io/rb/activerecord-slotted_counters.svg)](https://rubygems.org/gems/activerecord-slotted_counters) [![Build](https://github.com/evilmartians/activerecord-slotted_counters/workflows/Build/badge.svg)](https://github.com/evilmartians/activerecord-slotted_counters/actions)\n\n# Active Record slotted counters\n\nThis gem adds **slotted counters** support to [Active Record counter cache][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).\n\nRead more about slotted counters in [this post](https://planetscale.com/blog/the-slotted-counter-pattern).\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://evilmartians.com/?utm_source=active-record-slotted-counters\"\u003e\n    \u003cimg src=\"https://evilmartians.com/badges/sponsored-by-evil-martians.svg\"\n         alt=\"Sponsored by Evil Martians\" width=\"236\" height=\"54\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n## Installation\n\nAdd to your project:\n\n```ruby\n# Gemfile\ngem \"activerecord-slotted_counters\"\n```\n\n### Requirements\n\n- Ruby \u003e= 2.7.0\n- Rails 6+\n\n## Usage\n\nFirst, add and apply the required migration(-s):\n\n```sh\nbin/rails generate slotted_counters:install\nbin/rails db:migrate\n```\n\nThen, add the following line to the model to add a slotted counter:\n\n```ruby\nclass User \u003c ApplicationRecord\n  has_slotted_counter :comments\nend\n```\n\nNow you can use all the common counter cache APIs as before:\n\n```ruby\n# Manipulating the counter explicitly\nuser = User.first\n\nUser.increment_counter(:comments_count, user.id)\nUser.decrement_counter(:comments_count, user.id)\nUser.reset_counters(user.id, :comments)\n# etc.\n\n# Reading the value\nuser.comments_count\n```\n\nUnder the hood, a row in the `slotted_counters` table is created associated with the record.\n\n**NOTE:** Reading the current value performs SQL query once:\n\n```ruby\nuser.comments_count #=\u003e select * from slotted_counters where ...\nuser.comments_count #=\u003e no sql\n```\n\nIf you want to preload counters for multiple records, you can use a convenient `#with_slotted_counters` method:\n\n```ruby\nUser.all.with_slotted_counters(:comments).find_each do\n  _1.comments_count #=\u003e no sql\nend\n```\n\nUsing `counter_cache: true` on `belongs_to` associations also works as expected.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at [https://github.com/evilmartians/activerecord-slotted_counters](https://github.com/evilmartians/activerecord-slotted_counters).\n\n## Credis\n\nThis gem is generated via [new-gem-generator](https://github.com/palkan/new-gem-generator).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n\n[counter-cache]: https://api.rubyonrails.org/classes/ActiveRecord/CounterCache/ClassMethods.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevilmartians%2Factiverecord-slotted_counters","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fevilmartians%2Factiverecord-slotted_counters","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevilmartians%2Factiverecord-slotted_counters/lists"}