solid_cache 1.0.6 → 1.0.8

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: 1cf02364d38d08f18cbe3d9811a7957018ec23ebaf08951c51110204eea237de
4
- data.tar.gz: c04b221f9c37fdc78f78e07143afe33bc1d346695ef8c910dc986533dd460a38
3
+ metadata.gz: fbc53d2cf4cc3edfeb5292055f3e8bb9c0057e4e067bee764a4d88a62fbd8278
4
+ data.tar.gz: e3e9eac0f9a24a26eb3e992b2d2cdd0037c5d9ca4c36ea7cf8751b257fdf152b
5
5
  SHA512:
6
- metadata.gz: 725fa1c931e54f8aa743aa8311fd05f44fe200904ede83bcb1207377f54926c330abe887a2dfb04c956f703f8e673b38f8af219a31e04c2f1d653cdbe0b980db
7
- data.tar.gz: 6d793397978b7d1e1797ed4b97726dc25c1e053a8a8dc2aa9b39669bbb79c3a9d7b9d0d676e43bea4f1a401ae7528a1aee34c5985899e96e092b86c1be4c2f0a
6
+ metadata.gz: 3e94a6318abc57922c7b98c34b4034a660c8f598a557f0fb38a95f313228e02ae437396d415fe7bb2ed8782a492274458f621e059f74ea75905fd91b2bdee155
7
+ data.tar.gz: deebaed2b15b865e87aaf27ac521e0b85fb8ebffab19651dec622c6ad6008cb489ec1648059a26f7d870bf327b8295f70eb76651bb3465a5613a16c14b2901b8
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Solid Cache
2
2
 
3
- Solid Cache is a database-backed Active Support cache store that let's you keep a much larger cache than is typically possible with traditional memory-only Redis or Memcached stores. This is thanks to the speed of modern SSD drives, which make the access-time penalty of using disk vs RAM insignificant for most caching purposes. Simply put, you're now usually better off keeping a huge cache on disk rather than a small cache in memory.
3
+ Solid Cache is a database-backed Active Support cache store that lets you keep a much larger cache than is typically possible with traditional memory-only Redis or Memcached stores. This is thanks to the speed of modern SSD drives, which make the access-time penalty of using disk vs RAM insignificant for most caching purposes. Simply put, you're now usually better off keeping a huge cache on disk rather than a small cache in memory.
4
4
 
5
5
  ## Installation
6
6
 
@@ -11,7 +11,7 @@ Solid Cache is configured by default in new Rails 8 applications. But if you're
11
11
 
12
12
  This will configure Solid Cache as the production cache store, create `config/cache.yml`, and create `db/cache_schema.rb`.
13
13
 
14
- You will then have to add the configuration for the queue database in `config/database.yml`. If you're using sqlite, it'll look like this:
14
+ You will then have to add the configuration for the cache database in `config/database.yml`. If you're using sqlite, it'll look like this:
15
15
 
16
16
  ```yaml
17
17
  production:
@@ -55,7 +55,7 @@ default:
55
55
  size_estimate_samples: 1000
56
56
 
57
57
  development: &development
58
- database: development_cache
58
+ database: cache
59
59
  store_options:
60
60
  <<: *default_store_options
61
61
  max_size: <%= 256.gigabytes %>
@@ -69,6 +69,13 @@ production: &production
69
69
 
70
70
  For the full list of keys for `store_options` see [Cache configuration](#cache-configuration). Any options passed to the cache lookup will overwrite those specified here.
71
71
 
72
+ After running `solid_cache:install`, `environments/production.rb` will replace your cache store with Solid Cache, but you can also do this manually:
73
+
74
+ ```ruby
75
+ # config/environments/production.rb
76
+ config.cache_store = :solid_cache_store
77
+ ```
78
+
72
79
  ### Connection configuration
73
80
 
74
81
  You can set one of `database`, `databases` and `connects_to` in the config file. They will be used to configure the cache databases in `SolidCache::Record#connects_to`.
@@ -189,7 +196,7 @@ or
189
196
  config.solid_cache.encrypt = true
