dalli 5.0.16 → 5.1.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 +4 -4
- data/CHANGELOG.md +48 -142
- data/Gemfile +1 -1
- data/README.md +7 -33
- data/lib/dalli/client.rb +275 -116
- data/lib/dalli/compressor.rb +5 -53
- data/lib/dalli/key_manager.rb +2 -23
- data/lib/dalli/options.rb +1 -1
- data/lib/dalli/pipelined_deleter.rb +29 -12
- data/lib/dalli/pipelined_getter.rb +74 -52
- data/lib/dalli/pipelined_setter.rb +22 -19
- data/lib/dalli/protocol/base.rb +64 -151
- data/lib/dalli/protocol/connection_manager.rb +13 -139
- data/lib/dalli/protocol/key_regularizer.rb +18 -1
- data/lib/dalli/protocol/meta.rb +99 -117
- data/lib/dalli/protocol/request_formatter.rb +110 -50
- data/lib/dalli/protocol/response_processor.rb +42 -93
- data/lib/dalli/protocol/string_marshaller.rb +1 -3
- data/lib/dalli/protocol/value_compressor.rb +7 -36
- data/lib/dalli/protocol/value_marshaller.rb +0 -5
- data/lib/dalli/protocol/value_serializer.rb +1 -1
- data/lib/dalli/protocol.rb +9 -7
- data/lib/dalli/socket.rb +9 -87
- data/lib/dalli/version.rb +2 -2
- data/lib/dalli.rb +0 -5
- data/lib/rack/session/dalli.rb +2 -1
- metadata +3 -7
- data/lib/dalli/json_serializer.rb +0 -28
data/lib/dalli/client.rb
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'digest/md5'
|
|
4
4
|
|
|
5
|
+
# encoding: ascii
|
|
5
6
|
module Dalli
|
|
6
7
|
##
|
|
7
8
|
# Dalli::Client is the main class which developers will use to interact with
|
|
@@ -35,30 +36,21 @@ module Dalli
|
|
|
35
36
|
# to memcached. Default: true.
|
|
36
37
|
# - :compression_min_size - the minimum size (in bytes) for which Dalli will compress values sent to Memcached.
|
|
37
38
|
# Defaults to 4K.
|
|
38
|
-
# - :value_max_bytes - largest value Dalli will write, 1MB by default.
|
|
39
|
-
# - :max_response_bytes - largest value accepted from a server. Defaults to :value_max_bytes (1MB by default).
|
|
40
|
-
# Set this explicitly when reading larger items written by another client.
|
|
41
39
|
# - :serializer - defaults to Marshal
|
|
42
40
|
# - :compressor - defaults to Dalli::Compressor, a Zlib-based implementation
|
|
43
41
|
# - :cache_nils - defaults to false, if true Dalli will not treat cached nil values as 'not found' for
|
|
44
42
|
# #fetch operations.
|
|
45
43
|
# - :raw - If set, disables serialization and compression entirely at the client level.
|
|
46
44
|
# Only String values are supported. This is useful when the caller handles its own
|
|
47
|
-
# serialization (e.g., Rails' ActiveSupport::Cache).
|
|
48
|
-
#
|
|
49
|
-
#
|
|
50
|
-
# Dalli::MarshalError.
|
|
45
|
+
# serialization (e.g., Rails' ActiveSupport::Cache). Note: this is different from
|
|
46
|
+
# the per-request :raw option which converts values to strings but still uses the
|
|
47
|
+
# serialization pipeline.
|
|
51
48
|
# - :digest_class - defaults to Digest::MD5, allows you to pass in an object that responds to the hexdigest method,
|
|
52
49
|
# useful for injecting a FIPS compliant hash object.
|
|
53
50
|
# - :otel_db_statement - controls the +db.query.text+ span attribute when OpenTelemetry is loaded.
|
|
54
51
|
# +:include+ logs the full operation and key(s), +:obfuscate+ replaces keys with "?",
|
|
55
52
|
# +nil+ (default) omits the attribute entirely.
|
|
56
53
|
# - :otel_peer_service - when set, adds a +peer.service+ span attribute with this value for logical service naming.
|
|
57
|
-
# - :verify_reply_keys - defaults to false. When true, get_multi, get_multi_cas and get_multi_with_metadata only
|
|
58
|
-
# accept replies for the keys they requested; after a reply naming another key, the rest of
|
|
59
|
-
# that server's replies are treated as misses. memcached can name an item by its raw key
|
|
60
|
-
# bytes when the item was created by auto-create from a base64-encoded key, which Dalli no
|
|
61
|
-
# longer does. Costs a multi-get about 15%.
|
|
62
54
|
#
|
|
63
55
|
def initialize(servers = nil, options = {})
|
|
64
56
|
@normalized_servers = ::Dalli::ServersArgNormalizer.normalize_servers(servers)
|
|
@@ -76,6 +68,7 @@ module Dalli
|
|
|
76
68
|
# Get the value associated with the key.
|
|
77
69
|
# If a value is not found, then +nil+ is returned.
|
|
78
70
|
def get(key, req_options = nil)
|
|
71
|
+
validate_routing_tokens!(req_options)
|
|
79
72
|
perform(:get, key, req_options)
|
|
80
73
|
end
|
|
81
74
|
|
|
@@ -83,8 +76,9 @@ module Dalli
|
|
|
83
76
|
# Gat (get and touch) fetch an item and simultaneously update its expiration time.
|
|
84
77
|
#
|
|
85
78
|
# If a value is not found, then +nil+ is returned.
|
|
86
|
-
def gat(key, ttl = nil)
|
|
87
|
-
|
|
79
|
+
def gat(key, ttl = nil, req_options = nil)
|
|
80
|
+
validate_routing_tokens!(req_options)
|
|
81
|
+
perform(:gat, key, ttl_or_default(ttl), req_options)
|
|
88
82
|
end
|
|
89
83
|
|
|
90
84
|
##
|
|
@@ -99,8 +93,9 @@ module Dalli
|
|
|
99
93
|
##
|
|
100
94
|
# Get the value and CAS ID associated with the key. If a block is provided,
|
|
101
95
|
# value and CAS will be passed to the block.
|
|
102
|
-
def get_cas(key)
|
|
103
|
-
(
|
|
96
|
+
def get_cas(key, req_options = nil)
|
|
97
|
+
validate_routing_tokens!(req_options)
|
|
98
|
+
(value, cas) = perform(:cas, key, req_options)
|
|
104
99
|
return [value, cas] unless block_given?
|
|
105
100
|
|
|
106
101
|
yield value, cas
|
|
@@ -114,17 +109,23 @@ module Dalli
|
|
|
114
109
|
# - :return_cas [Boolean] return the CAS value (default: true)
|
|
115
110
|
# - :return_hit_status [Boolean] return whether item was previously accessed
|
|
116
111
|
# - :return_last_access [Boolean] return seconds since last access
|
|
112
|
+
# - :return_ttl_remaining [Boolean] return seconds of TTL remaining (-1 if no TTL)
|
|
117
113
|
# - :skip_lru_bump [Boolean] don't bump LRU or update access stats
|
|
118
114
|
#
|
|
119
115
|
# @return [Hash] containing:
|
|
120
116
|
# - :value - the cached value (or nil on miss)
|
|
121
117
|
# - :cas - the CAS value
|
|
118
|
+
# - :miss - true when the key does not exist. Always present. Prefer it
|
|
119
|
+
# over a nil :value, which cannot distinguish a miss from a stored nil
|
|
120
|
+
# under cache_nils
|
|
122
121
|
# - :hit_before - true/false if previously accessed (only if return_hit_status: true)
|
|
123
122
|
# - :last_access - seconds since last access (only if return_last_access: true)
|
|
123
|
+
# - :ttl_remaining - seconds of TTL remaining, -1 when the item has no
|
|
124
|
+
# expiry (only if return_ttl_remaining: true)
|
|
124
125
|
#
|
|
125
126
|
# @example Get with hit status
|
|
126
127
|
# result = client.get_with_metadata('key', return_hit_status: true)
|
|
127
|
-
# # => { value: "data", cas: 123, hit_before: true }
|
|
128
|
+
# # => { value: "data", cas: 123, miss: false, hit_before: true }
|
|
128
129
|
#
|
|
129
130
|
# @example Get with all metadata without affecting LRU
|
|
130
131
|
# result = client.get_with_metadata('key',
|
|
@@ -135,18 +136,15 @@ module Dalli
|
|
|
135
136
|
# # => { value: "data", cas: 123, hit_before: true, last_access: 42 }
|
|
136
137
|
#
|
|
137
138
|
def get_with_metadata(key, options = {})
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
139
|
+
validate_routing_tokens!(options)
|
|
140
|
+
key = key.to_s
|
|
141
|
+
key = @key_manager.validate_key(key)
|
|
141
142
|
|
|
142
|
-
server = ring.server_for_key(
|
|
143
|
-
Instrumentation.trace('get_with_metadata', trace_attrs('get_with_metadata',
|
|
144
|
-
server.request(:meta_get,
|
|
143
|
+
server = ring.server_for_key(key)
|
|
144
|
+
Instrumentation.trace('get_with_metadata', trace_attrs('get_with_metadata', key, server)) do
|
|
145
|
+
server.request(:meta_get, key, options)
|
|
145
146
|
end
|
|
146
|
-
|
|
147
|
-
# is marked down is final, as in #perform; retrying it looped forever once
|
|
148
|
-
# down_retry_delay let the same server be tried again.
|
|
149
|
-
rescue RetryableNetworkError => e
|
|
147
|
+
rescue NetworkError => e
|
|
150
148
|
Dalli.logger.debug { e.inspect }
|
|
151
149
|
Dalli.logger.debug { 'retrying get_with_metadata with new server' }
|
|
152
150
|
retry
|
|
@@ -156,18 +154,81 @@ module Dalli
|
|
|
156
154
|
# Fetch multiple keys efficiently.
|
|
157
155
|
# If a block is given, yields key/value pairs one at a time.
|
|
158
156
|
# Otherwise returns a hash of { 'key' => 'value', 'key2' => 'value1' }
|
|
159
|
-
#
|
|
160
|
-
|
|
157
|
+
#
|
|
158
|
+
# `req_options` accepts :p_token/:l_token, applied to every key in the batch.
|
|
159
|
+
#
|
|
160
|
+
# A transient network error is retried automatically. If a server remains
|
|
161
|
+
# unreachable after retrying, raises Dalli::NetworkError rather than
|
|
162
|
+
# silently omitting that server's keys from the result.
|
|
163
|
+
#
|
|
164
|
+
# @raise [Dalli::NetworkError] if a server is unreachable after retrying
|
|
165
|
+
# rubocop:disable Style/ExplicitBlockArgument
|
|
166
|
+
def get_multi(*keys, req_options: nil)
|
|
161
167
|
keys.flatten!
|
|
162
168
|
keys.compact!
|
|
163
169
|
return {} if keys.empty?
|
|
164
170
|
|
|
171
|
+
validate_routing_tokens!(req_options)
|
|
172
|
+
|
|
165
173
|
if block_given?
|
|
166
|
-
get_multi_yielding(keys) { |k, v| yield k, v }
|
|
174
|
+
get_multi_yielding(keys, req_options) { |k, v| yield k, v }
|
|
167
175
|
else
|
|
168
|
-
get_multi_hash(keys)
|
|
176
|
+
get_multi_hash(keys, req_options)
|
|
169
177
|
end
|
|
170
178
|
end
|
|
179
|
+
# rubocop:enable Style/ExplicitBlockArgument
|
|
180
|
+
|
|
181
|
+
##
|
|
182
|
+
# Fetch multiple keys efficiently, returning a stale-aware metadata Hash per
|
|
183
|
+
# key. If a block is given, yields key/metadata pairs one at a time.
|
|
184
|
+
#
|
|
185
|
+
# Like #get_multi and #get_multi_cas, keys that were not found are omitted
|
|
186
|
+
# from the result -- absence is the miss. A tombstoned item (see #delete
|
|
187
|
+
# with the meta protocol's invalidate flag) is *not* a miss: it is returned
|
|
188
|
+
# with stale: true, possibly with an empty value, which is the distinction
|
|
189
|
+
# stale-aware callers need.
|
|
190
|
+
#
|
|
191
|
+
# client.get_multi_with_metadata('a', 'b', 'absent')
|
|
192
|
+
# # => { 'a' => { value: 'v', cas: 12, stale: false, miss: false },
|
|
193
|
+
# # 'b' => { value: '', cas: 13, stale: true, miss: false } }
|
|
194
|
+
#
|
|
195
|
+
# Missing keys are `requested - result.keys`.
|
|
196
|
+
#
|
|
197
|
+
# Result key order matches request order only when every key lands on the
|
|
198
|
+
# same server; across multiple servers it follows per-server response
|
|
199
|
+
# order instead, the same as #get_multi.
|
|
200
|
+
#
|
|
201
|
+
# `req_options` accepts :p_token/:l_token, applied to every key in the batch.
|
|
202
|
+
#
|
|
203
|
+
# @param keys [Array<String>] the keys to fetch
|
|
204
|
+
# @param req_options [Hash, nil] routing-token options
|
|
205
|
+
# @return [Hash] key => { value:, cas:, stale:, miss: }
|
|
206
|
+
def get_multi_with_metadata(*keys, req_options: nil, &block)
|
|
207
|
+
keys.flatten!
|
|
208
|
+
keys.compact!
|
|
209
|
+
return {} if keys.empty?
|
|
210
|
+
|
|
211
|
+
validate_routing_tokens!(req_options)
|
|
212
|
+
|
|
213
|
+
results = Instrumentation.trace('get_multi_with_metadata',
|
|
214
|
+
multi_trace_attrs('get_multi_with_metadata', keys.size, keys)) do
|
|
215
|
+
if ring.servers.size == 1
|
|
216
|
+
single_server_get_multi_with_metadata(keys, req_options)
|
|
217
|
+
else
|
|
218
|
+
pipelined_getter.process_with_metadata(keys, req_options)
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
if block
|
|
223
|
+
results.each(&block)
|
|
224
|
+
# Matches get_multi/get_multi_cas: nil when a block is given, so
|
|
225
|
+
# callers can't come to depend on a return value that block-form
|
|
226
|
+
# get_multi never provided.
|
|
227
|
+
return nil
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
results
|
|
231
|
+
end
|
|
171
232
|
|
|
172
233
|
##
|
|
173
234
|
# Fetch multiple keys efficiently, including available metadata such as CAS.
|
|
@@ -175,12 +236,16 @@ module Dalli
|
|
|
175
236
|
# [value, cas_id]
|
|
176
237
|
# If no block is given, returns a hash of
|
|
177
238
|
# { 'key' => [value, cas_id] }
|
|
178
|
-
|
|
239
|
+
#
|
|
240
|
+
# `req_options` accepts :p_token/:l_token, applied to every key in the batch.
|
|
241
|
+
def get_multi_cas(*keys, req_options: nil)
|
|
242
|
+
validate_routing_tokens!(req_options)
|
|
243
|
+
|
|
179
244
|
if block_given?
|
|
180
|
-
pipelined_getter.process(keys) { |*args| yield(*args) }
|
|
245
|
+
pipelined_getter.process(keys, req_options) { |*args| yield(*args) }
|
|
181
246
|
else
|
|
182
247
|
{}.tap do |hash|
|
|
183
|
-
pipelined_getter.process(keys) { |k, data| hash[k] = data }
|
|
248
|
+
pipelined_getter.process(keys, req_options) { |k, data| hash[k] = data }
|
|
184
249
|
end
|
|
185
250
|
end
|
|
186
251
|
end
|
|
@@ -236,19 +301,15 @@ module Dalli
|
|
|
236
301
|
def fetch_with_lock(key, ttl: nil, lock_ttl: 30, recache_threshold: nil, req_options: nil, &block)
|
|
237
302
|
raise ArgumentError, 'Block is required for fetch_with_lock' unless block_given?
|
|
238
303
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
validated_key = @key_manager.validate_key(key.to_s)
|
|
304
|
+
validate_routing_tokens!(req_options)
|
|
305
|
+
key = key.to_s
|
|
306
|
+
key = @key_manager.validate_key(key)
|
|
243
307
|
|
|
244
|
-
server = ring.server_for_key(
|
|
245
|
-
Instrumentation.trace('fetch_with_lock', trace_attrs('fetch_with_lock',
|
|
246
|
-
fetch_with_lock_request(
|
|
308
|
+
server = ring.server_for_key(key)
|
|
309
|
+
Instrumentation.trace('fetch_with_lock', trace_attrs('fetch_with_lock', key, server)) do
|
|
310
|
+
fetch_with_lock_request(key, ttl, lock_ttl, recache_threshold, req_options, &block)
|
|
247
311
|
end
|
|
248
|
-
|
|
249
|
-
# is marked down is final, as in #perform; retrying it looped forever once
|
|
250
|
-
# down_retry_delay let the same server be tried again.
|
|
251
|
-
rescue RetryableNetworkError => e
|
|
312
|
+
rescue NetworkError => e
|
|
252
313
|
Dalli.logger.debug { e.inspect }
|
|
253
314
|
Dalli.logger.debug { 'retrying fetch_with_lock with new server' }
|
|
254
315
|
retry
|
|
@@ -297,11 +358,6 @@ module Dalli
|
|
|
297
358
|
# Any error replies will be discarded at the end of the block, and
|
|
298
359
|
# Dalli client methods invoked inside the block will not
|
|
299
360
|
# have return values
|
|
300
|
-
#
|
|
301
|
-
# The block is quiet only for the thread or fiber that runs it. Another
|
|
302
|
-
# thread or fiber sharing this client can make requests meanwhile; a
|
|
303
|
-
# non-quiet one first sends the block's buffered requests and discards
|
|
304
|
-
# their replies, so it reads only its own.
|
|
305
361
|
def quiet
|
|
306
362
|
old = Thread.current[::Dalli::QUIET]
|
|
307
363
|
Thread.current[::Dalli::QUIET] = true
|
|
@@ -321,16 +377,24 @@ module Dalli
|
|
|
321
377
|
# This method is more efficient than calling set() in a loop because
|
|
322
378
|
# it batches requests by server and uses quiet mode.
|
|
323
379
|
#
|
|
380
|
+
# A transient network error is retried automatically. If a server remains
|
|
381
|
+
# unreachable after retrying, raises Dalli::NetworkError; keys already
|
|
382
|
+
# sent to other servers before the error are not rolled back.
|
|
383
|
+
#
|
|
324
384
|
# @param hash [Hash] key-value pairs to set
|
|
325
385
|
# @param ttl [Integer] time-to-live in seconds (optional, uses default if not provided)
|
|
326
|
-
# @param req_options [Hash] options passed to each set operation
|
|
386
|
+
# @param req_options [Hash] options passed to each set operation; accepts
|
|
387
|
+
# :p_token/:l_token, applied to every key in the batch
|
|
327
388
|
# @return [void]
|
|
389
|
+
# @raise [Dalli::NetworkError] if a server is unreachable after retrying
|
|
328
390
|
#
|
|
329
391
|
# Example:
|
|
330
392
|
# client.set_multi({ 'key1' => 'value1', 'key2' => 'value2' }, 300)
|
|
331
393
|
def set_multi(hash, ttl = nil, req_options = nil)
|
|
332
394
|
return if hash.empty?
|
|
333
395
|
|
|
396
|
+
validate_routing_tokens!(req_options)
|
|
397
|
+
|
|
334
398
|
Instrumentation.trace('set_multi', multi_trace_attrs('set_multi', hash.size, hash.keys)) do
|
|
335
399
|
if ring.servers.size == 1
|
|
336
400
|
single_server_set_multi(hash, ttl_or_default(ttl), req_options)
|
|
@@ -344,6 +408,7 @@ module Dalli
|
|
|
344
408
|
# Set the key-value pair, verifying existing CAS.
|
|
345
409
|
# Returns the resulting CAS value if succeeded, and falsy otherwise.
|
|
346
410
|
def set_cas(key, value, cas, ttl = nil, req_options = nil)
|
|
411
|
+
validate_routing_tokens!(req_options)
|
|
347
412
|
perform(:set, key, value, ttl_or_default(ttl), cas, req_options)
|
|
348
413
|
end
|
|
349
414
|
|
|
@@ -351,6 +416,7 @@ module Dalli
|
|
|
351
416
|
# Conditionally add a key/value pair, if the key does not already exist
|
|
352
417
|
# on the server. Returns truthy if the operation succeeded.
|
|
353
418
|
def add(key, value, ttl = nil, req_options = nil)
|
|
419
|
+
validate_routing_tokens!(req_options)
|
|
354
420
|
perform(:add, key, value, ttl_or_default(ttl), req_options)
|
|
355
421
|
end
|
|
356
422
|
|
|
@@ -366,17 +432,49 @@ module Dalli
|
|
|
366
432
|
# key already exists on the server. Returns the new CAS value if the
|
|
367
433
|
# operation succeeded, or falsy otherwise.
|
|
368
434
|
def replace_cas(key, value, cas, ttl = nil, req_options = nil)
|
|
435
|
+
validate_routing_tokens!(req_options)
|
|
369
436
|
perform(:replace, key, value, ttl_or_default(ttl), cas, req_options)
|
|
370
437
|
end
|
|
371
438
|
|
|
372
439
|
# Delete a key/value pair, verifying existing CAS.
|
|
373
440
|
# Returns true if succeeded, and falsy otherwise.
|
|
374
|
-
|
|
375
|
-
|
|
441
|
+
# Delete a key, optionally with a CAS check.
|
|
442
|
+
#
|
|
443
|
+
# `req_options` accepts the same meta-delete options as #delete.
|
|
444
|
+
def delete_cas(key, cas = 0, req_options = nil)
|
|
445
|
+
validate_delete_options!(req_options)
|
|
446
|
+
validate_routing_tokens!(req_options)
|
|
447
|
+
perform(:delete, key, cas, req_options)
|
|
376
448
|
end
|
|
377
449
|
|
|
378
|
-
|
|
379
|
-
|
|
450
|
+
##
|
|
451
|
+
# Delete a key.
|
|
452
|
+
#
|
|
453
|
+
# `req_options` may include the memcached meta-delete options:
|
|
454
|
+
#
|
|
455
|
+
# - `:invalidate` (Boolean) — mark the item stale instead of removing it.
|
|
456
|
+
# This is the tombstone: readers see `stale: true` from
|
|
457
|
+
# #get_with_metadata and #get_multi_with_metadata, and the existing value
|
|
458
|
+
# is still readable unless `:drop_value` is also set. A tombstoned key is
|
|
459
|
+
# *not* a miss, which lets a reader tell "another process is repopulating
|
|
460
|
+
# this" apart from "this was never here".
|
|
461
|
+
# - `:tombstone_ttl` (Integer seconds) — how long the stale marker lives.
|
|
462
|
+
# Requires `:invalidate`; memcached only honors the TTL on a delete when
|
|
463
|
+
# it accompanies the invalidate flag, so passing it alone raises
|
|
464
|
+
# ArgumentError rather than sending a request the server would treat
|
|
465
|
+
# differently than intended. Once it elapses, reads see a miss.
|
|
466
|
+
# - `:drop_value` (Boolean) — remove the item's value but leave the item, so
|
|
467
|
+
# a tombstone need not retain the old payload. On its own it is not a
|
|
468
|
+
# tombstone: reads are an ordinary hit with an empty value.
|
|
469
|
+
# - `:p_token`/`:l_token` (String) — opaque routing tokens for an
|
|
470
|
+
# intermediate proxy or router; see #get.
|
|
471
|
+
#
|
|
472
|
+
# dc.delete('key', invalidate: true, tombstone_ttl: 30, drop_value: true)
|
|
473
|
+
#
|
|
474
|
+
# @param key [String] the key to delete
|
|
475
|
+
# @param req_options [Hash, nil] meta-delete options
|
|
476
|
+
def delete(key, req_options = nil)
|
|
477
|
+
delete_cas(key, 0, req_options)
|
|
380
478
|
end
|
|
381
479
|
|
|
382
480
|
##
|
|
@@ -384,22 +482,35 @@ module Dalli
|
|
|
384
482
|
# This method is more efficient than calling delete() in a loop because
|
|
385
483
|
# it batches requests by server and uses quiet mode.
|
|
386
484
|
#
|
|
485
|
+
# `req_options` accepts the same meta-delete options as #delete and applies
|
|
486
|
+
# them to every key in the batch.
|
|
487
|
+
#
|
|
387
488
|
# @param keys [Array<String>] keys to delete
|
|
388
|
-
# @
|
|
389
|
-
#
|
|
390
|
-
#
|
|
391
|
-
#
|
|
489
|
+
# @param req_options [Hash, nil] meta-delete options
|
|
490
|
+
# @return [Integer] the number of keys the server found and acted on. Only a
|
|
491
|
+
# key that did not exist decrements this count, so with `:invalidate` it
|
|
492
|
+
# reports how many keys were tombstoned rather than removed -- the action
|
|
493
|
+
# is whichever one the caller asked for. This is best-effort: a transient
|
|
494
|
+
# network error is retried automatically, and keys handled before the
|
|
495
|
+
# error are not recounted, so the result may under-report when a retry
|
|
496
|
+
# occurs. If a server remains unreachable after retrying, raises
|
|
497
|
+
# Dalli::NetworkError.
|
|
498
|
+
# @raise [Dalli::NetworkError] if a server is unreachable after retrying
|
|
392
499
|
#
|
|
393
500
|
# Example:
|
|
394
501
|
# client.delete_multi(['key1', 'key2', 'key3'])
|
|
395
|
-
|
|
502
|
+
# client.delete_multi(%w[key1 key2], invalidate: true, tombstone_ttl: 30)
|
|
503
|
+
def delete_multi(keys, req_options = nil)
|
|
396
504
|
return 0 if keys.empty?
|
|
397
505
|
|
|
506
|
+
validate_delete_options!(req_options)
|
|
507
|
+
validate_routing_tokens!(req_options)
|
|
508
|
+
|
|
398
509
|
Instrumentation.trace('delete_multi', multi_trace_attrs('delete_multi', keys.size, keys)) do
|
|
399
510
|
if ring.servers.size == 1
|
|
400
|
-
single_server_delete_multi(keys)
|
|
511
|
+
single_server_delete_multi(keys, req_options)
|
|
401
512
|
else
|
|
402
|
-
pipelined_deleter.process(keys)
|
|
513
|
+
pipelined_deleter.process(keys, req_options)
|
|
403
514
|
end
|
|
404
515
|
end
|
|
405
516
|
end
|
|
@@ -407,22 +518,22 @@ module Dalli
|
|
|
407
518
|
##
|
|
408
519
|
# Append value to the value already stored on the server for 'key'.
|
|
409
520
|
# Appending only works for values stored with :raw => true.
|
|
410
|
-
def append(key, value)
|
|
411
|
-
|
|
521
|
+
def append(key, value, req_options = nil)
|
|
522
|
+
validate_routing_tokens!(req_options)
|
|
523
|
+
perform(:append, key, value.to_s, req_options)
|
|
412
524
|
end
|
|
413
525
|
|
|
414
526
|
##
|
|
415
527
|
# Prepend value to the value already stored on the server for 'key'.
|
|
416
528
|
# Prepending only works for values stored with :raw => true.
|
|
417
|
-
def prepend(key, value)
|
|
418
|
-
|
|
529
|
+
def prepend(key, value, req_options = nil)
|
|
530
|
+
validate_routing_tokens!(req_options)
|
|
531
|
+
perform(:prepend, key, value.to_s, req_options)
|
|
419
532
|
end
|
|
420
533
|
|
|
421
534
|
##
|
|
422
535
|
# Incr adds the given amount to the counter on the memcached server.
|
|
423
536
|
# Amt must be a positive integer value.
|
|
424
|
-
# Default, if given, must be an Integer (or a String of decimal digits);
|
|
425
|
-
# anything else raises ArgumentError.
|
|
426
537
|
#
|
|
427
538
|
# If default is nil, the counter must already exist or the operation
|
|
428
539
|
# will fail and will return nil. Otherwise this method will return
|
|
@@ -433,18 +544,16 @@ module Dalli
|
|
|
433
544
|
# #cas.
|
|
434
545
|
#
|
|
435
546
|
# If the value already exists, it must have been set with raw: true
|
|
436
|
-
def incr(key, amt = 1, ttl = nil, default = nil)
|
|
547
|
+
def incr(key, amt = 1, ttl = nil, default = nil, req_options = nil)
|
|
437
548
|
check_positive!(amt)
|
|
438
|
-
|
|
549
|
+
validate_routing_tokens!(req_options)
|
|
439
550
|
|
|
440
|
-
perform(:incr, key, amt.to_i, ttl_or_default(ttl), default)
|
|
551
|
+
perform(:incr, key, amt.to_i, ttl_or_default(ttl), default, req_options)
|
|
441
552
|
end
|
|
442
553
|
|
|
443
554
|
##
|
|
444
555
|
# Decr subtracts the given amount from the counter on the memcached server.
|
|
445
556
|
# Amt must be a positive integer value.
|
|
446
|
-
# Default, if given, must be an Integer (or a String of decimal digits);
|
|
447
|
-
# anything else raises ArgumentError.
|
|
448
557
|
#
|
|
449
558
|
# memcached counters are unsigned and cannot hold negative values. Calling
|
|
450
559
|
# decr on a counter which is 0 will just return 0.
|
|
@@ -458,11 +567,11 @@ module Dalli
|
|
|
458
567
|
# #cas.
|
|
459
568
|
#
|
|
460
569
|
# If the value already exists, it must have been set with raw: true
|
|
461
|
-
def decr(key, amt = 1, ttl = nil, default = nil)
|
|
570
|
+
def decr(key, amt = 1, ttl = nil, default = nil, req_options = nil)
|
|
462
571
|
check_positive!(amt)
|
|
463
|
-
|
|
572
|
+
validate_routing_tokens!(req_options)
|
|
464
573
|
|
|
465
|
-
perform(:decr, key, amt.to_i, ttl_or_default(ttl), default)
|
|
574
|
+
perform(:decr, key, amt.to_i, ttl_or_default(ttl), default, req_options)
|
|
466
575
|
end
|
|
467
576
|
|
|
468
577
|
##
|
|
@@ -539,6 +648,30 @@ module Dalli
|
|
|
539
648
|
|
|
540
649
|
private
|
|
541
650
|
|
|
651
|
+
# Raised before the request reaches a server: RequestFormatter enforces the
|
|
652
|
+
# same rule, but reaching it means unwinding through Protocol::Base#request,
|
|
653
|
+
# which logs the failure as unexpected and closes the connection. A caller
|
|
654
|
+
# passing the wrong options should get a clean ArgumentError and keep its
|
|
655
|
+
# connection.
|
|
656
|
+
def validate_delete_options!(req_options)
|
|
657
|
+
return unless req_options.is_a?(Hash)
|
|
658
|
+
|
|
659
|
+
tombstone_ttl = req_options[:tombstone_ttl]
|
|
660
|
+
return unless tombstone_ttl
|
|
661
|
+
|
|
662
|
+
raise ArgumentError, 'tombstone_ttl requires invalidate: true' unless req_options[:invalidate]
|
|
663
|
+
|
|
664
|
+
# tombstone_kwargs coerces this with Integer(), deep inside the request
|
|
665
|
+
# path; validated here first so a bad value raises cleanly instead of
|
|
666
|
+
# unwinding through Protocol::Base#request, which would close the
|
|
667
|
+
# connection on the ArgumentError Integer() raises.
|
|
668
|
+
begin
|
|
669
|
+
Integer(tombstone_ttl)
|
|
670
|
+
rescue ArgumentError, TypeError
|
|
671
|
+
raise ArgumentError, "tombstone_ttl must be an integer, got #{tombstone_ttl.inspect}"
|
|
672
|
+
end
|
|
673
|
+
end
|
|
674
|
+
|
|
542
675
|
def record_hit_miss_metrics(span, key_count, hit_count)
|
|
543
676
|
return unless span
|
|
544
677
|
|
|
@@ -546,10 +679,10 @@ module Dalli
|
|
|
546
679
|
'db.memcached.miss_count' => key_count - hit_count)
|
|
547
680
|
end
|
|
548
681
|
|
|
549
|
-
def get_multi_yielding(keys)
|
|
682
|
+
def get_multi_yielding(keys, req_options = nil)
|
|
550
683
|
Instrumentation.trace_with_result('get_multi', get_multi_attributes(keys)) do |span|
|
|
551
684
|
hit_count = 0
|
|
552
|
-
pipelined_getter.process(keys) do |k, data|
|
|
685
|
+
pipelined_getter.process(keys, req_options) do |k, data|
|
|
553
686
|
hit_count += 1
|
|
554
687
|
yield k, data.first
|
|
555
688
|
end
|
|
@@ -558,13 +691,13 @@ module Dalli
|
|
|
558
691
|
end
|
|
559
692
|
end
|
|
560
693
|
|
|
561
|
-
def get_multi_hash(keys)
|
|
694
|
+
def get_multi_hash(keys, req_options = nil)
|
|
562
695
|
Instrumentation.trace_with_result('get_multi', get_multi_attributes(keys)) do |span|
|
|
563
696
|
hash = if ring.servers.size == 1
|
|
564
|
-
single_server_get_multi(keys)
|
|
697
|
+
single_server_get_multi(keys, req_options)
|
|
565
698
|
else
|
|
566
699
|
{}.tap do |h|
|
|
567
|
-
pipelined_getter.process(keys) { |k, data| h[k] = data.first }
|
|
700
|
+
pipelined_getter.process(keys, req_options) { |k, data| h[k] = data.first }
|
|
568
701
|
end
|
|
569
702
|
end
|
|
570
703
|
record_hit_miss_metrics(span, keys.size, hash.size)
|
|
@@ -577,18 +710,37 @@ module Dalli
|
|
|
577
710
|
server if server&.alive?
|
|
578
711
|
end
|
|
579
712
|
|
|
580
|
-
|
|
713
|
+
# The three single_server_* fast-path methods below share one contract,
|
|
714
|
+
# matching the pipelined multi-server path they stand in for: a transient
|
|
715
|
+
# RetryableNetworkError is retried (bounded implicitly by the server's own
|
|
716
|
+
# socket_max_failures, same as the pipelined path's retry), and a hard
|
|
717
|
+
# NetworkError -- the server genuinely unreachable, not just blipping --
|
|
718
|
+
# propagates to the caller rather than being swallowed into a silently
|
|
719
|
+
# wrong result. Only server_for_key/single_server finding no live server
|
|
720
|
+
# to route to at all is still silent, matching Ring#keys_grouped_by_server
|
|
721
|
+
# dropping a key it can't route on both the single- and multi-server paths.
|
|
722
|
+
def single_server_get_multi(keys, req_options = nil)
|
|
581
723
|
keys.map! { |k| @key_manager.validate_key(k.to_s) }
|
|
582
724
|
return {} unless (server = single_server)
|
|
583
725
|
|
|
584
|
-
result = server.request(:read_multi_req, keys)
|
|
726
|
+
result = server.request(:read_multi_req, keys, req_options)
|
|
727
|
+
result.transform_keys! { |k| @key_manager.key_without_namespace(k) }
|
|
728
|
+
result
|
|
729
|
+
rescue Dalli::RetryableNetworkError => e
|
|
730
|
+
Dalli.logger.debug { e.inspect }
|
|
731
|
+
Dalli.logger.debug { 'retrying single-server get_multi because of network error' }
|
|
732
|
+
retry
|
|
733
|
+
end
|
|
734
|
+
|
|
735
|
+
def single_server_get_multi_with_metadata(keys, req_options = nil)
|
|
736
|
+
keys.map! { |k| @key_manager.validate_key(k.to_s) }
|
|
737
|
+
return {} unless (server = single_server)
|
|
738
|
+
|
|
739
|
+
result = server.request(:read_multi_with_metadata_req, keys, req_options)
|
|
585
740
|
result.transform_keys! { |k| @key_manager.key_without_namespace(k) }
|
|
586
741
|
result
|
|
587
742
|
rescue Dalli::NetworkError
|
|
588
743
|
{}
|
|
589
|
-
rescue Dalli::UnexpectedReplyKeyError => e
|
|
590
|
-
Dalli.logger.warn { e.message }
|
|
591
|
-
{}
|
|
592
744
|
end
|
|
593
745
|
|
|
594
746
|
def single_server_set_multi(hash, ttl, req_options)
|
|
@@ -596,24 +748,21 @@ module Dalli
|
|
|
596
748
|
return unless (server = single_server)
|
|
597
749
|
|
|
598
750
|
server.request(:write_multi_req, pairs, ttl, req_options)
|
|
599
|
-
rescue Dalli::
|
|
600
|
-
|
|
751
|
+
rescue Dalli::RetryableNetworkError => e
|
|
752
|
+
Dalli.logger.debug { e.inspect }
|
|
753
|
+
Dalli.logger.debug { 'retrying single-server set_multi because of network error' }
|
|
754
|
+
retry
|
|
601
755
|
end
|
|
602
756
|
|
|
603
|
-
def single_server_delete_multi(keys)
|
|
757
|
+
def single_server_delete_multi(keys, req_options = nil)
|
|
604
758
|
validated_keys = keys.map { |k| @key_manager.validate_key(k.to_s) }
|
|
605
759
|
return 0 unless (server = single_server)
|
|
606
760
|
|
|
607
|
-
server.request(:delete_multi_req, validated_keys)
|
|
761
|
+
server.request(:delete_multi_req, validated_keys, req_options)
|
|
608
762
|
rescue Dalli::RetryableNetworkError => e
|
|
609
|
-
# Mirror the pipelined path: retry transient errors so a momentary blip
|
|
610
|
-
# still yields a real count. Bounded by the server's socket_max_failures,
|
|
611
|
-
# after which a hard NetworkError is raised and handled below.
|
|
612
763
|
Dalli.logger.debug { e.inspect }
|
|
613
764
|
Dalli.logger.debug { 'retrying single-server delete_multi because of network error' }
|
|
614
765
|
retry
|
|
615
|
-
rescue Dalli::NetworkError
|
|
616
|
-
0
|
|
617
766
|
end
|
|
618
767
|
|
|
619
768
|
def get_multi_attributes(keys)
|
|
@@ -647,21 +796,32 @@ module Dalli
|
|
|
647
796
|
raise ArgumentError, "Positive values only: #{amt}" if amt.negative?
|
|
648
797
|
end
|
|
649
798
|
|
|
650
|
-
#
|
|
651
|
-
#
|
|
652
|
-
#
|
|
653
|
-
#
|
|
654
|
-
#
|
|
655
|
-
|
|
656
|
-
|
|
799
|
+
# Validated here, before the request reaches Protocol::Base#request, rather
|
|
800
|
+
# than only at the RequestFormatter level. Reaching only the formatter's
|
|
801
|
+
# check means unwinding through Protocol::Base#request, which logs the
|
|
802
|
+
# failure as unexpected and closes the connection -- a caller passing a
|
|
803
|
+
# bad token should get a clean ArgumentError and keep its connection.
|
|
804
|
+
ROUTING_TOKEN_FORBIDDEN = /[\r\n\0]/
|
|
805
|
+
private_constant :ROUTING_TOKEN_FORBIDDEN
|
|
657
806
|
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
807
|
+
def validate_routing_tokens!(req_options)
|
|
808
|
+
return unless req_options.is_a?(Hash)
|
|
809
|
+
|
|
810
|
+
validate_routing_token!(:p_token, req_options[:p_token])
|
|
811
|
+
validate_routing_token!(:l_token, req_options[:l_token])
|
|
812
|
+
end
|
|
813
|
+
|
|
814
|
+
def validate_routing_token!(name, value)
|
|
815
|
+
# Only an empty *String* is a no-op; see the matching comment in
|
|
816
|
+
# RequestFormatter#routing_tokens for why respond_to?(:empty?) is wrong
|
|
817
|
+
# here (it would also excuse [] / {} from the type check below).
|
|
818
|
+
return if value.nil? || (value.is_a?(String) && value.empty?)
|
|
819
|
+
raise ArgumentError, "#{name} must be a String, got #{value.class}" unless value.is_a?(String)
|
|
820
|
+
raise ArgumentError, "#{name} must not contain CRLF or null bytes" if value.match?(ROUTING_TOKEN_FORBIDDEN)
|
|
661
821
|
end
|
|
662
822
|
|
|
663
823
|
def cas_core(key, always_set, ttl = nil, req_options = nil)
|
|
664
|
-
|
|
824
|
+
validate_routing_tokens!(req_options)
|
|
665
825
|
(value, cas) = perform(:cas, key, req_options)
|
|
666
826
|
return if value.nil? && !always_set
|
|
667
827
|
|
|
@@ -671,8 +831,9 @@ module Dalli
|
|
|
671
831
|
|
|
672
832
|
def fetch_with_lock_request(key, ttl, lock_ttl, recache_threshold, req_options)
|
|
673
833
|
server = ring.server_for_key(key)
|
|
674
|
-
# req_options
|
|
675
|
-
#
|
|
834
|
+
# req_options is the base, not the override: fetch_with_lock's own
|
|
835
|
+
# lock_ttl/recache_threshold parameters must always win, even if a
|
|
836
|
+
# caller's req_options happened to contain :vivify_ttl/:recache_ttl.
|
|
676
837
|
meta_options = req_options.is_a?(Hash) ? req_options.dup : {}
|
|
677
838
|
meta_options[:vivify_ttl] = lock_ttl
|
|
678
839
|
meta_options[:recache_ttl] = recache_threshold
|
|
@@ -714,20 +875,18 @@ module Dalli
|
|
|
714
875
|
# rubocop:enable Naming/MethodParameterName
|
|
715
876
|
return yield if block_given?
|
|
716
877
|
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
# namespace a second time and send the retry to a different key.
|
|
720
|
-
validated_key = @key_manager.validate_key(key.to_s)
|
|
878
|
+
key = key.to_s
|
|
879
|
+
key = @key_manager.validate_key(key)
|
|
721
880
|
|
|
722
|
-
server = ring.server_for_key(
|
|
881
|
+
server = ring.server_for_key(key)
|
|
723
882
|
|
|
724
883
|
if Instrumentation.enabled?
|
|
725
884
|
op_name = op.name
|
|
726
|
-
Instrumentation.trace(op_name, trace_attrs(op_name,
|
|
727
|
-
server.request(op,
|
|
885
|
+
Instrumentation.trace(op_name, trace_attrs(op_name, key, server)) do
|
|
886
|
+
server.request(op, key, ...)
|
|
728
887
|
end
|
|
729
888
|
else
|
|
730
|
-
server.request(op,
|
|
889
|
+
server.request(op, key, ...)
|
|
731
890
|
end
|
|
732
891
|
rescue RetryableNetworkError => e
|
|
733
892
|
Dalli.logger.debug { e.inspect }
|