redis 6.0.0 → 6.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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +114 -0
  3. data/README.md +172 -5
  4. data/lib/redis/client.rb +5 -1
  5. data/lib/redis/commands/arrays.rb +358 -0
  6. data/lib/redis/commands/bitmaps.rb +24 -0
  7. data/lib/redis/commands/bless.rb +115 -0
  8. data/lib/redis/commands/cluster.rb +9 -2
  9. data/lib/redis/commands/hashes.rb +250 -7
  10. data/lib/redis/commands/keys.rb +48 -11
  11. data/lib/redis/commands/modules/json.rb +27 -2
  12. data/lib/redis/commands/modules/search/aggregation.rb +2 -1
  13. data/lib/redis/commands/modules/search/field.rb +11 -2
  14. data/lib/redis/commands/modules/search/hybrid.rb +3 -0
  15. data/lib/redis/commands/modules/search/index.rb +2 -1
  16. data/lib/redis/commands/modules/search/miscellaneous.rb +2 -1
  17. data/lib/redis/commands/modules/search/query.rb +2 -2
  18. data/lib/redis/commands/modules/search/schema.rb +4 -0
  19. data/lib/redis/commands/scripting.rb +109 -0
  20. data/lib/redis/commands/server.rb +158 -7
  21. data/lib/redis/commands/sets.rb +270 -0
  22. data/lib/redis/commands/sorted_sets.rb +131 -6
  23. data/lib/redis/commands/streams.rb +284 -33
  24. data/lib/redis/commands/strings.rb +232 -6
  25. data/lib/redis/commands/transactions.rb +6 -1
  26. data/lib/redis/commands/vector_sets.rb +407 -0
  27. data/lib/redis/commands.rb +169 -6
  28. data/lib/redis/distributed.rb +539 -22
  29. data/lib/redis/hash_ring.rb +2 -1
  30. data/lib/redis/keyspace_notifications/channels.rb +121 -0
  31. data/lib/redis/keyspace_notifications/manager.rb +1121 -0
  32. data/lib/redis/keyspace_notifications/notification.rb +95 -0
  33. data/lib/redis/keyspace_notifications/parser.rb +170 -0
  34. data/lib/redis/keyspace_notifications.rb +59 -0
  35. data/lib/redis/pipeline.rb +17 -5
  36. data/lib/redis/ractorize.rb +6 -0
  37. data/lib/redis/subscribe.rb +80 -2
  38. data/lib/redis/version.rb +1 -1
  39. data/lib/redis/xxh3.rb +354 -0
  40. data/lib/redis.rb +29 -8
  41. metadata +16 -6
@@ -66,6 +66,91 @@ class Redis
66
66
  send_command([:incrbyfloat, key, Float(increment)], &Floatify)
67
67
  end
68
68
 