190
197
  ```
191
198
 
192
- You will need to set up your application to (use Active Record Encryption)[https://guides.rubyonrails.org/active_record_encryption.html].
199
+ You will need to set up your application to [use Active Record Encryption](https://guides.rubyonrails.org/active_record_encryption.html).
193
200
 
194
201
  Solid Cache by default uses a custom encryptor and message serializer that are optimised for it.
195
202
 
@@ -260,7 +267,7 @@ This ensures that all the Rails versions dependencies are updated.
260
267
 
261
268
  ## Implementation
262
269
 
263
- Solid Cache is a FIFO (first in, first out) cache. While this is not as efficient as an LRU cache, it is mitigated by the longer cache lifespan.
270
+ Solid Cache is a FIFO (first in, first out) cache. While this is not as efficient as an LRU (least recently used) cache, it is mitigated by the longer cache lifespan.
264
271
 
265
272
  A FIFO cache is much easier to manage:
266
273
  1. We don't need to track when items are read.
@@ -14,6 +14,8 @@ module SolidCache
14
14
 
15
15
  KEY_HASH_ID_RANGE = -(2**63)..(2**63 - 1)
16
16
 
17
+ MULTI_BATCH_SIZE = 1000
18
+
17
19
  class << self
18
20
  def write(key, value)
19
21
  write_multi([ { key: key, value: value } ])
@@ -21,9 +23,11 @@ module SolidCache
21
23
 
22
24
  def write_multi(payloads)
23
25
  without_query_cache do
24
- upsert_all \
25
- add_key_hash_and_byte_size(payloads),
26
- unique_by: upsert_unique_by, on_duplicate: :update, update_only: [ :key, :value, :byte_size ]
26
+ payloads.each_slice(MULTI_BATCH_SIZE).each do |payload_batch|
27
+ upsert_all \
28
+ add_key_hash_and_byte_size(payload_batch),
29
+ unique_by: upsert_unique_by, on_duplicate: :update, update_only: [ :key, :value, :byte_size ]
30
+ end
27
31
  end
28
32
  end
29
33
 
@@ -33,9 +37,15 @@ module SolidCache
33
37
 
34
38
  def read_multi(keys)
35
39
  without_query_cache do
36
- query = Arel.sql(select_sql(keys), *key_hashes_for(keys))
40
+ {}.tap do |results|
41
+ keys.each_slice(MULTI_BATCH_SIZE).each do |keys_batch|
42
+ query = Arel.sql(select_sql(keys_batch), *key_hashes_for(keys_batch))
37
43
 
38
- connection.select_all(query, "SolidCache::Entry Load").cast_values(attribute_types).to_h
44
+ with_connection do |connection|
45
+ results.merge!(connection.select_all(query, "SolidCache::Entry Load").cast_values(attribute_types).to_h)
46
+ end
47
+ end
48
+ end
39
49
  end
40
50
  end
41
51
 
@@ -46,7 +56,9 @@ module SolidCache
46
56
  end
47
57
 
48
58
  def clear_truncate
49
- connection.truncate(table_name)
59
+ with_connection do |connection|
60
+ connection.truncate(table_name)
61
+ end
50
62
  end
51
63
 
52
64
  def clear_delete
@@ -83,7 +95,9 @@ module SolidCache
83
95
  end
84
96
 
85
97
  def upsert_unique_by
86
- connection.supports_insert_conflict_target? ? :key_hash : nil
98
+ with_connection do |connection|
99
+ connection.supports_insert_conflict_target? ? :key_hash : nil
100
+ end
87
101
  end
88
102
 
89
103
  # This constructs and caches a SQL query for a given number of keys.
@@ -10,7 +10,22 @@ module SolidCache
10
10
 
11
11
  class << self
12
12
  def disable_instrumentation(&block)
13
- connection.with_instrumenter(NULL_INSTRUMENTER, &block)
13
+ with_instrumenter(NULL_INSTRUMENTER, &block)
14
+ end
15
+
16
+ def with_instrumenter(instrumenter, &block)
17
+ with_connection do |connection|
18
+ if connection.respond_to?(:with_instrumenter)
19
+ connection.with_instrumenter(instrumenter, &block)
20
+ else
21
+ begin
22
+ old_instrumenter, ActiveSupport::IsolatedExecutionState[:active_record_instrumenter] = ActiveSupport::IsolatedExecutionState[:active_record_instrumenter], instrumenter
23
+ block.call
24
+ ensure
25
+ ActiveSupport::IsolatedExecutionState[:active_record_instrumenter] = old_instrumenter
26
+ end
27
+ end
28
+ end
14
29
  end
15
30
 
16
31
  def with_shard(shard, &block)
@@ -41,7 +41,7 @@ module SolidCache
41
41
  end
42
42
 
43
43
  config.after_initialize do
44
- if SolidCache.configuration.encrypt? && Record.connection.adapter_name == "PostgreSQL" && Rails::VERSION::MAJOR <= 7
44
+ if SolidCache.configuration.encrypt? && Record.lease_connection.adapter_name == "PostgreSQL" && Rails::VERSION::MAJOR <= 7
45
45
  raise \
46
46
  "Cannot enable encryption for Solid Cache: in Rails 7, Active Record Encryption does not support " \
47
47
  "encrypting binary columns on PostgreSQL"
@@ -34,26 +34,6 @@ module SolidCache
34
34
  end
35
35
  end
36
36
 
37
- def with_connection_for(key, async: false, &block)
38
- connections.with_connection_for(key) do
39
- execute(async, &block)
40
- end
41
- end
42
-
43
- def with_connection(name, async: false, &block)
44
- connections.with(name) do
45
- execute(async, &block)
46
- end
47
- end
48
-
49
- def group_by_connection(keys)
50
- connections.assign(keys)
51
- end
52
-
53
- def connection_names
54
- connections.names
55
- end
56
-
57
37
  def connections
58
38
  @connections ||= SolidCache::Connections.from_config(@shard_options)
59
39
  end
@@ -63,6 +43,26 @@ module SolidCache
63
43
  connections
64
44
  end
65
45
 
46
+ def with_connection_for(key, async: false, &block)
47
+ connections.with_connection_for(key) do
48
+ execute(async, &block)
49
+ end
50
+ end
51
+
52
+ def with_connection(name, async: false, &block)
53
+ connections.with(name) do
54
+ execute(async, &block)
55
+ end
56
+ end
57
+
58
+ def group_by_connection(keys)
59
+ connections.assign(keys)
60
+ end
61
+
62
+ def connection_names
63
+ connections.names
64
+ end
65
+
66
66
  def reading_key(key, failsafe:, failsafe_returning: nil, &block)
67
67
  failsafe(failsafe, returning: failsafe_returning) do
68
68
  with_connection_for(key, &block)
@@ -70,16 +70,15 @@ module SolidCache
70
70
  end
71
71
 
72
72
  def reading_keys(keys, failsafe:, failsafe_returning: nil)
73
- group_by_connection(keys).map do |connection, keys|
73
+ group_by_connection(keys).map do |connection, grouped_keys|
74
74
  failsafe(failsafe, returning: failsafe_returning) do
75
75
  with_connection(connection) do
76
- yield keys
76
+ yield grouped_keys
77
77
  end
78
78
  end
79
79
  end
80
80
  end
81
81
 
82
-
83
82
  def writing_key(key, failsafe:, failsafe_returning: nil, &block)
84
83
  failsafe(failsafe, returning: failsafe_returning) do
85
84
  with_connection_for(key, &block)
@@ -87,10 +86,10 @@ module SolidCache
87
86
  end
88
87
 
89
88
  def writing_keys(entries, failsafe:, failsafe_returning: nil)
90
- group_by_connection(entries).map do |connection, entries|
89
+ group_by_connection(entries).map do |connection, grouped_entries|
91
90
  failsafe(failsafe, returning: failsafe_returning) do
92
91
  with_connection(connection) do
93
- yield entries
92
+ yield grouped_entries
94
93
  end
95
94
  end
96
95
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "concurrent/atomic/atomic_fixnum"
4
-
5
3
  module SolidCache
6
4
  class Store
7
5
  module Expiry
@@ -6,7 +6,6 @@ module SolidCache
6
6
  TRANSIENT_ACTIVE_RECORD_ERRORS = [
7
7
  ActiveRecord::AdapterTimeout,
8
8
  ActiveRecord::ConnectionNotEstablished,
9
- ActiveRecord::ConnectionTimeoutError,
10
9
  ActiveRecord::Deadlocked,
11
10
  ActiveRecord::LockWaitTimeout,
12
11
  ActiveRecord::QueryCanceled,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolidCache
4
- VERSION = "1.0.6"
4
+ VERSION = "1.0.8"
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solid_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Donal McBreen
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-09-10 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activerecord
@@ -141,9 +140,6 @@ licenses:
141
140
  metadata:
142
141
  homepage_uri: http://github.com/rails/solid_cache
143
142
  source_code_uri: http://github.com/rails/solid_cache
144
- post_install_message: |
145
- Upgrading from Solid Cache v0.3 or earlier? There are new database migrations in v0.4.
146
- See https://github.com/rails/solid_cache/blob/main/upgrading_to_version_0.4.x.md for upgrade instructions.
147
143
  rdoc_options: []
148
144
  require_paths:
149
145
  - lib
@@ -158,8 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
154
  - !ruby/object:Gem::Version
159
155
  version: '0'
160
156
  requirements: []
161
- rubygems_version: 3.5.11
162
- signing_key:
157
+ rubygems_version: 3.6.9
163
158
  specification_version: 4
164
159
  summary: A database backed ActiveSupport::Cache::Store
165
160
  test_files: []