solid_cache 0.5.3 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e16bebed50154ce9a1657114a5fe1316d8137e7f5a0136fdb8858ece68f5351
4
- data.tar.gz: 6a66887e0dac5e4b52fae796c482fcc54ebcd1402f145217cf3530adc47d005a
3
+ metadata.gz: 7ba0486907f0e4656593843d1cca1c7ec35763772ff6d5a2a4e43b9d43b023b3
4
+ data.tar.gz: a93991c212dcb6d0a1dedb20e2d8d85e0664092c2342db895489beafb9f17e23
5
5
  SHA512:
6
- metadata.gz: 527da66c8b66d68ae31eafe5d3b6474f5801649119ed96848ec83a6990f3b59c0a35a706b20a33ba2ab99beebe3195575388b8f44e64308c4060e20b6fd9892f
7
- data.tar.gz: 0d41d9ad62843d0815db7468b9475088f6f95d3122a0eaada4d16e15020eee38a5a4642dd1570f8bde0ee347f7ea54a38c5b6810429b316ad888a3fb641dadf2
6
+ metadata.gz: 9897aef78db43aff6bceea922aa43669f76a48852d4dc94e64ce8aa198c1ca7fd04152a15033760a6fecad11245b650369342345c6f07e366c3d3d4e0a71f1da
7
+ data.tar.gz: cd59a068b761fd6060005d9ef78328cd03a3420ee539c07a6afd14eed072a0e9a092d135fbadeb5a58d38aa5a146e8481286b50ef411548a15b701464c0394b0
@@ -31,7 +31,7 @@ module SolidCache
31
31
  end
32
32
 
33
33
  def delete_by_key(key)
34
- delete_no_query_cache(:key_hash, key_hash_for(key))
34
+ delete_no_query_cache(:key_hash, key_hash_for(key)) > 0
35
35
  end
36
36
 
37
37
  def delete_multi(keys)
@@ -47,21 +47,17 @@ module SolidCache
47
47
  in_batches.delete_all
48
48
  end
49
49
 
50
- def increment(key, amount)
50
+ def lock_and_write(key, &block)
51
51
  transaction do
52
52
  uncached do
53
53
  result = lock.where(key_hash: key_hash_for(key)).pick(:key, :value)
54
- amount += result[1].to_i if result&.first == key
55
- write(key, amount)
56
- amount
54
+ new_value = block.call(result&.first == key ? result[1] : nil)
55
+ write(key, new_value)
56
+ new_value
57
57
  end
58
58
  end
59
59
  end
60
60
 
61
- def decrement(key, amount)
62
- increment(key, -amount)
63
- end
64
-
65
61
  def id_range
66
62
  uncached do
67
63
  pick(Arel.sql("max(id) - min(id) + 1")) || 0
@@ -70,13 +66,13 @@ module SolidCache
70
66
 
71
67
  private
72
68
  def upsert_all_no_query_cache(payloads)
73
- insert_all = ActiveRecord::InsertAll.new(
74
- self,
75
- add_key_hash_and_byte_size(payloads),
76
- unique_by: upsert_unique_by,
77
- on_duplicate: :update,
78
- update_only: upsert_update_only
79
- )
69
+ args = [ self,
70
+ connection_for_insert_all,
71
+ add_key_hash_and_byte_size(payloads) ].compact
72
+ options = { unique_by: upsert_unique_by,
73
+ on_duplicate: :update,
74
+ update_only: upsert_update_only }
75
+ insert_all = ActiveRecord::InsertAll.new(*args, **options)
80
76
  sql = connection.build_insert_sql(ActiveRecord::InsertAll::Builder.new(insert_all))
81
77
 
82
78
  message = +"#{self} "
@@ -86,6 +82,10 @@ module SolidCache
86
82
  connection.send exec_query_method, sql, message
87
83
  end
88
84
 