69
+ # Increment the numeric value of a key atomically, with optional bounds
70
+ # and expiration control. Uses 0 as the initial value if the key does
71
+ # not exist.
72
+ #
73
+ # Unlike `incr`/`incrby`, the reply is a two-element array of the new
74
+ # value and the increment that was actually applied. When the result
75
+ # would fall outside `lbound:`/`ubound:` (or the type limits) the
76
+ # operation is skipped and the reply is `[current_value, 0]`; with
77
+ # `saturate: true` the result is capped at the bound instead and the
78
+ # second element reflects the saturated delta.
79
+ #
80
+ # @example Increment by 1 (integer mode default)
81
+ # redis.increx("counter")
82
+ # # => [1, 1]
83
+ # @example Window counter rate limiter: cap at 100, TTL only on window creation
84
+ # value, applied = redis.increx("ratelimit:#{user_id}", by: 1, ubound: 100, ex: 60, enx: true)
85
+ # reject_request if applied == 0
86
+ # @example Float mode — selected by the Ruby type of `by:`
87
+ # redis.increx("temp", by: 0.25)
88
+ # # => [1.75, 0.25]
89
+ #
90
+ # @param [String] key
91
+ # @param [Integer, Float] by the increment (may be negative). The Ruby
92
+ # type selects the mode: an `Integer` is sent as `BYINT` (requires an
93
+ # integer-typed stored value, replies with Integers), a `Float` as
94
+ # `BYFLOAT` (stored value may be integer or float, replies with
95
+ # Floats) — so `by: 5` and `by: 5.0` behave differently. Other types
96
+ # raise `TypeError`. Without `by:`, increments by 1 in integer mode.
97
+ # @param [Integer, Float] lbound lower bound for the result. In float
98
+ # mode any numeric is accepted; in integer mode it must be an
99
+ # `Integer` (a `Float` raises `TypeError` rather than silently
100
+ # truncating, since bounds decide whether the increment applies)
101
+ # @param [Integer, Float] ubound upper bound for the result, with the
102
+ # same typing rules as `lbound`
103
+ # @param [Boolean] saturate cap/floor an out-of-bounds result at the
104
+ # bound instead of skipping the operation
105
+ # @param [Integer] ex expiration in seconds
106
+ # @param [Integer] px expiration in milliseconds
107
+ # @param [Integer] exat expiration as a Unix timestamp in seconds
108
+ # @param [Integer] pxat expiration as a Unix timestamp in milliseconds
109
+ # @param [Boolean] persist remove the key's expiration
110
+ # @param [Boolean] enx only set the expiration when the key has no TTL
111
+ # (the increment is applied regardless); requires one of
112
+ # `ex`/`px`/`exat`/`pxat`
113
+ # @return [Array(Integer, Integer), Array(Float, Float)]
114
+ # `[new_value, applied_increment]` — Integers in integer mode, Floats
115
+ # in float mode (under RESP2 and RESP3 alike); `applied_increment`
116
+ # is 0 when the operation was skipped as out of bounds
117
+ def increx(key, by: nil, lbound: nil, ubound: nil, saturate: nil,
118
+ ex: nil, px: nil, exat: nil, pxat: nil, persist: nil, enx: nil)
119
+ if [ex, px, exat, pxat, persist].count { |option| option } > 1
120
+ raise ArgumentError, "ex, px, exat, pxat and persist are mutually exclusive"
121
+ end
122
+ raise ArgumentError, "enx is incompatible with persist" if enx && persist
123
+ raise ArgumentError, "enx requires one of ex, px, exat or pxat" if enx && !(ex || px || exat || pxat)
124
+
125
+ # The Ruby type of `by` selects the wire mode; anything else would
126
+ # have to silently pick a mode, so it is rejected instead.
127
+ float_mode = case by
128
+ when nil, Integer then false
129
+ when Float then true
130
+ else raise TypeError, "by must be an Integer (BYINT) or a Float (BYFLOAT), got #{by.class}"
131
+ end
132
+
133
+ args = [:increx, key]
134
+ args << (float_mode ? "BYFLOAT" : "BYINT") << by if by
135
+ args << "LBOUND" << increx_bound(:lbound, lbound, float_mode) if lbound
136
+ args << "UBOUND" << increx_bound(:ubound, ubound, float_mode) if ubound
137
+ args << "SATURATE" if saturate
138
+ args << "EX" << Integer(ex) if ex
139
+ args << "PX" << Integer(px) if px
140
+ args << "EXAT" << Integer(exat) if exat
141
+ args << "PXAT" << Integer(pxat) if pxat
142
+ args << "PERSIST" if persist
143
+ args << "ENX" if enx
144
+
145
+ if float_mode
146
+ # RESP2 replies with bulk strings, RESP3 with native doubles;
147
+ # converge both on Float.
148
+ send_command(args) { |reply| reply.is_a?(Array) ? reply.map(&Floatify) : reply }
149
+ else
150
+ send_command(args)
151
+ end
152
+ end
153
+
69
154
  # Set the string value of a key.
70
155
  #
71
156
  # @param [String] key
