response_bank 1.5.0 → 1.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: c3db28140576d28fa3faf6af84b571085208f7f40b977e615b102f038d60632a
4
- data.tar.gz: ff991b1105a3c3b7827e5d7872ca55a648b692c6a67a67c144f5c458291adbe5
3
+ metadata.gz: 4049027dc5a512bb6f1225b179ac82989b995e8e77f0b384fce0154c58cd76c0
4
+ data.tar.gz: 0351324db2bb6a5d00d2585d7e2fb52d5f1f17860f19ec56e23bc848d411ce0f
5
5
  SHA512:
6
- metadata.gz: face725382f16dc7ae054efd68de804eb303ff566bbf6758aa88d09922a2d8747e78fc9b9b2a9d109d508a0af9bcea28dc919d7f2b70954661c349f5feaaad2a
7
- data.tar.gz: b9101b86d39ddc5ea9a427326cfd58834bd5d3015509d0411007f286ad34cd00f1113c43f6d20d1d01a2cc317340d905dc9bfb1b24bb2e2df8c6509ca897b00e
6
+ metadata.gz: 278e15de6e89c0a006c13da52ff42a326be704050ef905d8ad7c0541a8444359253923a9331747440f72fe2855ac7addf3af3b917c75c5a2775c052618aa62fb
7
+ data.tar.gz: 9ec95ddb14b3a4d6763dd7ddd0e40b9f5367a3943baad464890b64723747b849627cbb513e1d3b982640563f3c73e1f18d8a437470e6303853369c07bf9b8154
data/README.md CHANGED
@@ -11,7 +11,7 @@
11
11
 
12
12
  This gem supports the following versions of Ruby and Rails:
13
13
 
14
- * Ruby 3.1.0+
14
+ * Ruby 3.3.0+
15
15
  * Rails 6.0.0+
16
16
 
17
17
  ## Usage
@@ -169,6 +169,24 @@ The headers passed to `complete` describe the cached representation. They can di
169
169
 
170
170
  `abort` is idempotent and releases an owned fill lock through `ResponseBank.release_lock`. Its default implementation is a no-op. The existing `write_to_cache` hook remains responsible for cleanup after a write attempt. An integration that releases those fills from `write_to_cache` should also implement `release_lock` for abandoned fills and failures that happen before the write hook. A key-only lock cannot prevent an old fill from releasing a replacement lock after its lease expires; integrations that need that guarantee must use owner tokens in their lock implementation.
171
171
 
172
+ ## Cache Entry Metadata
173
+
174
+ Applications can store a Hash inside a cache entry by setting
175
+ `env['cacheable.metadata']` before the response is stored:
176
+
177
+ ```ruby
178
+ env['cacheable.metadata'] = { 'variant' => 'b' }
179
+ ```
180
+
181
+ On a server cache hit, `env['cacheable.metadata']` holds the Hash stored with the
182
+ served entry, stale or fresh; the key is removed when the entry has none. The Hash
183
+ is stored once and shared by every request served from the entry, so it must not
184
+ contain anything visitor-specific. It is serialized with MessagePack (String keys on
185
+ read) and nested under `ResponseBank::APP_METADATA_KEY`, so it cannot collide with
186
+ ResponseBank's own slots. Keep it to plain MessagePack types: a Hash that does not
187
+ survive a MessagePack round-trip, because a value cannot be serialized or the nesting
188
+ is too deep for the unpacker, is logged and dropped, and the response is cached without it.
189
+
172
190
  ## Brotli Splice Slots
173
191
 
174
192
  Applications that need per-request replacement inside cached Brotli HTML responses can pass an injector builder to `ResponseBank::Middleware`:
@@ -3,4 +3,11 @@
3
3
  module ResponseBank
4
4
  CACHEABLE_HEADERS = ["Location", "Content-Type", "ETag", "Content-Encoding", "Last-Modified", "Cache-Control", "Expires", "Link", "Surrogate-Keys", "Cache-Tags", "Speculation-Rules"].freeze