85
+ def connection_for_insert_all
86
+ Rails.version >= "7.2" ? connection : nil
87
+ end
88
+
89
89
  def add_key_hash_and_byte_size(payloads)
90
90
  payloads.map do |payload|
91
91
  payload.dup.tap do |payload|
@@ -148,9 +148,9 @@ module SolidCache
148
148
 
149
149
  # exec_delete does not clear the query cache
150
150
  if connection.prepared_statements?
151
- connection.exec_delete(sql, "#{name} Delete All", Array(values)).nonzero?
151
+ connection.exec_delete(sql, "#{name} Delete All", Array(values))
152
152
  else
153
- connection.exec_delete(sql, "#{name} Delete All").nonzero?
153
+ connection.exec_delete(sql, "#{name} Delete All")
154
154
  end
155
155
  end
156
156
  end
@@ -1,4 +1,4 @@
1
- class AddKeyHashAndByteSizeToSolidCacheEntries < ActiveRecord::Migration[7.1]
1
+ class AddKeyHashAndByteSizeToSolidCacheEntries < ActiveRecord::Migration[7.0]
2
2
  def change
3
3
  change_table :solid_cache_entries do |t|
4
4
  t.column :key_hash, :integer, null: true, limit: 8
@@ -1,4 +1,4 @@
1
- class AddKeyHashAndByteSizeIndexesAndNullConstraintsToSolidCacheEntries < ActiveRecord::Migration[7.1]
1
+ class AddKeyHashAndByteSizeIndexesAndNullConstraintsToSolidCacheEntries < ActiveRecord::Migration[7.0]
2
2
  def change
3
3
  change_table :solid_cache_entries, bulk: true do |t|
4
4
  t.change_null :key_hash, false
@@ -1,4 +1,4 @@
1
- class RemoveKeyIndexFromSolidCacheEntries < ActiveRecord::Migration[7.1]
1
+ class RemoveKeyIndexFromSolidCacheEntries < ActiveRecord::Migration[7.0]
2
2
  def change
3
3
  change_table :solid_cache_entries do |t|
4
4
  t.remove_index :key, unique: true
@@ -15,17 +15,11 @@ module SolidCache
15
15
  end
16
16
 
17
17
  def increment(name, amount = 1, options = nil)
18
- options = merged_options(options)
19
- key = normalize_key(name, options)
20
-
21
- entry_increment(key, amount)
18
+ adjust(name, amount, options)
22
19
  end
23
20
 
24
21
  def decrement(name, amount = 1, options = nil)
25
- options = merged_options(options)
26
- key = normalize_key(name, options)
27
-
28
- entry_decrement(key, amount)
22
+ adjust(name, -amount, options)
29
23
  end
30
24
 
31
25
  def cleanup(options = nil)
@@ -41,7 +35,7 @@ module SolidCache
41
35
  deserialize_entry(read_serialized_entry(key, **options), **options)
42
36
  end
43
37
 
44
- def read_serialized_entry(key, raw: false, **options)
38
+ def read_serialized_entry(key, **options)
45
39
  entry_read(key)
46
40
  end
47
41
 
@@ -109,11 +103,7 @@ module SolidCache
109
103
  end
110
104
 
111
105
  def serialize_entry(entry, raw: false, **options)
112
- if raw
113
- entry.value.to_s
114
- else
115
- super(entry, raw: raw, **options)
116
- end
106
+ super(entry, raw: raw, **options)
117
107
  end
118
108
 
119
109
  def serialize_entries(entries, **options)
@@ -122,12 +112,8 @@ module SolidCache
122
112
  end
123
113
  end
124
114
 
125
- def deserialize_entry(payload, raw: false, **)
126
- if payload && raw
127
- ActiveSupport::Cache::Entry.new(payload)
128
- else
129
- super(payload)
130
- end
115
+ def deserialize_entry(payload, **)
116
+ super(payload)
131
117
  end
132
118
 
