activerecord-slotted_counters 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +1 -1
- data/lib/activerecord_slotted_counters/has_slotted_counter.rb +7 -15
- data/lib/activerecord_slotted_counters/version.rb +1 -1
- metadata +20 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 94aead1f1be073e3dbf7548257d6db2b8eda15be09f2c199b6b3cdbf9b6ba0f3
|
4
|
+
data.tar.gz: ac2e1b977ab04501cd1139bd1d410b9f58b0b9395add905b1034eebb54fe4c86
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba74654e981dbc83043dc295296939e85053b2c7c8543ca3472d2f659523b9a3ea5be60ac15a87a492f7fa3758defac451382ff518a96f4feee573eb0f68ed26
|
7
|
+
data.tar.gz: 857ec7de346c584b1fcdd79bcb81becb7ecec9bb86b2087a8acf83d25ed13b55d25883439b6242b3f26bd726cc592897930ff9f1755c720b7aca41ed55d91503
|
data/CHANGELOG.md
CHANGED
@@ -2,9 +2,18 @@
|
|
2
2
|
|
3
3
|
## master
|
4
4
|
|
5
|
+
## 0.1.2 (2023-03-28)
|
6
|
+
|
7
|
+
- Fix preloading counters for multiple records [#12](https://github.com/evilmartians/activerecord-slotted_counters/pull/12) ([@LukinEgor][])
|
8
|
+
|
9
|
+
## 0.1.1 (2023-01-17)
|
10
|
+
|
11
|
+
- Fix prevent double increment/decrement of native counter caches [#10](https://github.com/evilmartians/activerecord-slotted_counters/pull/10) ([@danielwestendorf][])
|
12
|
+
|
5
13
|
## 0.1.0 (2022-11-29)
|
6
14
|
|
7
15
|
- Initial release.
|
8
16
|
|
9
17
|
[@palkan]: https://github.com/palkan
|
10
18
|
[@LukinEgor]: https://github.com/LukinEgor
|
19
|
+
[@danielwestendorf]: https://github.com/danielwestendorf
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# Active Record slotted counters
|
4
4
|
|
5
|
-
This 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 many concurrent operations (like updating a page views counter during traffic spikes).
|
5
|
+
This 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).
|
6
6
|
|
7
7
|
Read more about slotted counters in [this post](https://planetscale.com/blog/the-slotted-counter-pattern).
|
8
8
|
|
@@ -5,15 +5,15 @@ require "activerecord_slotted_counters/utils"
|
|
5
5
|
|
6
6
|
module ActiveRecordSlottedCounters
|
7
7
|
class SlottedCounter < ::ActiveRecord::Base
|
8
|
-
scope :associated_records, ->(counter_name,
|
9
|
-
where(counter_name: counter_name,
|
8
|
+
scope :associated_records, ->(counter_name, klass) do
|
9
|
+
where(counter_name: counter_name, associated_record_type: klass)
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
module BelongsToAssociation
|
14
14
|
def update_counters_via_scope(klass, foreign_key, by)
|
15
15
|
counter_name = reflection.counter_cache_column
|
16
|
-
super unless klass.registered_slotted_counter? counter_name
|
16
|
+
return super unless klass.registered_slotted_counter? counter_name
|
17
17
|
|
18
18
|
klass.update_counters(foreign_key, counter_name => by, :touch => reflection.options[:touch])
|
19
19
|
end
|
@@ -38,7 +38,7 @@ module ActiveRecordSlottedCounters
|
|
38
38
|
counter_name = slotted_counter_name(counter_type)
|
39
39
|
association_name = slotted_counter_association_name(counter_type)
|
40
40
|
|
41
|
-
has_many association_name, ->(model) { associated_records(counter_name, model.
|
41
|
+
has_many association_name, ->(model) { associated_records(counter_name, model.class.to_s) }, **SLOTTED_COUNTERS_ASSOCIATION_OPTIONS
|
42
42
|
|
43
43
|
scope :with_slotted_counters, ->(counter_type) do
|
44
44
|
association_name = slotted_counter_association_name(counter_type)
|
@@ -182,7 +182,7 @@ module ActiveRecordSlottedCounters
|
|
182
182
|
end
|
183
183
|
|
184
184
|
def increment!(attribute, by = 1, touch: nil)
|
185
|
-
super unless self.class.registered_slotted_counter? attribute
|
185
|
+
return super unless self.class.registered_slotted_counter? attribute
|
186
186
|
|
187
187
|
self.class.update_counters(id, attribute => by, :touch => touch)
|
188
188
|
end
|
@@ -191,16 +191,8 @@ module ActiveRecordSlottedCounters
|
|
191
191
|
|
192
192
|
def read_slotted_counter(counter_type)
|
193
193
|
association_name = slotted_counter_association_name(counter_type)
|
194
|
-
|
195
|
-
|
196
|
-
scope = association(association_name).scope
|
197
|
-
counter = scope.sum(&:count)
|
198
|
-
|
199
|
-
return counter
|
200
|
-
end
|
201
|
-
|
202
|
-
scope = send(association_name)
|
203
|
-
scope.sum(:count)
|
194
|
+
scope = association(association_name).load_target
|
195
|
+
scope.sum(&:count)
|
204
196
|
end
|
205
197
|
end
|
206
198
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-slotted_counters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Egor Lukin
|
8
8
|
- Vladimir Dementyev
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-04-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -95,6 +95,20 @@ dependencies:
|
|
95
95
|
- - ">="
|
96
96
|
- !ruby/object:Gem::Version
|
97
97
|
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: rspec-sqlimit
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
98
112
|
description: Active Record slotted counters support
|
99
113
|
email:
|
100
114
|
- dementiev.vm@gmail.com
|
@@ -121,7 +135,7 @@ metadata:
|
|
121
135
|
documentation_uri: http://github.com/evilmartians/activerecord-slotted_counters
|
122
136
|
homepage_uri: http://github.com/evilmartians/activerecord-slotted_counters
|
123
137
|
source_code_uri: http://github.com/evilmartians/activerecord-slotted_counters
|
124
|
-
post_install_message:
|
138
|
+
post_install_message:
|
125
139
|
rdoc_options: []
|
126
140
|
require_paths:
|
127
141
|
- lib
|
@@ -136,8 +150,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
150
|
- !ruby/object:Gem::Version
|
137
151
|
version: '0'
|
138
152
|
requirements: []
|
139
|
-
rubygems_version: 3.3.
|
140
|
-
signing_key:
|
153
|
+
rubygems_version: 3.3.3
|
154
|
+
signing_key:
|
141
155
|
specification_version: 4
|
142
156
|
summary: Active Record slotted counters support
|
143
157
|
test_files: []
|