@@ -75,12 +160,35 @@ class Redis
75
160
  # - `:px => Integer`: Set the specified expire time, in milliseconds.
76
161
  # - `:exat => Integer` : Set the specified Unix time at which the key will expire, in seconds.
77
162
  # - `:pxat => Integer` : Set the specified Unix time at which the key will expire, in milliseconds.
78
- # - `:nx => true`: Only set the key if it does not already exist.
79
- # - `:xx => true`: Only set the key if it already exist.
163
+ # - `:nx => true`: Only set the key if it does not already exist. Mutually exclusive with
164
+ # `:xx`, `:ifeq`, `:ifne`, `:ifdeq`, and `:ifdne`.
165
+ # - `:xx => true`: Only set the key if it already exist. Mutually exclusive with `:nx`,
166
+ # `:ifeq`, `:ifne`, `:ifdeq`, and `:ifdne`.
167
+ # - `:ifeq => String`: Only set the key if its current value equals the given value.
168
+ # Mutually exclusive with `:nx`, `:xx`, `:ifne`, `:ifdeq`, and `:ifdne`.
169
+ # - `:ifne => String`: Only set the key if its current value does not equal the given
170
+ # value. Mutually exclusive with `:nx`, `:xx`, `:ifeq`, `:ifdeq`, and `:ifdne`.
171
+ # - `:ifdeq => String`: Only set the key if the digest of its current value (see #digest)
172
+ # equals the given digest. Mutually exclusive with `:nx`, `:xx`, `:ifeq`, `:ifne`, and
173
+ # `:ifdne`.
174
+ # - `:ifdne => String`: Only set the key if the digest of its current value does not
175
+ # equal the given digest. Mutually exclusive with `:nx`, `:xx`, `:ifeq`, `:ifne`, and
176
+ # `:ifdeq`.
80
177
  # - `:keepttl => true`: Retain the time to live associated with the key.
81
- # - `:get => true`: Return the old string stored at key, or nil if key did not exist.
82
- # @return [String, Boolean] `"OK"` or true, false if `:nx => true` or `:xx => true`
83
- def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil, get: nil)
178
+ # - `:get => true`: Return the old string stored at key, or nil if key did not exist,
179
+ # regardless of whether `:nx`/`:xx`/`:ifeq`/`:ifne`/`:ifdeq`/`:ifdne` was satisfied.
180
+ # @return [String, Boolean] `"OK"`, or true/false if `:nx`, `:xx`, `:ifeq`, `:ifne`,
181
+ # `:ifdeq`, or `:ifdne` is given without `:get`
182
+ def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil,
183
+ ifeq: nil, ifne: nil, ifdeq: nil, ifdne: nil, keepttl: nil, get: nil)
184
+ # nx/xx are pure flags (only truthiness matters); ifeq/ifne/ifdeq/ifdne are
185
+ # value-bearing, so a deliberately-passed `false` (e.g. comparing against the
186
+ # literal string "false") must count as given — only `nil` means "not given".
187
+ active_conditions = [nx, xx, !ifeq.nil?, !ifne.nil?, !ifdeq.nil?, !ifdne.nil?]
188
+ if active_conditions.count { |active| active } > 1
189
+ raise ArgumentError, "nx, xx, ifeq, ifne, ifdeq, and ifdne are mutually exclusive"
190
+ end
191
+
84
192
  args = [:set, key, value.to_s]
85
193
  args << "EX" << Integer(ex) if ex
86
194
  args << "PX" << Integer(px) if px
@@ -88,16 +196,61 @@ class Redis
88
196
  args << "PXAT" << Integer(pxat) if pxat
89
197
  args << "NX" if nx
90
198
  args << "XX" if xx
199
+ args << "IFEQ" << ifeq.to_s unless ifeq.nil?
200
+ args << "IFNE" << ifne.to_s unless ifne.nil?
201
+ args << "IFDEQ" << ifdeq.to_s unless ifdeq.nil?
202
+ args << "IFDNE" << ifdne.to_s unless ifdne.nil?
91
203
  args << "KEEPTTL" if keepttl
