query_stash 0.1.0 → 0.1.2

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: d529606d76f063f11ac7a7abb456993d0246b0e3f2e36e7251e9ea5529fe5e29
4
- data.tar.gz: 0e9bfa4fb79a9e94702592cbff12322e20c9183d3e696930aa560c0f936f6ab9
3
+ metadata.gz: d8b96148cffcca877a9656d5ab76d459e506d63ea0cf33644519359842a40aee
4
+ data.tar.gz: 5db04a4e692c81a761118a9dd573385d6c80e69368dd69e1f8c4477f80d5dbf7
5
5
  SHA512:
6
- metadata.gz: 32dca414d0f63f2930d590bcdce47198606eeb2de6241ae14ebcffbfa81a2260b7dc657b9a3d4cd69371dc3e14e87f62f23a29b27fae712746cabda179eff105
7
- data.tar.gz: 69e189314e960fa8671bbba95b60f61ef6dfe85c4678cece2d1c3d9d947df69c56938ea2f76691ee78f1bcdb5b072866556258c42ff6ac1bfad274047423dd30
6
+ metadata.gz: f55f79ddba3939ccf5c84f5506e82f622f07e9f1c08815bc58a9ed3f303a351ae87448f5f1dd9fefe44582f25008151d7f4d7efb107cc3ccba717835169cf38e
7
+ data.tar.gz: d95cb51c54390b89a56df1a515d8d8c23ce7dd03dfb43ef7b50cecb6131fe3d98848f366fd5488726ed63f5f5c4bd46534a055ad406608f3250624a23f9a239b
data/README.md CHANGED
@@ -28,6 +28,7 @@ What it does not do:
28
28
  - **No invalidation.** Entries leave on TTL (plus jitter) or eviction. A write does not clear the stash, so a read after a write can return the old row until the TTL runs out. Wrap read-after-write code in `disable!`, or don't stash those queries.
29
29
  - **Inlined SQL only.** Queries with bind parameters are passed through untouched. Use an adapter or configuration that inlines values (Trilogy and mysql2 do by default; set `prepared_statements: false` for SQLite or PostgreSQL).
30
30
  - **`SELECT` only**, matched with `/\ASELECT/i`. SQL that starts with a comment, whitespace or a `WITH` clause is never stashed.
31
+ - **Locking reads always run.** A `SELECT` with `FOR UPDATE`, `FOR SHARE` (and their variants) or `LOCK IN SHARE MODE`, or a Sequel dataset with a lock, is never stashed, so it still takes its locks.
31
32
  - **Not transaction-aware.** A matching read inside a transaction can be served from the stash instead of seeing the transaction's own writes.
32
33
  - **`load_async` queries bypass it.**
33
34
  - The ActiveRecord cache key is the SQL plus `ActiveRecord::Base.current_shard`. It does not include the role or the database name.
@@ -35,6 +35,11 @@ module QueryStash
35
35
  CACHEABLE_SQL_REGEX = /\ASELECT/i #: Regexp
36
36
  private_constant :CACHEABLE_SQL_REGEX
37
37
 
38
+ # A `SELECT` that takes row locks (`FOR UPDATE`, `FOR SHARE`, `LOCK IN SHARE MODE`, ...): served from the
39
+ # stash, it would skip the lock. Rails' own query cache skips locked queries too.
40
+ LOCKING_SQL_REGEX = /\bFOR\s+(?:NO\s+)?(?:KEY\s+)?(?:UPDATE|SHARE)\b|\bLOCK\s+IN\s+SHARE\s+MODE\b/i #: Regexp
41
+ private_constant :LOCKING_SQL_REGEX
42
+
38
43
  # Enables the stash for the duration of each request.
39
44
  class Middleware
40
45
  # @rbs @app: _RackApp
@@ -158,12 +163,13 @@ module QueryStash
158
163
  end
159
164
 
160
165
  # The rule the query is stashed under, or nil when it isn't to be stashed: the stash is off, it has
161
- # binds (not supported), it is not a SELECT, or no rule matches it.
166
+ # binds (not supported), it is not a SELECT, it takes row locks, or no rule matches it.
162
167
  #: (String sql, ?has_binds: bool?) -> Registry::Rule?
163
168
  def rule_for(sql, has_binds: false)
164
169
  return unless stashing?
165
170
  return if has_binds
166
171
  return unless CACHEABLE_SQL_REGEX.match?(sql)
172
+ return if LOCKING_SQL_REGEX.match?(sql)
167
173
 
168
174
  rule = registry.match_rule(sql)
169
175
  rule if rule && (rule.enabled.nil? || rule.enabled.call)
