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
@@ -0,0 +1,358 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Redis
4
+ module Commands
5
+ module Arrays
6
+ # Set one or more contiguous values starting at an index in an array.
7
+ #
8
+ # When multiple values are given they are stored at consecutive indices
9
+ # beginning at `index`. Writing past the current end of the array
10
+ # extends it; slots that are skipped over stay empty. Only non-negative
11
+ # indices are valid.
12
+ #
13
+ # @example Set two values at the head of the array
14
+ # redis.arset("foo", 0, "a", "b")
15
+ # # => 2
16
+ # @example Overwrite an existing slot (no new slot is filled)
17
+ # redis.arset("foo", 1, "B")
18
+ # # => 0
19
+ #
20
+ # @param [String] key
21
+ # @param [Integer] index zero-based index at which to start writing
22
+ # @param [String] values one or more values stored at consecutive
23
+ # indices beginning at `index`
24
+ # @return [Integer] the number of previously empty slots that were set
25
+ def arset(key, index, *values)
26
+ send_command([:arset, key, Integer(index), *values])
27
+ end
28
+
29
+ # Get the value at an index in an array.
30
+ #
31
+ # @example
32
+ # redis.arget("foo", 0)
33
+ # # => "a"
34
+ # @example A missing key, an empty slot or an index past the end
35
+ # redis.arget("foo", 99)
36
+ # # => nil
37
+ #
38
+ # @param [String] key
39
+ # @param [Integer] index zero-based index of the element to retrieve
40
+ # @return [String, nil] the value at the given index, or `nil` if the
41
+ # key does not exist or the index holds no value
42
+ def arget(key, index)
43
+ send_command([:arget, key, Integer(index)])
44
+ end
45
+
46
+ # Set multiple index-value pairs in an array.
47
+ #
48
+ # Pairs may be non-contiguous and given in any order, as a flat list or
49
+ # a Hash.
50
+ #
51
+ # @example With a flat list of pairs
52
+ # redis.armset("foo", 0, "a", 5, "f")
53
+ # # => 2
54
+ # @example With an array (flat, or one array per pair)
55
+ # redis.armset("foo", [0, "a"], [5, "f"])
56
+ # # => 2
57
+ # @example With a Hash
58
+ # redis.armset("foo", { 0 => "a", 5 => "f" })
59
+ # # => 2
60
+ #
61
+ # @param [String] key
62
+ # @param [Integer, String, Array<Integer, String>, Array<Array(Integer, String)>,
63
+ # Hash{Integer => String}] pairs index-value pairs — as alternating
64
+ # index/value arguments, a single flat array, one array per pair, or a Hash
65
+ # @return [Integer] the number of previously empty slots that were set
66
+ def armset(key, *pairs)
67
+ pairs = if pairs.size == 1 && pairs.first.is_a?(Hash)
68
+ pairs.first.flatten
69
+ else
70
+ pairs.flatten(1)
71
+ end
72
+ raise ArgumentError, "wrong number of arguments" if pairs.empty? || pairs.size.odd?
73
+
74
+ args = pairs.each_slice(2).flat_map { |index, value| [Integer(index), value] }
75
+ send_command([:armset, key, *args])
76
+ end
77
+
78
+ # Get values at multiple indices in an array.
79
+ #
80
+ # The reply preserves the order of the requested indices and contains
81
+ # `nil` for any index that is not set.
82
+ #
83
+ # @example
84
+ # redis.armget("foo", 0, 1, 9)
85
+ # # => ["a", "b", nil]
86
+ # @example With an array of indices
87
+ # redis.armget("foo", [0, 1, 9])
88
+ # # => ["a", "b", nil]
89
+ #
90
+ # @param [String] key
91
+ # @param [Integer, Array<Integer>] indices one or more zero-based indices
92
+ # @return [Array<String, nil>] the values at the requested indices
93
+ def armget(key, *indices)
94
+ indices = indices.flatten(1).map { |index| Integer(index) }
95
+ send_command([:armget, key, *indices])
96
+ end
97
+
98
+ # Get values in a range of indices.
99
+ #
100
+ # Empty slots inside the range are returned as `nil`. When `start` is
101
+ # greater than `stop`, elements are returned in reverse index order.
102
+ #
103
+ # @example
104
+ # redis.argetrange("foo", 0, 2)
105
+ # # => ["a", nil, "c"]
106
+ # @example Reverse order
107
+ # redis.argetrange("foo", 2, 0)
108
+ # # => ["c", nil, "a"]
109
+ #
110
+ # @param [String] key
111
+ # @param [Integer] start zero-based index of the first element (inclusive)
112
+ # @param [Integer] stop zero-based index of the last element (inclusive)
113
+ # @return [Array<String, nil>] the values in traversal order
114
+ def argetrange(key, start, stop)
115
+ send_command([:argetrange, key, Integer(start), Integer(stop)])
116
+ end
117
+
118
+ # Get the length of an array (max index + 1).
119
+ #
120
+ # @param [String] key
121
+ # @return [Integer] the array length, or 0 if the key does not exist
122
+ def arlen(key)
123
+ send_command([:arlen, key])
124
+ end
125
+
126
+ # Get the number of non-empty elements in an array.
127
+ #
128
+ # @param [String] key
129
+ # @return [Integer] the number of set elements, or 0 if the key does not exist
130
+ def arcount(key)
131
+ send_command([:arcount, key])
132
+ end
133
+
134
+ # Delete elements at the specified indices in an array.
135
+ #
136
+ # Deleting an index that is not set counts as zero elements deleted and
137
+ # does not modify the array.
138
+ #
139
+ # @param [String] key
140
+ # @param [Integer, Array<Integer>] indices one or more zero-based indices to delete
141
+ # @return [Integer] the number of elements deleted
142
+ def ardel(key, *indices)
143
+ indices = indices.flatten(1).map { |index| Integer(index) }
144
+ send_command([:ardel, key, *indices])
145
+ end
146
+
147
+ # Delete elements in one or more inclusive index ranges.
148
+ #
149
+ # Ranges may overlap; each element is counted at most once. A range
150
+ # given with `start > stop` is processed in ascending order regardless.
151
+ #
152
+ # @example Delete two ranges
153
+ # redis.ardelrange("foo", 0, 2, 5, 7)
154
+ # # => 6
155
+ # @example Ranges as pairs
156
+ # redis.ardelrange("foo", [0, 2], [5, 7])
157
+ # # => 6
158
+ #
159
+ # @param [String] key
160
+ # @param [Integer, Array<Integer>] ranges one or more start/stop pairs,
161
+ # as flat alternating integers or one array per range
162
+ # @return [Integer] the number of elements deleted
163
+ def ardelrange(key, *ranges)
164
+ ranges = ranges.flatten(1).map { |index| Integer(index) }
165
+ raise ArgumentError, "ranges must be given as start/stop pairs" if ranges.empty? || ranges.size.odd?
166
+
167
+ send_command([:ardelrange, key, *ranges])
168
+ end
169
+
170
+ # Insert one or more values at consecutive indices, beginning at the
171
+ # array's insert cursor. The cursor advances by one for each value.
172
+ #
173
+ # @see #arnext inspect the current cursor position
174
+ # @see #arseek reposition the cursor
175
+ #
176
+ # @param [String] key
177
+ # @param [String] values one or more values to insert
178
+ # @return [Integer] the last index where a value was inserted
179
+ def arinsert(key, *values)
180
+ send_command([:arinsert, key, *values])
181
+ end
182
+
183
+ # Set the insert cursor of an array to a specific index.
184
+ #
185
+ # @param [String] key
186
+ # @param [Integer] index zero-based index for the new cursor position
187
+ # @return [Boolean] whether the cursor was set (`false` if the key does not exist)
188
+ def arseek(key, index)
189
+ send_command([:arseek, key, Integer(index)], &Boolify)
190
+ end
191
+
192
+ # Get the next index {#arinsert} would use.
193
+ #
194
+ # @param [String] key
195
+ # @return [Integer, nil] the next insert index (0 for missing keys or
196
+ # before any insert), or `nil` when the insertion cursor is exhausted
197
+ def arnext(key)
198
+ send_command([:arnext, key])
199
+ end
200
+
201
+ # Get the most recently inserted elements.
202
+ #
203
+ # @param [String] key
204
+ # @param [Integer] count maximum number of elements to return; if the
205
+ # array holds fewer elements, all of them are returned
206
+ # @param [Boolean] rev return elements most recent first instead of the
207
+ # default oldest-first order
208
+ # @return [Array<String>] the most recently inserted elements
209
+ def arlastitems(key, count, rev: false)
210
+ args = [:arlastitems, key, Integer(count)]
211
+ args << "REV" if rev
212
+ send_command(args)
213
+ end
214
+
215
+ # Insert one or more values into an array used as a fixed-size ring
216
+ # buffer. Each value is placed at the next position in the ring and the
217
+ # cursor advances, wrapping around to index 0 once the ring is full.
218
+ #
219
+ # @param [String] key
220
+ # @param [Integer] size the size of the ring buffer window
221
+ # @param [String] values one or more values to insert
222
+ # @return [Integer] the last index where a value was inserted
223
+ def arring(key, size, *values)
224
+ send_command([:arring, key, Integer(size), *values])
225
+ end
226
+
227
+ # Iterate existing elements in an index range.
228
+ #
229
+ # Empty slots are excluded. When `start` is greater than `stop` the
230
+ # iteration is reversed.
231
+ #
232
+ # @example
233
+ # redis.arscan("foo", 0, 9)
234
+ # # => [[0, "a"], [3, "d"]]
235
+ #
236
+ # @param [String] key
237
+ # @param [Integer] start zero-based start index (inclusive)
238
+ # @param [Integer] stop zero-based end index (inclusive)
239
+ # @param [Integer] limit cap on the number of elements returned;
240
+ # all populated elements in range are returned when omitted
241
+ # @return [Array<Array(Integer, String)>] `[index, value]` pairs in
242
+ # traversal order; empty when the key does not exist
243
+ def arscan(key, start, stop, limit: nil)
244
+ args = [:arscan, key, Integer(start), Integer(stop)]
245
+ args << "LIMIT" << Integer(limit) if limit
246
+ send_command(args)
247
+ end
248
+
249
+ # Search array elements within an inclusive index range using one or
250
+ # more textual predicates. Empty slots are skipped.
251
+ #
252
+ # Each predicate keyword accepts a single value or an array of values;
253
+ # multiple predicates are combined with `logic:` (`:or` by default).
254
+ #
255
+ # @example Substring search
256
+ # redis.argrep("foo", 0, 9, match: "an")
257
+ # # => [1, 2]
258
+ # @example Combined predicates with values
259
+ # redis.argrep("foo", 0, 9, glob: "a*", exact: "cherry", logic: :or, with_values: true)
260
+ # # => [[0, "apple"], [2, "cherry"]]
261
+ #
262
+ # @param [String] key
263
+ # @param [Integer, String] start zero-based start index (inclusive), or
264
+ # `"-"` for the start of the array; iteration is reversed when greater
265
+ # than `stop`
266
+ # @param [Integer, String] stop zero-based end index (inclusive), or
267
+ # `"+"` for the end of the array
268
+ # @param [String, Array<String>] exact match by exact equality
269
+ # @param [String, Array<String>] match match by substring
270
+ # @param [String, Array<String>] glob match by glob-style pattern (`*`, `?`, `[...]`)
271
+ # @param [String, Array<String>] re match by regular expression
272
+ # @param [Symbol] logic `:and` or `:or` — how multiple predicates combine (server default is OR)
273
+ # @param [Integer] limit stop after this many matches
274
+ # @param [Boolean] with_values return `[index, value]` pairs instead of indices
275
+ # @param [Boolean] nocase case-insensitive comparison for all predicates
276
+ # @return [Array<Integer>, Array<Array(Integer, String)>] matching
277
+ # indices in traversal order, or `[index, value]` pairs with `with_values`
278
+ def argrep(key, start, stop, exact: nil, match: nil, glob: nil, re: nil,
279
+ logic: nil, limit: nil, with_values: nil, nocase: nil)
280
+ args = [:argrep, key, argrep_bound(start), argrep_bound(stop)]
281
+ { "EXACT" => exact, "MATCH" => match, "GLOB" => glob, "RE" => re }.each do |predicate, values|
282
+ Array(values).each { |value| args << predicate << value }
283
+ end
284
+ if logic
285
+ operator = logic.to_s.upcase
286
+ raise ArgumentError, "logic must be :and or :or" unless %w[AND OR].include?(operator)
287
+
288
+ args << operator
289
+ end
290
+ args << "LIMIT" << Integer(limit) if limit
291
+ args << "WITHVALUES" if with_values
292
+ args << "NOCASE" if nocase
293
+ send_command(args)
294
+ end
295
+
296
+ # Perform an aggregate operation on the non-empty elements in a range.
297
+ #
298
+ # Supported operations: `:sum`, `:min`, `:max` (numeric, returned as
299
+ # Float), `:and`, `:or`, `:xor` (bitwise, floats truncated toward
300
+ # zero), `:match` (count of elements equal to `value`) and `:used`
301
+ # (count of non-empty elements).
302
+ #
303
+ # @example
304
+ # redis.arop("foo", 0, 9, :sum)
305
+ # # => 6.0
306
+ # @example Count elements equal to a value
307
+ # redis.arop("foo", 0, 9, :match, value: "2")
308
+ # # => 1
309
+ #
310
+ # @param [String] key
311
+ # @param [Integer] start zero-based index of the first element (inclusive);
312
+ # the range is always scanned from the lower to the higher index
313
+ # @param [Integer] stop zero-based index of the last element (inclusive)
314
+ # @param [Symbol, String] operation one of `:sum`, `:min`, `:max`,
315
+ # `:and`, `:or`, `:xor`, `:match`, `:used`
316
+ # @param [String] value the value to compare against (required for `:match`)
317
+ # @return [Float, Integer, nil] the aggregate result — a Float for
318
+ # `:sum`/`:min`/`:max`, an Integer otherwise; `nil` when no elements
319
+ # qualify
320
+ def arop(key, start, stop, operation, value: nil)
321
+ operation = operation.to_s.upcase
322
+ raise ArgumentError, "value is required for the MATCH operation" if operation == "MATCH" && value.nil?
323
+
324
+ args = [:arop, key, Integer(start), Integer(stop), operation]
325
+ args << value if operation == "MATCH"
326
+
327
+ if %w[SUM MIN MAX].include?(operation)
328
+ send_command(args, &Floatify)
329
+ else
330
+ send_command(args)
331
+ end
332
+ end
333
+
334
+ # Get metadata about an array.
335
+ #
336
+ # @param [String] key
337
+ # @param [Boolean] full include per-slice statistics
338
+ # @return [Hash{String => Integer, Float}] metadata fields such as
339
+ # `count`, `len`, `next-insert-index` and `slices`; with `full:` the
340
+ # `avg-*` slice statistics are returned as `Float`
341
+ # @raise [Redis::CommandError] when the key does not exist
342
+ def arinfo(key, full: false)
343
+ args = [:arinfo, key]
344
+ args << "FULL" if full
345
+ send_command(args, &HashifyArrayInfo)
346
+ end
347
+
348
+ private
349
+
350
+ # ARGREP accepts "-" / "+" as full-range bounds — unlike the other AR*
351
+ # range commands, which take numeric indices only (verified on Redis
352
+ # 8.8). Anything else is coerced so typos still fail fast client-side.
353
+ def argrep_bound(index)
354
+ index == "-" || index == "+" ? index : Integer(index)
355
+ end
356
+ end
357
+ end
358
+ end
@@ -68,6 +68,30 @@ class Redis
68
68
  command << scale if scale