133
119
  def normalize_key(key, options)
@@ -143,6 +129,30 @@ module SolidCache
143
129
  key
144
130
  end
145
131
  end
132
+
133
+ def adjust(name, amount, options)
134
+ options = merged_options(options)
135
+ key = normalize_key(name, options)
136
+
137
+ new_value = entry_lock_and_write(key) do |value|
138
+ serialize_entry(adjusted_entry(value, amount, options))
139
+ end
140
+ deserialize_entry(new_value, **options).value if new_value
141
+ end
142
+
143
+ def adjusted_entry(value, amount, options)
144
+ entry = deserialize_entry(value, **options)
145
+
146
+ if entry && !entry.expired?
147
+ ActiveSupport::Cache::Entry.new \
148
+ amount + entry.value.to_i, **options.dup.merge(expires_in: nil, expires_at: entry.expires_at)
149
+ elsif /\A\d+\z/.match?(value)
150
+ # This is to match old raw values
151
+ ActiveSupport::Cache::Entry.new(amount + value.to_i, **options)
152
+ else
153
+ ActiveSupport::Cache::Entry.new(amount, **options)
154
+ end
155
+ end
146
156
  end
147
157
  end
148
158
  end
@@ -67,12 +67,12 @@ module SolidCache
67
67
 
68
68
  def writing_all(failsafe:, failsafe_returning: nil, &block)
69
69
  first_cluster_sync_rest_async do |cluster, async|
70
- cluster.connection_names.each do |connection|
70
+ cluster.connection_names.map do |connection|
71
71
  failsafe(failsafe, returning: failsafe_returning) do
72
72
  cluster.with_connection(connection, async: async, &block)
73
73
  end
74
74
  end
75
- end
75
+ end.first
76
76
  end
77
77
 
78
78
  def first_cluster_sync_rest_async
@@ -18,7 +18,7 @@ module SolidCache
18
18
 
19
19
  private
20
20
  def entry_clear
21
- writing_all(failsafe: :clear) do
21
+ writing_all(failsafe: :clear, failsafe_returning: nil) do
22
22
  if clear_with == :truncate
23
23
  Entry.clear_truncate
24
24
  else
@@ -27,15 +27,9 @@ module SolidCache
27
27
  end
28
28
  end
29
29
 
30
- def entry_increment(key, amount)
30
+ def entry_lock_and_write(key, &block)
31
31
  writing_key(key, failsafe: :increment) do
32
- Entry.increment(key, amount)
33
- end
34
- end
35
-
36
- def entry_decrement(key, amount)
37
- writing_key(key, failsafe: :decrement) do
38
- Entry.decrement(key, amount)
32
+ Entry.lock_and_write(key, &block)
39
33
  end
40
34
  end
41
35
 
@@ -52,7 +46,7 @@ module SolidCache
52
46
  end
53
47
 
54
48
  def entry_write(key, payload)
55
- writing_key(key, failsafe: :write_entry, failsafe_returning: false) do |cluster|
49
+ writing_key(key, failsafe: :write_entry, failsafe_returning: nil) do |cluster|
56
50
  Entry.write(key, payload)
57
51
  cluster.track_writes(1)
58
52
  true
@@ -74,7 +68,7 @@ module SolidCache
74
68
  end
75
69
 
76
70
  def entry_delete_multi(entries)
77
- writing_keys(entries, failsafe: :delete_multi_entries, failsafe_returning: false) do
71
+ writing_keys(entries, failsafe: :delete_multi_entries, failsafe_returning: 0) do
78
72
  Entry.delete_multi(entries)
79
73
  end
80
74
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolidCache
4
- VERSION = "0.5.3"
4
+ VERSION = "0.6.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solid_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Donal McBreen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-29 00:00:00.000000000 Z
11
+ date: 2024-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: msgpack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: A database backed ActiveSupport::Cache::Store
84
98
  email:
85
99
  - donal@37signals.com