Skip to content

Conversation

@andyatkinson
Copy link
Owner

@andyatkinson andyatkinson commented Jun 17, 2025

Basic examples:
https://github.com/evilmartians/activerecord-slotted_counters

Slotted Counters

A Rider can track its trip_requests.

# https://github.com/evilmartians/activerecord-slotted_counters
has_slotted_counter :trip_requests

Increment counter manually

rideshare(dev)> Rider.increment_counter(:trip_requests_count, rider.id)
  TRANSACTION (9.1ms)  BEGIN
   (2.3ms)  INSERT INTO "slotted_counters" ("counter_name","associated_record_type","associated_record_id","slot","count","created_at","updated_at") VALUES ('trip_requests_count', 'Rider', 20200, 86, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) ON CONFLICT ("associated_record_id","associated_record_type","counter_name","slot") DO UPDATE SET count = slotted_counters.count + EXCLUDED.count RETURNING "id"
  TRANSACTION (0.3ms)  COMMIT

Access:

rider.trip_requests_count

Why the "slot"?

  • By having a limit of rand(100), there are up to 100

@andyatkinson andyatkinson changed the title Add slotted counters Add evilmartians/activerecord-slotted_counters gem and usage example Jul 1, 2025
@andyatkinson andyatkinson force-pushed the chore/slotted-counters branch from 8890645 to fac2fe2 Compare July 1, 2025 03:46
@andyatkinson andyatkinson force-pushed the chore/slotted-counters branch from fac2fe2 to 81ce857 Compare July 1, 2025 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants