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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +114 -0
- data/README.md +172 -5
- data/lib/redis/client.rb +5 -1
- data/lib/redis/commands/arrays.rb +358 -0
- data/lib/redis/commands/bitmaps.rb +24 -0
- data/lib/redis/commands/bless.rb +115 -0
- data/lib/redis/commands/cluster.rb +9 -2
- data/lib/redis/commands/hashes.rb +250 -7
- data/lib/redis/commands/keys.rb +48 -11
- data/lib/redis/commands/modules/json.rb +27 -2
- data/lib/redis/commands/modules/search/aggregation.rb +2 -1
- data/lib/redis/commands/modules/search/field.rb +11 -2
- data/lib/redis/commands/modules/search/hybrid.rb +3 -0
- data/lib/redis/commands/modules/search/index.rb +2 -1
- data/lib/redis/commands/modules/search/miscellaneous.rb +2 -1
- data/lib/redis/commands/modules/search/query.rb +2 -2
- data/lib/redis/commands/modules/search/schema.rb +4 -0
- data/lib/redis/commands/scripting.rb +109 -0
- data/lib/redis/commands/server.rb +158 -7
- data/lib/redis/commands/sets.rb +270 -0
- data/lib/redis/commands/sorted_sets.rb +131 -6
- data/lib/redis/commands/streams.rb +284 -33
- data/lib/redis/commands/strings.rb +232 -6
- data/lib/redis/commands/transactions.rb +6 -1
- data/lib/redis/commands/vector_sets.rb +407 -0
- data/lib/redis/commands.rb +169 -6
- data/lib/redis/distributed.rb +539 -22
- data/lib/redis/hash_ring.rb +2 -1
- data/lib/redis/keyspace_notifications/channels.rb +121 -0
- data/lib/redis/keyspace_notifications/manager.rb +1121 -0
- data/lib/redis/keyspace_notifications/notification.rb +95 -0
- data/lib/redis/keyspace_notifications/parser.rb +170 -0
- data/lib/redis/keyspace_notifications.rb +59 -0
- data/lib/redis/pipeline.rb +17 -5
- data/lib/redis/ractorize.rb +6 -0
- data/lib/redis/subscribe.rb +80 -2
- data/lib/redis/version.rb +1 -1
- data/lib/redis/xxh3.rb +354 -0
- data/lib/redis.rb +29 -8
- metadata +16 -6
data/lib/redis/distributed.rb
CHANGED
|
@@ -100,8 +100,8 @@ class Redis
|
|
|
100
100
|
end
|
|
101
101
|
|
|
102
102
|
# Get information and statistics about the server.
|
|
103
|
-
def info(
|
|
104
|
-
on_each_node :info,
|
|
103
|
+
def info(*sections)
|
|
104
|
+
on_each_node :info, *sections
|
|
105
105
|
end
|
|
106
106
|
|
|
107
107
|
# Get the UNIX time stamp of the last successful save to disk.
|
|
@@ -114,11 +114,38 @@ class Redis
|
|
|
114
114
|
raise NotImplementedError
|
|
115
115
|
end
|
|
116
116
|
|
|
117
|
+
HOTKEYS_NOT_SUPPORTED = "HOTKEYS commands are not supported on Redis::Distributed: the tracking " \
|
|
118
|
+
"session is state of a single server. Call them on one node, " \
|
|
119
|
+
"e.g. distributed.nodes.first.hotkeys_start(...)."
|
|
120
|
+
private_constant :HOTKEYS_NOT_SUPPORTED
|
|
121
|
+
|
|
122
|
+
def hotkeys_start(**)
|
|
123
|
+
raise NotImplementedError, HOTKEYS_NOT_SUPPORTED
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def hotkeys_get
|
|
127
|
+
raise NotImplementedError, HOTKEYS_NOT_SUPPORTED
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def hotkeys_stop
|
|
131
|
+
raise NotImplementedError, HOTKEYS_NOT_SUPPORTED
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def hotkeys_reset
|
|
135
|
+
raise NotImplementedError, HOTKEYS_NOT_SUPPORTED
|
|
136
|
+
end
|
|
137
|
+
|
|
117
138
|
# Synchronously save the dataset to disk.
|
|
118
139
|
def save
|
|
119
140
|
on_each_node :save
|
|
120
141
|
end
|
|
121
142
|
|
|
143
|
+
# Block until each node has fsynced this ring's preceding writes to its AOF and/or its
|
|
144
|
+
# replicas'. Returns one `[local, replicas]` pair per node.
|
|
145
|
+
def waitaof(numlocal, numreplicas, timeout)
|
|
146
|
+
on_each_node :waitaof, numlocal, numreplicas, timeout
|
|
147
|
+
end
|
|
148
|
+
|
|
122
149
|
# Get server time: an UNIX timestamp and the elapsed microseconds in the current second.
|
|
123
150
|
def time
|
|
124
151
|
on_each_node :time
|
|
@@ -259,13 +286,18 @@ class Redis
|
|
|
259
286
|
|
|
260
287
|
# Sort the elements in a list, set or sorted set.
|
|
261
288
|
def sort(key, **options)
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
ensure_same_node(:sort, keys) do |node|
|
|
289
|
+
ensure_same_node(:sort, sort_keys(key, options)) do |node|
|
|
265
290
|
node.sort(key, **options)
|
|
266
291
|
end
|
|
267
292
|
end
|
|
268
293
|
|
|
294
|
+
# Sort the elements in a list, set or sorted set without storing the result.
|
|
295
|
+
def sort_ro(key, **options)
|
|
296
|
+
ensure_same_node(:sort_ro, sort_keys(key, options)) do |node|
|
|
297
|
+
node.sort_ro(key, **options)
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
|
|
269
301
|
# Determine the type stored at key.
|
|
270
302
|
def type(key)
|
|
271
303
|
node_for(key).type(key)
|
|
@@ -296,6 +328,12 @@ class Redis
|
|
|
296
328
|
node_for(key).incrbyfloat(key, increment)
|
|
297
329
|
end
|
|
298
330
|
|
|
331
|
+
# Increment the numeric value of a key atomically, with optional bounds
|
|
332
|
+
# and expiration control.
|
|
333
|
+
def increx(key, **options)
|
|
334
|
+
node_for(key).increx(key, **options)
|
|
335
|
+
end
|
|
336
|
+
|
|
299
337
|
# Set the string value of a key.
|
|
300
338
|
def set(key, value, **options)
|
|
301
339
|
node_for(key).set(key, value, **options)
|
|
@@ -349,6 +387,16 @@ class Redis
|
|
|
349
387
|
node_for(key).getex(key, **options)
|
|
350
388
|
end
|
|
351
389
|
|
|
390
|
+
# Delete a key, optionally only if its current value or value digest matches a condition.
|
|
391
|
+
def delex(key, **options)
|
|
392
|
+
node_for(key).delex(key, **options)
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
# Compute a digest of the string value stored at a key.
|
|
396
|
+
def digest(key)
|
|
397
|
+
node_for(key).digest(key)
|
|
398
|
+
end
|
|
399
|
+
|
|
352
400
|
# Set the JSON value at a path in the document stored under a key.
|
|
353
401
|
def json_set(key, path, value, **options)
|
|
354
402
|
node_for(key).json_set(key, path, value, **options)
|
|
@@ -524,6 +572,11 @@ class Redis
|
|
|
524
572
|
node_for(key).bitpos(key, bit, start, stop, scale: scale)
|
|
525
573
|
end
|
|
526
574
|
|
|
575
|
+
# Return the offsets of the set (or clear) bits in a range of the string value stored at key.
|
|
576
|
+
def bitrangeintarray(key, start, stop, scale: nil, limit: nil, zeros: false)
|
|
577
|
+
node_for(key).bitrangeintarray(key, start, stop, scale: scale, limit: limit, zeros: zeros)
|
|
578
|
+
end
|
|
579
|
+
|
|
527
580
|
# Set the string value of a key and return its old value.
|
|
528
581
|
def getset(key, value)
|
|
529
582
|
node_for(key).getset(key, value)
|
|
@@ -534,6 +587,13 @@ class Redis
|
|
|
534
587
|
node_for(key).strlen(key)
|
|
535
588
|
end
|
|
536
589
|
|
|
590
|
+
# Find the longest common subsequence of the strings stored at two keys.
|
|
591
|
+
def lcs(key1, key2, **options)
|
|
592
|
+
ensure_same_node(:lcs, [key1, key2]) do |node|
|
|
593
|
+
node.lcs(key1, key2, **options)
|
|
594
|
+
end
|
|
595
|
+
end
|
|
596
|
+
|
|
537
597
|
def [](key)
|
|
538
598
|
get(key)
|
|
539
599
|
end
|
|
@@ -728,6 +788,11 @@ class Redis
|
|
|
728
788
|
node_for(key).sadd?(key, *members)
|
|
729
789
|
end
|
|
730
790
|
|
|
791
|
+
# Add one or more members to a set and optionally set their expiration.
|
|
792
|
+
def saddex(key, *members, mnx: nil, mxx: nil, ex: nil, px: nil, exat: nil, pxat: nil, keepttl: false)
|
|
793
|
+
node_for(key).saddex(key, *members, mnx: mnx, mxx: mxx, ex: ex, px: px, exat: exat, pxat: pxat, keepttl: keepttl)
|
|
794
|
+
end
|
|
795
|
+
|
|
731
796
|
# Remove one or more members from a set.
|
|
732
797
|
def srem(key, *members)
|
|
733
798
|
node_for(key).srem(key, *members)
|
|
@@ -765,6 +830,55 @@ class Redis
|
|
|
765
830
|
node_for(key).smismember(key, *members)
|
|
766
831
|
end
|
|
767
832
|
|
|
833
|
+
# Set the time to live in seconds for one or more set members.
|
|
834
|
+
def sexpire(key, ttl, *members, nx: nil, xx: nil, gt: nil, lt: nil)
|
|
835
|
+
node_for(key).sexpire(key, ttl, *members, nx: nx, xx: xx, gt: gt, lt: lt)
|
|
836
|
+
end
|
|
837
|
+
|
|
838
|
+
# Set the time to live in milliseconds for one or more set members.
|
|
839
|
+
def spexpire(key, ttl, *members, nx: nil, xx: nil, gt: nil, lt: nil)
|
|
840
|
+
node_for(key).spexpire(key, ttl, *members, nx: nx, xx: xx, gt: gt, lt: lt)
|
|
841
|
+
end
|
|
842
|
+
|
|
843
|
+
# Set the expiration for one or more set members as an absolute Unix
|
|
844
|
+
# timestamp in seconds.
|
|
845
|
+
def sexpireat(key, unix_time_seconds, *members, nx: nil, xx: nil, gt: nil, lt: nil)
|
|
846
|
+
node_for(key).sexpireat(key, unix_time_seconds, *members, nx: nx, xx: xx, gt: gt, lt: lt)
|
|
847
|
+
end
|
|
848
|
+
|
|
849
|
+
# Set the expiration for one or more set members as an absolute Unix
|
|
850
|
+
# timestamp in milliseconds.
|
|
851
|
+
def spexpireat(key, unix_time_milliseconds, *members, nx: nil, xx: nil, gt: nil, lt: nil)
|
|
852
|
+
node_for(key).spexpireat(key, unix_time_milliseconds, *members, nx: nx, xx: xx, gt: gt, lt: lt)
|
|
853
|
+
end
|
|
854
|
+
|
|
855
|
+
# Get the time to live in seconds for one or more set members.
|
|
856
|
+
def sttl(key, *members)
|
|
857
|
+
node_for(key).sttl(key, *members)
|
|
858
|
+
end
|
|
859
|
+
|
|
860
|
+
# Get the time to live in milliseconds for one or more set members.
|
|
861
|
+
def spttl(key, *members)
|
|
862
|
+
node_for(key).spttl(key, *members)
|
|
863
|
+
end
|
|
864
|
+
|
|
865
|
+
# Get the expiration of one or more set members as an absolute Unix
|
|
866
|
+
# timestamp in seconds.
|
|
867
|
+
def sexpiretime(key, *members)
|
|
868
|
+
node_for(key).sexpiretime(key, *members)
|
|
869
|
+
end
|
|
870
|
+
|
|
871
|
+
# Get the expiration of one or more set members as an absolute Unix
|
|
872
|
+
# timestamp in milliseconds.
|
|
873
|
+
def spexpiretime(key, *members)
|
|
874
|
+
node_for(key).spexpiretime(key, *members)
|
|
875
|
+
end
|
|
876
|
+
|
|
877
|
+
# Remove the expiration from one or more set members.
|
|
878
|
+
def spersist(key, *members)
|
|
879
|
+
node_for(key).spersist(key, *members)
|
|
880
|
+
end
|
|
881
|
+
|
|
768
882
|
# Get all the members in a set.
|
|
769
883
|
def smembers(key)
|
|
770
884
|
node_for(key).smembers(key)
|
|
@@ -813,6 +927,14 @@ class Redis
|
|
|
813
927
|
end
|
|
814
928
|
end
|
|
815
929
|
|
|
930
|
+
# Get the number of members in the intersection of multiple sets.
|
|
931
|
+
def sintercard(*keys, limit: nil)
|
|
932
|
+
keys.flatten!(1)
|
|
933
|
+
ensure_same_node(:sintercard, keys) do |node|
|
|
934
|
+
node.sintercard(keys, limit: limit)
|
|
935
|
+
end
|
|
936
|
+
end
|
|
937
|
+
|
|
816
938
|
# Intersect multiple sets and store the resulting set in a key.
|
|
817
939
|
def sinterstore(destination, *keys)
|
|
818
940
|
keys.flatten!(1)
|
|
@@ -896,6 +1018,28 @@ class Redis
|
|
|
896
1018
|
end
|
|
897
1019
|
end
|
|
898
1020
|
|
|
1021
|
+
# Atomically move members from one sorted set to another, preserving their scores.
|
|
1022
|
+
def zmove(source, destination, **options)
|
|
1023
|
+
ensure_same_node(:zmove, [source, destination]) do |node|
|
|
1024
|
+
node.zmove(source, destination, **options)
|
|
1025
|
+
end
|
|
1026
|
+
end
|
|
1027
|
+
|
|
1028
|
+
# Atomically move the lowest- or highest-scoring members from one sorted set to
|
|
1029
|
+
# another, or block until a member is available or the timeout expires.
|
|
1030
|
+
def bzmove(source, destination, **options)
|
|
1031
|
+
ensure_same_node(:bzmove, [source, destination]) do |node|
|
|
1032
|
+
node.bzmove(source, destination, **options)
|
|
1033
|
+
end
|
|
1034
|
+
end
|
|
1035
|
+
|
|
1036
|
+
# Atomically move the given members from one sorted set to another, preserving their scores.
|
|
1037
|
+
def zmovemember(source, destination, members, **options)
|
|
1038
|
+
ensure_same_node(:zmovemember, [source, destination]) do |node|
|
|
1039
|
+
node.zmovemember(source, destination, members, **options)
|
|
1040
|
+
end
|
|
1041
|
+
end
|
|
1042
|
+
|
|
899
1043
|
# Return a range of members in a sorted set, by index, score or lexicographical ordering.
|
|
900
1044
|
def zrange(key, start, stop, **options)
|
|
901
1045
|
node_for(key).zrange(key, start, stop, **options)
|
|
@@ -960,6 +1104,14 @@ class Redis
|
|
|
960
1104
|
end
|
|
961
1105
|
end
|
|
962
1106
|
|
|
1107
|
+
# Get the number of members in the intersection of multiple sorted sets.
|
|
1108
|
+
def zintercard(*keys, limit: nil)
|
|
1109
|
+
keys.flatten!(1)
|
|
1110
|
+
ensure_same_node(:zintercard, keys) do |node|
|
|
1111
|
+
node.zintercard(keys, limit: limit)
|
|
1112
|
+
end
|
|
1113
|
+
end
|
|
1114
|
+
|
|
963
1115
|
# Intersect multiple sorted sets and store the resulting sorted set in a new
|
|
964
1116
|
# key.
|
|
965
1117
|
def zinterstore(destination, *keys, **options)
|
|
@@ -1052,6 +1204,23 @@ class Redis
|
|
|
1052
1204
|
node_for(key).hdel(key, fields)
|
|
1053
1205
|
end
|
|
1054
1206
|
|
|
1207
|
+
# Get the value of one or more hash fields and delete them.
|
|
1208
|
+
def hgetdel(key, *fields)
|
|
1209
|
+
fields.flatten!(1)
|
|
1210
|
+
node_for(key).hgetdel(key, fields)
|
|
1211
|
+
end
|
|
1212
|
+
|
|
1213
|
+
# Get the value of one or more hash fields and optionally set their expiration.
|
|
1214
|
+
def hgetex(key, *fields, ex: nil, px: nil, exat: nil, pxat: nil, persist: false)
|
|
1215
|
+
fields.flatten!(1)
|
|
1216
|
+
node_for(key).hgetex(key, *fields, ex: ex, px: px, exat: exat, pxat: pxat, persist: persist)
|
|
1217
|
+
end
|
|
1218
|
+
|
|
1219
|
+
# Set the value of one or more hash fields, and optionally their expiration.
|
|
1220
|
+
def hsetex(key, *attrs, fnx: nil, fxx: nil, ex: nil, px: nil, exat: nil, pxat: nil, keepttl: false)
|
|
1221
|
+
node_for(key).hsetex(key, *attrs, fnx: fnx, fxx: fxx, ex: ex, px: px, exat: exat, pxat: pxat, keepttl: keepttl)
|
|
1222
|
+
end
|
|
1223
|
+
|
|
1055
1224
|
# Determine if a hash field exists.
|
|
1056
1225
|
def hexists(key, field)
|
|
1057
1226
|
node_for(key).hexists(key, field)
|
|
@@ -1082,8 +1251,15 @@ class Redis
|
|
|
1082
1251
|
node_for(key).hgetall(key)
|
|
1083
1252
|
end
|
|
1084
1253
|
|
|
1085
|
-
|
|
1086
|
-
|
|
1254
|
+
# Set the time to live in seconds for one or more hash fields.
|
|
1255
|
+
#
|
|
1256
|
+
# @param [String] key
|
|
1257
|
+
# @param [Integer] ttl
|
|
1258
|
+
# @param [Array<String>] fields
|
|
1259
|
+
# @option options [Boolean] :nx, :xx, :gt, :lt at most one expiry condition
|
|
1260
|
+
# @return [Array<Integer>] per-field feedback, see Redis::Commands::Hashes#hexpire
|
|
1261
|
+
def hexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil)
|
|
1262
|
+
node_for(key).hexpire(key, ttl, *fields, nx: nx, xx: xx, gt: gt, lt: lt)
|
|
1087
1263
|
end
|
|
1088
1264
|
|
|
1089
1265
|
def httl(key, *fields)
|
|
@@ -1113,13 +1289,67 @@ class Redis
|
|
|
1113
1289
|
node_for(key).hpttl(key, *fields)
|
|
1114
1290
|
end
|
|
1115
1291
|
|
|
1292
|
+
# Set the expiration for one or more hash fields as an absolute Unix timestamp
|
|
1293
|
+
# in seconds.
|
|
1294
|
+
#
|
|
1295
|
+
# @param [String] key
|
|
1296
|
+
# @param [Integer] unix_time_seconds absolute expiration timestamp in seconds since epoch
|
|
1297
|
+
# @param [Array<String>] fields
|
|
1298
|
+
# @option options [Boolean] :nx, :xx, :gt, :lt at most one expiry condition
|
|
1299
|
+
# @return [Array<Integer>] per-field feedback, see Redis::Commands::Hashes#hexpireat
|
|
1300
|
+
def hexpireat(key, unix_time_seconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil)
|
|
1301
|
+
node_for(key).hexpireat(key, unix_time_seconds, *fields, nx: nx, xx: xx, gt: gt, lt: lt)
|
|
1302
|
+
end
|
|
1303
|
+
|
|
1304
|
+
# Set the expiration for one or more hash fields as an absolute Unix timestamp
|
|
1305
|
+
# in milliseconds.
|
|
1306
|
+
#
|
|
1307
|
+
# @param [String] key
|
|
1308
|
+
# @param [Integer] unix_time_milliseconds absolute expiration timestamp in milliseconds since epoch
|
|
1309
|
+
# @param [Array<String>] fields
|
|
1310
|
+
# @option options [Boolean] :nx, :xx, :gt, :lt at most one expiry condition
|
|
1311
|
+
# @return [Array<Integer>] per-field feedback, see Redis::Commands::Hashes#hpexpireat
|
|
1312
|
+
def hpexpireat(key, unix_time_milliseconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil)
|
|
1313
|
+
node_for(key).hpexpireat(key, unix_time_milliseconds, *fields, nx: nx, xx: xx, gt: gt, lt: lt)
|
|
1314
|
+
end
|
|
1315
|
+
|
|
1316
|
+
# Get the expiration of one or more hash fields as an absolute Unix timestamp
|
|
1317
|
+
# in seconds.
|
|
1318
|
+
#
|
|
1319
|
+
# @param [String] key
|
|
1320
|
+
# @param [Array<String>] fields
|
|
1321
|
+
# @return [Array<Integer>] per-field timestamps in seconds, see Redis::Commands::Hashes#hexpiretime
|
|
1322
|
+
def hexpiretime(key, *fields)
|
|
1323
|
+
node_for(key).hexpiretime(key, *fields)
|
|
1324
|
+
end
|
|
1325
|
+
|
|
1326
|
+
# Get the expiration of one or more hash fields as an absolute Unix timestamp
|
|
1327
|
+
# in milliseconds.
|
|
1328
|
+
#
|
|
1329
|
+
# @param [String] key
|
|
1330
|
+
# @param [Array<String>] fields
|
|
1331
|
+
# @return [Array<Integer>] per-field timestamps in milliseconds, see Redis::Commands::Hashes#hpexpiretime
|
|
1332
|
+
def hpexpiretime(key, *fields)
|
|
1333
|
+
node_for(key).hpexpiretime(key, *fields)
|
|
1334
|
+
end
|
|
1335
|
+
|
|
1336
|
+
# Remove the expiration from one or more hash fields, making them persistent.
|
|
1337
|
+
#
|
|
1338
|
+
# @param [String] key
|
|
1339
|
+
# @param [Array<String>] fields
|
|
1340
|
+
# @return [Array<Integer>] per-field feedback, see Redis::Commands::Hashes#hpersist
|
|
1341
|
+
def hpersist(key, *fields)
|
|
1342
|
+
node_for(key).hpersist(key, *fields)
|
|
1343
|
+
end
|
|
1344
|
+
|
|
1116
1345
|
# Register a fieldset on every ring node. Fieldsets are scoped to a physical
|
|
1117
1346
|
# connection, so every node that may receive an `himport_set` for a key it
|
|
1118
1347
|
# owns needs the fieldset on its own connection.
|
|
1119
1348
|
def himport_prepare(fieldset_name, *fields)
|
|
1120
1349
|
fields.flatten!(1)
|
|
1350
|
+
fields.map! { |field| field.dup.freeze }
|
|
1121
1351
|
results = on_each_node(:himport_prepare, fieldset_name, fields)
|
|
1122
|
-
@himport_fieldsets[fieldset_name.to_s] = fields.
|
|
1352
|
+
@himport_fieldsets[fieldset_name.to_s] = fields.freeze
|
|
1123
1353
|
results
|
|
1124
1354
|
end
|
|
1125
1355
|
|
|
@@ -1235,12 +1465,17 @@ class Redis
|
|
|
1235
1465
|
# Watch the given keys to determine execution of the MULTI/EXEC block.
|
|
1236
1466
|
def watch(*keys, &block)
|
|
1237
1467
|
ensure_same_node(:watch, keys) do |node|
|
|
1238
|
-
@watch_key = key_tag(keys.first) || keys.first.to_s
|
|
1468
|
+
watch_key = @watch_key = key_tag(keys.first) || keys.first.to_s
|
|
1469
|
+
@released_watch_key = nil
|
|
1239
1470
|
|
|
1240
1471
|
begin
|
|
1241
1472
|
node.watch(*keys, &block)
|
|
1242
|
-
rescue StandardError
|
|
1243
|
-
|
|
1473
|
+
rescue StandardError => error
|
|
1474
|
+
# The block can end the first watch with #multi and then watch a key on another node.
|
|
1475
|
+
# Redis#watch skips UNWATCH on a connection error, which may come from another client.
|
|
1476
|
+
current = @watch_key
|
|
1477
|
+
@watch_key = @released_watch_key = nil
|
|
1478
|
+
unwatch_after_error(current) if current && (current != watch_key || error.is_a?(ConnectionError))
|
|
1244
1479
|
raise
|
|
1245
1480
|
end
|
|
1246
1481
|
end
|
|
@@ -1248,10 +1483,11 @@ class Redis
|
|
|
1248
1483
|
|
|
1249
1484
|
# Forget about all watched keys.
|
|
1250
1485
|
def unwatch
|
|
1251
|
-
|
|
1486
|
+
key = transaction_key
|
|
1487
|
+
raise CannotDistribute, :unwatch unless key
|
|
1252
1488
|
|
|
1253
|
-
result = node_for(
|
|
1254
|
-
@watch_key = nil
|
|
1489
|
+
result = node_for(key).unwatch
|
|
1490
|
+
@watch_key = @released_watch_key = nil
|
|
1255
1491
|
result
|
|
1256
1492
|
end
|
|
1257
1493
|
|
|
@@ -1261,26 +1497,49 @@ class Redis
|
|
|
1261
1497
|
|
|
1262
1498
|
# Mark the start of a transaction block.
|
|
1263
1499
|
def multi(&block)
|
|
1264
|
-
|
|
1500
|
+
key = transaction_key
|
|
1501
|
+
raise CannotDistribute, :multi unless key
|
|
1502
|
+
|
|
1503
|
+
node = node_for(key)
|
|
1504
|
+
begin
|
|
1505
|
+
result = node.multi(&block)
|
|
1506
|
+
rescue StandardError
|
|
1507
|
+
# If the block raised, no MULTI/EXEC was sent and the server still watches the keys. The
|
|
1508
|
+
# error may come from another connection, so the UNWATCH is always attempted.
|
|
1509
|
+
begin
|
|
1510
|
+
unwatch_after_error(key)
|
|
1511
|
+
ensure
|
|
1512
|
+
release_watch_key(key)
|
|
1513
|
+
end
|
|
1514
|
+
raise
|
|
1515
|
+
end
|
|
1265
1516
|
|
|
1266
|
-
|
|
1517
|
+
begin
|
|
1518
|
+
# A block that queues no command sends no MULTI/EXEC, so the server still watches the keys.
|
|
1519
|
+
node.unwatch if result == []
|
|
1520
|
+
ensure
|
|
1521
|
+
release_watch_key(key)
|
|
1522
|
+
end
|
|
1523
|
+
result
|
|
1267
1524
|
end
|
|
1268
1525
|
|
|
1269
1526
|
# Execute all commands issued after MULTI.
|
|
1270
1527
|
def exec
|
|
1271
|
-
|
|
1528
|
+
key = transaction_key
|
|
1529
|
+
raise CannotDistribute, :exec unless key
|
|
1272
1530
|
|
|
1273
|
-
result = node_for(
|
|
1274
|
-
@watch_key = nil
|
|
1531
|
+
result = node_for(key).exec
|
|
1532
|
+
@watch_key = @released_watch_key = nil
|
|
1275
1533
|
result
|
|
1276
1534
|
end
|
|
1277
1535
|
|
|
1278
1536
|
# Discard all commands issued after MULTI.
|
|
1279
1537
|
def discard
|
|
1280
|
-
|
|
1538
|
+
key = transaction_key
|
|
1539
|
+
raise CannotDistribute, :discard unless key
|
|
1281
1540
|
|
|
1282
|
-
result = node_for(
|
|
1283
|
-
@watch_key = nil
|
|
1541
|
+
result = node_for(key).discard
|
|
1542
|
+
@watch_key = @released_watch_key = nil
|
|
1284
1543
|
result
|
|
1285
1544
|
end
|
|
1286
1545
|
|
|
@@ -1289,6 +1548,226 @@ class Redis
|
|
|
1289
1548
|
on_each_node(:script, subcommand, *args)
|
|
1290
1549
|
end
|
|
1291
1550
|
|
|
1551
|
+
# Manage Redis Functions libraries.
|
|
1552
|
+
#
|
|
1553
|
+
# Function libraries are server-global rather than per-database, so the fan-out runs
|
|
1554
|
+
# once per physical server: ring nodes differing only by database would re-run
|
|
1555
|
+
# mutations (e.g. FUNCTION LOAD) against the same server and fail on collision.
|
|
1556
|
+
def function(subcommand, *args, **options)
|
|
1557
|
+
# Both RedisClient::Config and RedisClient::SentinelConfig expose the address;
|
|
1558
|
+
# for a sentinel-backed node it is the currently resolved master.
|
|
1559
|
+
function_nodes = nodes.uniq do |node|
|
|
1560
|
+
config = node._client.config
|
|
1561
|
+
[config.path, config.host, config.port]
|
|
1562
|
+
end
|
|
1563
|
+
|
|
1564
|
+
if subcommand.to_s.downcase == "kill"
|
|
1565
|
+
# Only the node actually running a function replies OK; idle nodes raise NOTBUSY.
|
|
1566
|
+
# Succeed when any node killed a function, mirroring the cluster client's
|
|
1567
|
+
# one_succeeded fan-out semantics.
|
|
1568
|
+
not_busy = nil
|
|
1569
|
+
killed = function_nodes.filter_map do |node|
|
|
1570
|
+
node.function(:kill)
|
|
1571
|
+
rescue CommandError => e
|
|
1572
|
+
raise unless e.message.start_with?("NOTBUSY")
|
|
1573
|
+
|
|
1574
|
+
not_busy = e
|
|
1575
|
+
nil
|
|
1576
|
+
end
|
|
1577
|
+
|
|
1578
|
+
raise not_busy if killed.empty? && not_busy
|
|
1579
|
+
|
|
1580
|
+
killed.first
|
|
1581
|
+
else
|
|
1582
|
+
function_nodes.map { |node| node.function(subcommand, *args, **options) }
|
|
1583
|
+
end
|
|
1584
|
+
end
|
|
1585
|
+
|
|
1586
|
+
# Add a new element into a vector set, or update its vector if it already exists.
|
|
1587
|
+
def vadd(key, vector, element, **options)
|
|
1588
|
+
node_for(key).vadd(key, vector, element, **options)
|
|
1589
|
+
end
|
|
1590
|
+
|
|
1591
|
+
# Return the number of elements in a vector set.
|
|
1592
|
+
def vcard(key)
|
|
1593
|
+
node_for(key).vcard(key)
|
|
1594
|
+
end
|
|
1595
|
+
|
|
1596
|
+
# Return the number of dimensions of the vectors in a vector set.
|
|
1597
|
+
def vdim(key)
|
|
1598
|
+
node_for(key).vdim(key)
|
|
1599
|
+
end
|
|
1600
|
+
|
|
1601
|
+
# Return the approximate vector associated with an element in a vector set.
|
|
1602
|
+
def vemb(key, element, **options)
|
|
1603
|
+
node_for(key).vemb(key, element, **options)
|
|
1604
|
+
end
|
|
1605
|
+
|
|
1606
|
+
# Associate, update or delete the JSON attributes of an element in a vector set.
|
|
1607
|
+
def vsetattr(key, element, attributes)
|
|
1608
|
+
node_for(key).vsetattr(key, element, attributes)
|
|
1609
|
+
end
|
|
1610
|
+
|
|
1611
|
+
# Return the JSON attributes associated with an element in a vector set.
|
|
1612
|
+
def vgetattr(key, element, **options)
|
|
1613
|
+
node_for(key).vgetattr(key, element, **options)
|
|
1614
|
+
end
|
|
1615
|
+
|
|
1616
|
+
# Return elements of a vector set by vector similarity.
|
|
1617
|
+
def vsim(key, **options)
|
|
1618
|
+
node_for(key).vsim(key, **options)
|
|
1619
|
+
end
|
|
1620
|
+
|
|
1621
|
+
# Remove an element from a vector set.
|
|
1622
|
+
def vrem(key, element)
|
|
1623
|
+
node_for(key).vrem(key, element)
|
|
1624
|
+
end
|
|
1625
|
+
|
|
1626
|
+
# Check if an element exists in a vector set.
|
|
1627
|
+
def vismember(key, element)
|
|
1628
|
+
node_for(key).vismember(key, element)
|
|
1629
|
+
end
|
|
1630
|
+
|
|
1631
|
+
# Return the neighbors of an element at each layer in the HNSW graph.
|
|
1632
|
+
def vlinks(key, element, **options)
|
|
1633
|
+
node_for(key).vlinks(key, element, **options)
|
|
1634
|
+
end
|
|
1635
|
+
|
|
1636
|
+
# Return metadata and internal details about a vector set.
|
|
1637
|
+
def vinfo(key)
|
|
1638
|
+
node_for(key).vinfo(key)
|
|
1639
|
+
end
|
|
1640
|
+
|
|
1641
|
+
# Return one or more random elements from a vector set.
|
|
1642
|
+
def vrandmember(key, count = nil)
|
|
1643
|
+
node_for(key).vrandmember(key, count)
|
|
1644
|
+
end
|
|
1645
|
+
|
|
1646
|
+
# Return elements of a vector set in a lexicographical range.
|
|
1647
|
+
def vrange(key, start, stop, count = nil)
|
|
1648
|
+
node_for(key).vrange(key, start, stop, count)
|
|
1649
|
+
end
|
|
1650
|
+
|
|
1651
|
+
# Set one or more contiguous values starting at an index in an array.
|
|
1652
|
+
def arset(key, index, *values)
|
|
1653
|
+
node_for(key).arset(key, index, *values)
|
|
1654
|
+
end
|
|
1655
|
+
|
|
1656
|
+
# Get the value at an index in an array.
|
|
1657
|
+
def arget(key, index)
|
|
1658
|
+
node_for(key).arget(key, index)
|
|
1659
|
+
end
|
|
1660
|
+
|
|
1661
|
+
# Set multiple index-value pairs in an array.
|
|
1662
|
+
def armset(key, *pairs)
|
|
1663
|
+
node_for(key).armset(key, *pairs)
|
|
1664
|
+
end
|
|
1665
|
+
|
|
1666
|
+
# Get values at multiple indices in an array.
|
|
1667
|
+
def armget(key, *indices)
|
|
1668
|
+
node_for(key).armget(key, *indices)
|
|
1669
|
+
end
|
|
1670
|
+
|
|
1671
|
+
# Get values in a range of indices.
|
|
1672
|
+
def argetrange(key, start, stop)
|
|
1673
|
+
node_for(key).argetrange(key, start, stop)
|
|
1674
|
+
end
|
|
1675
|
+
|
|
1676
|
+
# Get the length of an array (max index + 1).
|
|
1677
|
+
def arlen(key)
|
|
1678
|
+
node_for(key).arlen(key)
|
|
1679
|
+
end
|
|
1680
|
+
|
|
1681
|
+
# Get the number of non-empty elements in an array.
|
|
1682
|
+
def arcount(key)
|
|
1683
|
+
node_for(key).arcount(key)
|
|
1684
|
+
end
|
|
1685
|
+
|
|
1686
|
+
# Delete elements at the specified indices in an array.
|
|
1687
|
+
def ardel(key, *indices)
|
|
1688
|
+
node_for(key).ardel(key, *indices)
|
|
1689
|
+
end
|
|
1690
|
+
|
|
1691
|
+
# Delete elements in one or more inclusive index ranges.
|
|
1692
|
+
def ardelrange(key, *ranges)
|
|
1693
|
+
node_for(key).ardelrange(key, *ranges)
|
|
1694
|
+
end
|
|
1695
|
+
|
|
1696
|
+
# Insert one or more values at the array's insert cursor.
|
|
1697
|
+
def arinsert(key, *values)
|
|
1698
|
+
node_for(key).arinsert(key, *values)
|
|
1699
|
+
end
|
|
1700
|
+
|
|
1701
|
+
# Set the insert cursor of an array to a specific index.
|
|
1702
|
+
def arseek(key, index)
|
|
1703
|
+
node_for(key).arseek(key, index)
|
|
1704
|
+
end
|
|
1705
|
+
|
|
1706
|
+
# Get the next index ARINSERT would use.
|
|
1707
|
+
def arnext(key)
|
|
1708
|
+
node_for(key).arnext(key)
|
|
1709
|
+
end
|
|
1710
|
+
|
|
1711
|
+
# Get the most recently inserted elements.
|
|
1712
|
+
def arlastitems(key, count, rev: false)
|
|
1713
|
+
node_for(key).arlastitems(key, count, rev: rev)
|
|
1714
|
+
end
|
|
1715
|
+
|
|
1716
|
+
# Insert one or more values into an array used as a fixed-size ring buffer.
|
|
1717
|
+
def arring(key, size, *values)
|
|
1718
|
+
node_for(key).arring(key, size, *values)
|
|
1719
|
+
end
|
|
1720
|
+
|
|
1721
|
+
# Iterate existing elements in an index range.
|
|
1722
|
+
def arscan(key, start, stop, limit: nil)
|
|
1723
|
+
node_for(key).arscan(key, start, stop, limit: limit)
|
|
1724
|
+
end
|
|
1725
|
+
|
|
1726
|
+
# Search array elements within an index range using textual predicates.
|
|
1727
|
+
def argrep(key, start, stop, **options)
|
|
1728
|
+
node_for(key).argrep(key, start, stop, **options)
|
|
1729
|
+
end
|
|
1730
|
+
|
|
1731
|
+
# Perform an aggregate operation on the elements in a range.
|
|
1732
|
+
def arop(key, start, stop, operation, value: nil)
|
|
1733
|
+
node_for(key).arop(key, start, stop, operation, value: value)
|
|
1734
|
+
end
|
|
1735
|
+
|
|
1736
|
+
# Get metadata about an array.
|
|
1737
|
+
def arinfo(key, full: false)
|
|
1738
|
+
node_for(key).arinfo(key, full: full)
|
|
1739
|
+
end
|
|
1740
|
+
|
|
1741
|
+
# Return the active protection flags of a key.
|
|
1742
|
+
def bless_get(key)
|
|
1743
|
+
node_for(key).bless_get(key)
|
|
1744
|
+
end
|
|
1745
|
+
|
|
1746
|
+
# Add a protection flag to a key.
|
|
1747
|
+
def bless_set(key, flag)
|
|
1748
|
+
node_for(key).bless_set(key, flag)
|
|
1749
|
+
end
|
|
1750
|
+
|
|
1751
|
+
# Remove a protection flag from a key.
|
|
1752
|
+
def bless_clear(key, flag)
|
|
1753
|
+
node_for(key).bless_clear(key, flag)
|
|
1754
|
+
end
|
|
1755
|
+
|
|
1756
|
+
# A BLESS SCAN cursor is only meaningful on the node that issued it, so a
|
|
1757
|
+
# single cursor cannot drive an iteration across independent ring nodes.
|
|
1758
|
+
# Use #bless_scan_each, which iterates every node in turn.
|
|
1759
|
+
def bless_scan(_cursor, _flag, **_options)
|
|
1760
|
+
raise NotImplementedError
|
|
1761
|
+
end
|
|
1762
|
+
|
|
1763
|
+
# Iterate every key that carries the given protection flag, node by node
|
|
1764
|
+
# in ring order.
|
|
1765
|
+
def bless_scan_each(flag, **options, &block)
|
|
1766
|
+
return to_enum(:bless_scan_each, flag, **options) unless block_given?
|
|
1767
|
+
|
|
1768
|
+
nodes.each { |node| node.bless_scan_each(flag, **options, &block) }
|
|
1769
|
+
end
|
|
1770
|
+
|
|
1292
1771
|
# Add one or more members to a HyperLogLog structure.
|
|
1293
1772
|
def pfadd(key, member)
|
|
1294
1773
|
node_for(key).pfadd(key, member)
|
|
@@ -1332,6 +1811,16 @@ class Redis
|
|
|
1332
1811
|
_eval(:evalsha, args)
|
|
1333
1812
|
end
|
|
1334
1813
|
|
|
1814
|
+
# Invoke a Redis Function.
|
|
1815
|
+
def fcall(*args)
|
|
1816
|
+
_eval(:fcall, args)
|
|
1817
|
+
end
|
|
1818
|
+
|
|
1819
|
+
# Invoke a read-only Redis Function.
|
|
1820
|
+
def fcall_ro(*args)
|
|
1821
|
+
_eval(:fcall_ro, args)
|
|
1822
|
+
end
|
|
1823
|
+
|
|
1335
1824
|
def inspect
|
|
1336
1825
|
"#<Redis client v#{Redis::VERSION} for #{nodes.map(&:id).join(', ')}>"
|
|
1337
1826
|
end
|
|
@@ -1357,6 +1846,34 @@ class Redis
|
|
|
1357
1846
|
key[@tag, 1] if key.match?(@tag)
|
|
1358
1847
|
end
|
|
1359
1848
|
|
|
1849
|
+
# The key #multi, #exec, #discard and #unwatch run against. After #multi the watched key no
|
|
1850
|
+
# longer pins other commands to its node, but these methods still target that node so that
|
|
1851
|
+
# an explicit #unwatch or a second #multi keeps working as before.
|
|
1852
|
+
def transaction_key
|
|
1853
|
+
@watch_key || @released_watch_key
|
|
1854
|
+
end
|
|
1855
|
+
|
|
1856
|
+
def release_watch_key(key)
|
|
1857
|
+
@released_watch_key = key
|
|
1858
|
+
@watch_key = nil
|
|
1859
|
+
end
|
|
1860
|
+
|
|
1861
|
+
# Cleanup while another error propagates. The error may come from a different node, so the
|
|
1862
|
+
# UNWATCH is always attempted, and a connection error from it does not hide the original.
|
|
1863
|
+
def unwatch_after_error(key)
|
|
1864
|
+
node_for(key).unwatch
|
|
1865
|
+
rescue BaseConnectionError
|
|
1866
|
+
nil
|
|
1867
|
+
end
|
|
1868
|
+
|
|
1869
|
+
# Keys SORT/SORT_RO touch, for same-node routing. A BY/GET pattern without `*` never reads
|
|
1870
|
+
# another key (`BY nosort` skips sorting, `GET #` yields the element itself), so it is
|
|
1871
|
+
# left out.
|
|
1872
|
+
def sort_keys(key, options)
|
|
1873
|
+
patterns = [options[:by], *Array(options[:get])].compact.select { |pattern| pattern.to_s.include?("*") }
|
|
1874
|
+
[key, options[:store], *patterns].compact
|
|
1875
|
+
end
|
|
1876
|
+
|
|
1360
1877
|
def ensure_same_node(command, keys)
|
|
1361
1878
|
all = true
|
|
1362
1879
|
|