69
69
  send_command(command)
70
70
  end
71
+
72
+ # Return the offsets of the set (or clear) bits in a range of the string value stored at key.
73
+ #
74
+ # @example Offsets of the set bits in the whole string
75
+ # redis.bitrangeintarray("key", 0, -1) # => [1, 7, 9, 30]
76
+ # @example Clear bits in a bit range, at most two of them
77
+ # redis.bitrangeintarray("key", 24, 31, scale: :bit, zeros: true, limit: 2) # => [24, 25]
78
+ #
79
+ # @param [String] key
80
+ # @param [Integer] start start index
81
+ # @param [Integer] stop stop index
82
+ # @param [String, Symbol] scale the scale of the offset range
83
+ # e.g. 'BYTE' - interpreted as a range of bytes, 'BIT' - interpreted as a range of bits
84
+ # @param [Integer] limit return at most this many offsets; in byte scale the byte
85
+ # holding the last match is completed, so a few more may be returned
86
+ # @param [Boolean] zeros return the offsets of the clear bits instead of the set bits
87
+ # @return [Array<Integer>] the matching bit offsets in ascending order
88
+ def bitrangeintarray(key, start, stop, scale: nil, limit: nil, zeros: false)
89
+ command = [:bitrangeintarray, key, start, stop]
90
+ command << scale if scale
91
+ command << "LIMIT" << limit if limit
92
+ command << "ZEROS" if zeros
93
+ send_command(command)
94
+ end
71
95
  end