@@ -37,6 +37,11 @@ module QueryStash
37
37
  CACHEABLE_SQL_REGEX = /\ASELECT/i #: Regexp
38
38
  private_constant :CACHEABLE_SQL_REGEX
39
39
 
40
+ # A `SELECT` that takes row locks, written in raw SQL (datasets have `opts[:lock]`): served from the stash,
41
+ # it would skip the lock.
42
+ LOCKING_SQL_REGEX = /\bFOR\s+(?:NO\s+)?(?:KEY\s+)?(?:UPDATE|SHARE)\b|\bLOCK\s+IN\s+SHARE\s+MODE\b/i #: Regexp
43
+ private_constant :LOCKING_SQL_REGEX
44
+
40
45
  # Enables the stash for the duration of each request. `application` is the Rails application the
41
46
  # middleware stack belongs to; `app` is the next middleware.
42
47
  class Middleware
@@ -111,12 +116,14 @@ module QueryStash
111
116
  end
112
117
 
113
118
  # The rule the query is stashed under, or nil when it isn't to be stashed: the stash is off, it has
114
- # binds or runs through a cursor (not supported), it is not a SELECT, or no rule matches it.
119
+ # binds or runs through a cursor (not supported), it is not a SELECT, it takes row locks, or no rule
120
+ # matches it.
115
121
  #: (_Dataset dataset, String sql) -> Registry::Rule?
116
122
  def rule_for(dataset, sql)
117
123
  return unless stashing?
118
124
  return if bound?(dataset) || dataset.opts[:cursor]
119
125
  return unless CACHEABLE_SQL_REGEX.match?(sql)
126
+ return if dataset.opts[:lock] || LOCKING_SQL_REGEX.match?(sql)
120
127
 
121
128
  rule = registry.match_rule(sql)
122
129
  rule if rule && (rule.enabled.nil? || rule.enabled.call)
@@ -2,5 +2,5 @@
2
2
 
3
3
  module QueryStash
4
4
  # Merging a change to this file into main releases the gem (.github/workflows/release.yml).
5
- VERSION = "0.1.0" #: String
5
+ VERSION = "0.1.2" #: String
6
6
  end
@@ -27,6 +27,10 @@ module QueryStash
27
27
 
28
28
  CACHEABLE_SQL_REGEX: Regexp
29
29
 
30
+ # A `SELECT` that takes row locks (`FOR UPDATE`, `FOR SHARE`, `LOCK IN SHARE MODE`, ...): served from the
31
+ # stash, it would skip the lock. Rails' own query cache skips locked queries too.
32
+ LOCKING_SQL_REGEX: Regexp
33
+
30
34
  # Enables the stash for the duration of each request.
31
35
  class Middleware
32
36
  @app: _RackApp
@@ -103,7 +107,7 @@ module QueryStash
103
107
  def self.implementation: () -> (singleton(InternalExecQuery) | singleton(ExecuteIntent))
104
108
 
105
109
  # The rule the query is stashed under, or nil when it isn't to be stashed: the stash is off, it has
106
- # binds (not supported), it is not a SELECT, or no rule matches it.
110
+ # binds (not supported), it is not a SELECT, it takes row locks, or no rule matches it.
107
111
  # : (String sql, ?has_binds: bool?) -> Registry::Rule?
108
112
  def rule_for: (String sql, ?has_binds: bool?) -> Registry::Rule?
109
113
 
@@ -28,6 +28,10 @@ module QueryStash
28
28
 
29
29
  CACHEABLE_SQL_REGEX: Regexp
30
30
 
31
+ # A `SELECT` that takes row locks, written in raw SQL (datasets have `opts[:lock]`): served from the stash,
32
+ # it would skip the lock.
33
+ LOCKING_SQL_REGEX: Regexp
34
+
31
35
  # Enables the stash for the duration of each request. `application` is the Rails application the
32
36
  # middleware stack belongs to; `app` is the next middleware.
33
37
  class Middleware
@@ -74,7 +78,8 @@ module QueryStash
74
78
  def wrap: (_Dataset dataset, String sql, (^(Hash[Symbol, untyped]) -> untyped)? consumer) { (?) -> untyped } -> untyped
75
79
 
76
80
  # The rule the query is stashed under, or nil when it isn't to be stashed: the stash is off, it has
77
- # binds or runs through a cursor (not supported), it is not a SELECT, or no rule matches it.
81
+ # binds or runs through a cursor (not supported), it is not a SELECT, it takes row locks, or no rule
82
+ # matches it.
78
83
  # : (_Dataset dataset, String sql) -> Registry::Rule?
79
84
  def rule_for: (_Dataset dataset, String sql) -> Registry::Rule?
80
85
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: query_stash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Persona