5
5
  CACHEABLE_STATUSES = [200, 404, 301].freeze
6
+
7
+ # Application metadata stored inside the cache entry: set `env[METADATA_ENV_KEY]`
8
+ # (a Hash) before the response is stored; on a server cache hit it holds the Hash
9
+ # stored with the served entry.
10
+ METADATA_ENV_KEY = 'cacheable.metadata'
11
+ # Keeps application metadata apart from ResponseBank's own slots.
12
+ APP_METADATA_KEY = 'app'
6
13
  end
@@ -157,9 +157,34 @@ module ResponseBank
157
157
  end
158
158
  cached_headers = representation_headers.slice(*ResponseBank::CACHEABLE_HEADERS)
159
159
  data = [status, cached_headers, stored.compressed_body, timestamp, env['cacheable.compression_level']]
160
- data << stored.metadata if stored.metadata
160
+ metadata = entry_metadata(env, stored.metadata)
161
+ data << metadata if metadata
161
162
  data
162
163
  end
164
+
165
+ # Application metadata never fails the write, nor the reads that follow: anything
166
+ # that is not a Hash, or that does not round-trip through MessagePack, is logged
167
+ # and left out of the entry.
168
+ def entry_metadata(env, metadata)
169
+ app_metadata = env[ResponseBank::METADATA_ENV_KEY]
170
+ return metadata if app_metadata.nil?
171
+ unless app_metadata.is_a?(Hash)
172
+ ResponseBank.log("Ignoring #{ResponseBank::METADATA_ENV_KEY}: expected a Hash, got #{app_metadata.class}")
173
+ return metadata
174
+ end
175
+
176
+ merged = (metadata || {}).merge(ResponseBank::APP_METADATA_KEY => app_metadata)
177
+ begin
178
+ # Loading catches what dumping accepts but the reader rejects, such as nesting
179
+ # past the unpacker's stack; the array puts the Hash at its depth in the entry.
180
+ MessagePack.load(MessagePack.dump([merged]))
181
+ rescue StandardError => error
182
+ ResponseBank.log("Ignoring #{ResponseBank::METADATA_ENV_KEY}: #{error.class} - #{error.message}")
183
+ return metadata
184
+ end
185
+
186
+ merged
187
+ end
163
188
  end
164
189
  end
165
190
  end
@@ -176,6 +176,14 @@ module ResponseBank
176
176
  ResponseBank.log("Cache hit, but missing content-encoding in the cache value headers, maybe because of 301 or 404 response or empty body string")
177
177
  end
178
178
 
179
+ # After decompression and splicing, so a refill never inherits the rejected
180
+ # entry's metadata; cleared when the served entry has none.
181
+ if metadata&.key?(ResponseBank::APP_METADATA_KEY)
182
+ @env[ResponseBank::METADATA_ENV_KEY] = metadata[ResponseBank::APP_METADATA_KEY]
183
+ else
184
+ @env.delete(ResponseBank::METADATA_ENV_KEY)
185
+ end
186
+
179
187
  [status, @headers, [body]]
180
188
 
181
189
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ResponseBank
3
- VERSION = "1.5.0"
3
+ VERSION = "1.6.0"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: response_bank
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Lütke
@@ -169,14 +169,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
169
169
  requirements:
170
170
  - - ">="
171
171
  - !ruby/object:Gem::Version
172
- version: 3.1.0
172
+ version: 3.3.0
173
173
  required_rubygems_version: !ruby/object:Gem::Requirement
174
174
  requirements:
175
175
  - - ">="
176
176
  - !ruby/object:Gem::Version
177
177
  version: '0'
178
178
  requirements: []
179
- rubygems_version: 4.0.19
179
+ rubygems_version: 4.0.21
180
180
  specification_version: 4
181
181
  summary: Simple response caching for Ruby applications
182
182
  test_files: []