72
96
  end
73
97
  end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Redis
4
+ module Commands
5
+ # The `BLESS` command family (Redis 8.12): per-key protection flags that
6
+ # shield a key from memory-pressure actions. The only flag so far is
7
+ # `NO-EVICT`, which excludes the key from the eviction policy.
8
+ #
9
+ # Flags are passed to the server verbatim: no client-side validation or
10
+ # normalization is done, so spell them the way the server does
11
+ # (`"NO-EVICT"`). An unknown flag is rejected by the server with a
12
+ # {Redis::CommandError}.
13
+ module Bless
14
+ # Return the active protection flags of a key.
15
+ #
16
+ # @example
17
+ # redis.set("foo", "bar")
18
+ # redis.bless_set("foo", "NO-EVICT")
19
+ # redis.bless_get("foo")
20
+ # # => ["NO-EVICT"]
21
+ #
22
+ # @param [String] key
23
+ # @return [Array<String>] the key's active flags (e.g. `"NO-EVICT"`);
24
+ # empty when none are set
25
+ def bless_get(key)
26
+ send_command([:bless, "GET", key])
27
+ end
28
+
29
+ # Add a protection flag to a key.
30
+ #
31
+ # @example
32
+ # redis.bless_set("foo", "NO-EVICT")
33
+ # # => true
34
+ # redis.bless_set("foo", "NO-EVICT")
35
+ # # => false
36
+ #
37
+ # @param [String] key
38
+ # @param [String] flag the protection flag, e.g. `"NO-EVICT"`
39
+ # @return [Boolean] whether the key's flag set changed (`false` if the
40
+ # flag was already set)
41
+ def bless_set(key, flag)
42
+ send_command([:bless, "SET", key, flag], &Boolify)
43
+ end
44
+
45
+ # Remove a protection flag from a key.
46
+ #
47
+ # @example
48
+ # redis.bless_clear("foo", "NO-EVICT")
49
+ # # => true
50
+ # redis.bless_clear("foo", "NO-EVICT")
51
+ # # => false
52
+ #
53
+ # @param [String] key
54
+ # @param [String] flag the protection flag, e.g. `"NO-EVICT"`
55
+ # @return [Boolean] whether the key's flag set changed (`false` if the
56
+ # flag was already clear)
57
+ def bless_clear(key, flag)
58
+ send_command([:bless, "CLEAR", key, flag], &Boolify)
59
+ end
60
+
61
+ # Incrementally iterate the keys of the current database that carry the
62
+ # given protection flag. Like `SCAN`, a full iteration is complete when
63
+ # the returned cursor is `"0"`.
64
+ #
65
+ # @example Retrieve the first batch of keys blessed with NO-EVICT
66
+ # redis.bless_scan(0, "NO-EVICT")
67
+ # # => ["17", ["key:1", "key:2"]]
68
+ # @example Hint the batch size
69
+ # redis.bless_scan(17, "NO-EVICT", count: 100)
70
+ # # => ["0", ["key:3"]]
71
+ #
72
+ # @param [String, Integer] cursor the cursor of the iteration
73
+ # @param [String] flag the protection flag, e.g. `"NO-EVICT"`
74
+ # @param [Hash] options
75
+ # @option options [Integer] :count return about this many keys per call
76
+ # @return [Array(String, Array<String>)] the next cursor and the keys found
77
+ #
78
+ # @note On `Redis::Cluster` the driver walks the nodes one by one, as it
79
+ # does for `SCAN`, encoding the node index into the returned cursor, so
80
+ # a full iteration covers every shard. Inside `pipelined`/`multi` the
81
+ # command is sent to a single node instead and only that node's keys
82
+ # are returned.
83
+ def bless_scan(cursor, flag, count: nil)
84
+ args = [:bless, "SCAN", cursor, flag]
85
+ args << "COUNT" << Integer(count) if count
86
+
87
+ send_command(args)
88
+ end
89
+
90
+ # Iterate every key of the current database that carries the given
91
+ # protection flag, driving {#bless_scan} until the cursor wraps around.
92
+ #
93
+ # @example Collect all keys blessed with NO-EVICT (with possible duplicates)
94
+ # redis.bless_scan_each("NO-EVICT").to_a
95
+ # # => ["key:1", "key:2", "key:3"]
96
+ # @example Execute a block for each blessed key
97
+ # redis.bless_scan_each("NO-EVICT", count: 100) { |key| puts key }
98
+ #
99
+ # @param [String] flag the protection flag, e.g. `"NO-EVICT"`
100
+ # @param [Hash] options
101
+ # @option options [Integer] :count return about this many keys per call
102
+ # @return [Enumerator] an enumerator over all found keys when no block is given
103
+ def bless_scan_each(flag, **options, &block)
104
+ return to_enum(:bless_scan_each, flag, **options) unless block_given?
105
+
106
+ cursor = 0
107
+ loop do
108
+ cursor, keys = bless_scan(cursor, flag, **options)
109
+ keys.each(&block)
110
+ break if cursor == "0"
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -8,11 +8,18 @@ class Redis
8
8
  # @see https://redis.io/commands#cluster Reference of cluster command
9
9
  #
10
10
  # @param subcommand [String, Symbol] the subcommand of cluster command
11
- # e.g. `:slots`, `:nodes`, `:slaves`, `:info`
11
+ # e.g. `:slots`, `:nodes`, `:slaves`, `:info`, `:'slot-stats'`
12
+ #
13
+ # @example Per-slot usage stats for a range (Redis 8.2)
14
+ # redis.cluster('slot-stats', 'SLOTSRANGE', 0, 100)
15
+ # # => [[0, {"key-count" => 1}], [1, {"key-count" => 0}], ...]
16
+ # @example Top slots by key count (Redis 8.2)
17
+ # redis.cluster('slot-stats', 'ORDERBY', 'key-count', 'LIMIT', 3, 'DESC')
12
18
  #
13
19
  # @return [Object] depends on the subcommand
14
20
  def cluster(subcommand, *args)
15
- send_command([:cluster, subcommand] + args)
21
+ block = subcommand.to_s.downcase == 'slot-stats' ? HashifyClusterSlotStats : Noop
22
+ send_command([:cluster, subcommand] + args, &block)
16
23
  end
17
24
 
18
25
  # Sends `ASKING` command to random node and returns its reply.