92
204
  args << "GET" if get
93
205
 
94
- if nx || xx
206
+ # GET always returns the previous value (or nil) verbatim; boolifying it would
207
+ # both mangle a previous value that happened to be "OK" and turn a "key didn't
208
+ # exist" nil into a misleading false.
209
+ if !get && active_conditions.any?
95
210
  send_command(args, &BoolifySet)
96
211
  else
97
212
  send_command(args)
98
213
  end
99
214
  end
100
215
 
216
+ # Delete a key, optionally only if its current value or value digest matches a condition.
217
+ #
218
+ # @example Without options
219
+ # redis.delex("foo")
220
+ # # => 1
221
+ # @example With IFEQ
222
+ # redis.delex("foo", ifeq: "bar")
223
+ # # => 1 (if the current value of "foo" was "bar"), 0 otherwise
224
+ #
225
+ # @param [String] key
226
+ # @param [Hash] options
227
+ # - `:ifeq => String`: Only delete the key if its current value equals the given value.
228
+ # Mutually exclusive with `:ifne`, `:ifdeq`, and `:ifdne`.
229
+ # - `:ifne => String`: Only delete the key if its current value does not equal the given
230
+ # value. Mutually exclusive with `:ifeq`, `:ifdeq`, and `:ifdne`.
231
+ # - `:ifdeq => String`: Only delete the key if the digest of its current value (see
232
+ # #digest) equals the given digest. Mutually exclusive with `:ifeq`, `:ifne`, and
233
+ # `:ifdne`.
234
+ # - `:ifdne => String`: Only delete the key if the digest of its current value does not
235
+ # equal the given digest. Mutually exclusive with `:ifeq`, `:ifne`, and `:ifdeq`.
236
+ # @return [Integer] `1` if the key was deleted, `0` if it did not exist or the condition
237
+ # was not met
238
+ def delex(key, ifeq: nil, ifne: nil, ifdeq: nil, ifdne: nil)
239
+ # Value-bearing options: a deliberately-passed `false` must count as given —
240
+ # only `nil` means "not given" (see #set).
241
+ if [ifeq, ifne, ifdeq, ifdne].count { |option| !option.nil? } > 1
242
+ raise ArgumentError, "ifeq, ifne, ifdeq, and ifdne are mutually exclusive"
243
+ end
244
+
245
+ args = [:delex, key]
246
+ args << "IFEQ" << ifeq.to_s unless ifeq.nil?
247
+ args << "IFNE" << ifne.to_s unless ifne.nil?
248
+ args << "IFDEQ" << ifdeq.to_s unless ifdeq.nil?
249
+ args << "IFDNE" << ifdne.to_s unless ifdne.nil?
250
+
251
+ send_command(args)
252
+ end
253
+
101
254
  # Set the time to live in seconds of a key.
102
255
  #
103
256
  # @param [String] key
@@ -301,6 +454,22 @@ class Redis
301
454
  send_command(args)
302
455
  end
303
456
 
457
+ # Compute a digest of the string value stored at a key, suitable for later comparison
458
+ # via SET's `:ifdeq`/`:ifdne` or DELEX's `:ifdeq`/`:ifdne` options without transmitting
459
+ # the full value.
460
+ #
461
+ # @example
462
+ # redis.set("foo", "bar")
463
+ # redis.digest("foo")
464
+ # # => "<hex digest>"
465
+ #
466
+ # @param [String] key
467
+ # @return [String, nil] the digest of the value stored at key, as a 16-character
468
+ # lowercase hex String, or `nil` if the key does not exist
469
+ def digest(key)
470
+ send_command([:digest, key])
471
+ end
472
+
304
473
  # Get the length of the value stored in a key.
305
474
  #
306
475
  # @param [String] key
@@ -309,6 +478,63 @@ class Redis
309
478
  def strlen(key)
310
479
  send_command([:strlen, key])
311
480
  end
481
+
482
+ # Find the longest common subsequence of the strings stored at two keys.
483
+ #
484
+ # Matching characters need not be contiguous: the LCS of "foo" and "fao" is "fo".
485
+ # Runs in O(N*M) on the server, so keep the strings small.
486
+ #
487
+ # @example Get the subsequence
488
+ # redis.mset("key1", "ohmytext", "key2", "mynewtext")
489
+ # redis.lcs("key1", "key2")
490
+ # # => "mytext"
491
+ # @example Get only its length
492
+ # redis.lcs("key1", "key2", len: true)
493
+ # # => 6
494
+ # @example Get the match positions, last match first
495
+ # redis.lcs("key1", "key2", idx: true)
496
+ # # => { "matches" => [[[4, 7], [5, 8]], [[2, 3], [0, 1]]], "len" => 6 }
497
+ # @example Only matches of at least 4 characters, each with its length
498
+ # redis.lcs("key1", "key2", idx: true, minmatchlen: 4, withmatchlen: true)
499
+ # # => { "matches" => [[[4, 7], [5, 8], 4]], "len" => 6 }
500
+ #
501
+ # @param [String] key1
502
+ # @param [String] key2
503
+ # @param [Hash] options
504
+ # - `:len => true`: return the length of the subsequence instead of the subsequence
505
+ # - `:idx => true`: return the match positions instead of the subsequence
506
+ # - `:minmatchlen => Integer`: (`idx`) only report matches at least this long
507
+ # - `:withmatchlen => true`: (`idx`) append each match's length to its entry
508
+ #
509
+ # @return [String, Integer, Hash]
510
+ # - by default, the subsequence (`""` when there is none)
511
+ # - with `:len`, its length
512
+ # - with `:idx`, `{ "matches" => [[[start1, end1], [start2, end2], (len)], ...], "len" => Integer }`
513
+ def lcs(key1, key2, len: false, idx: false, minmatchlen: nil, withmatchlen: false)
514
+ args = [:lcs, key1, key2]
515
+ args << "LEN" if len
516
+ args << "IDX" if idx
517
+ args << "MINMATCHLEN" << Integer(minmatchlen) if minmatchlen
518
+ args << "WITHMATCHLEN" if withmatchlen
519
+
520
+ if idx
521
+ send_command(args, &Hashify)
522
+ else
523
+ send_command(args)
524
+ end
525
+ end
526
+
527
+ private
528
+
529
+ # INCREX bounds decide whether the increment is applied at all, so a
530
+ # Float bound in integer mode must raise rather than silently truncate
531
+ # (`Integer(2.9)` => 2) — mirroring how `by:` selects the mode strictly.
532
+ def increx_bound(name, value, float_mode)
533
+ return Float(value) if float_mode
534
+ raise TypeError, "#{name} must be an Integer in integer mode, got #{value.class}" unless value.is_a?(Integer)
535
+
536
+ value
537
+ end
312
538
  end
313
539
  end
314
540
  end
@@ -23,9 +23,12 @@ class Redis
23
23
  def multi
24
24
  synchronize do |client|
25
25
  client.multi do |raw_transaction|
26
- yield MultiConnection.new(raw_transaction)
26
+ yield MultiConnection.new(raw_transaction, client: client)
27
27
  end
28
28
  end
29
+ rescue ::RedisClient::Error => error
30
+ # With sentinels, the client is a plain RedisClient that doesn't translate its own errors.
31
+ Client.translate_error!(error)
29
32
  end
30
33
 
31
34
  # Watch the given keys to determine execution of the MULTI/EXEC block.
@@ -75,6 +78,8 @@ class Redis
75
78
  res
76
79
  end
77
80
  end
81
+ rescue ::RedisClient::Error => error
82
+ Client.translate_error!(error)
78
83
  end
79
84
 
80
85
  # Forget about all watched keys.