Class: Redis::Distributed

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/distributed.rb

Defined Under Namespace

Classes: CannotDistribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_configs, options = {}) ⇒ Distributed

Returns a new instance of Distributed.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/redis/distributed.rb', line 20

def initialize(node_configs, options = {})
  @tag = options[:tag] || /^\{(.+?)\}/
  @ring = options[:ring] || HashRing.new
  @node_configs = node_configs.map(&:dup)
  @default_options = options.dup
  # Schemas registered via himport_prepare, replayed to nodes that join the ring later (see
  # #add_node) so ring membership changes don't leave nodes without the fieldsets that
  # key-routed himport_set calls expect. Initialized before the add_node loop below.
  @himport_fieldsets = {}
  node_configs.each { |node_config| add_node(node_config) }
  @subscribed_node = nil
  @watch_key = nil
end

Instance Attribute Details

#ringObject (readonly)

Returns the value of attribute ring.



18
19
20
# File 'lib/redis/distributed.rb', line 18

def ring
  @ring
end

Instance Method Details

#[](key) ⇒ Object



537
538
539
# File 'lib/redis/distributed.rb', line 537

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



541
542
543
# File 'lib/redis/distributed.rb', line 541

def []=(key, value)
  set(key, value)
end

#_bpop(cmd, args) ⇒ Object



623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'lib/redis/distributed.rb', line 623

def _bpop(cmd, args)
  timeout = if args.last.is_a?(Hash)
    options = args.pop
    options[:timeout]
  end

  args.flatten!(1)

  ensure_same_node(cmd, args) do |node|
    if timeout
      node.__send__(cmd, args, timeout: timeout)
    else
      node.__send__(cmd, args)
    end
  end
end

#_eval(cmd, args) ⇒ Object



1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
# File 'lib/redis/distributed.rb', line 1312

def _eval(cmd, args)
  script = args.shift
  options = args.pop if args.last.is_a?(Hash)
  options ||= {}

  keys = args.shift || options[:keys] || []
  argv = args.shift || options[:argv] || []

  ensure_same_node(cmd, keys) do |node|
    node.send(cmd, script, keys, argv)
  end
end

#add_node(options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/redis/distributed.rb', line 45

def add_node(options)
  options = { url: options } if options.is_a?(String)
  options = @default_options.merge(options)
  options.delete(:tag)
  options.delete(:ring)
  node = Redis.new(options)
  # Replay registered fieldsets before the node can receive key-routed himport_set calls:
  # a node joining after a himport_prepare fan-out has neither the server-side fieldset nor
  # a populated registry to self-recover from "no such fieldset".
  @himport_fieldsets.each { |name, fields| node.himport_prepare(name, fields) }
  @ring.add_node node
end

#append(key, value) ⇒ Object

Append a value to a key.



505
506
507
# File 'lib/redis/distributed.rb', line 505

def append(key, value)
  node_for(key).append(key, value)
end

#bgsaveObject

Asynchronously save the dataset to disk.



83
84
85
# File 'lib/redis/distributed.rb', line 83

def bgsave
  on_each_node :bgsave
end

#bitcount(key, start = 0, stop = -1,, scale: nil) ⇒ Object

Count the number of set bits in a range of the string value stored at key.



510
511
512
# File 'lib/redis/distributed.rb', line 510

def bitcount(key, start = 0, stop = -1, scale: nil)
  node_for(key).bitcount(key, start, stop, scale: scale)
end

#bitop(operation, destkey, *keys) ⇒ Object

Perform a bitwise operation between strings and store the resulting string in a key.



515
516
517
518
519
520
# File 'lib/redis/distributed.rb', line 515

def bitop(operation, destkey, *keys)
  keys.flatten!(1)
  ensure_same_node(:bitop, [destkey] + keys) do |node|
    node.bitop(operation, destkey, keys)
  end
end

#bitpos(key, bit, start = nil, stop = nil, scale: nil) ⇒ Object

Return the position of the first bit set to 1 or 0 in a string.



523
524
525
# File 'lib/redis/distributed.rb', line 523

def bitpos(key, bit, start = nil, stop = nil, scale: nil)
  node_for(key).bitpos(key, bit, start, stop, scale: scale)
end

#blmove(source, destination, where_source, where_destination, timeout: 0) ⇒ Object

Remove the first/last element in a list and append/prepend it to another list and return it, or block until one is available.



568
569
570
571
572
# File 'lib/redis/distributed.rb', line 568

def blmove(source, destination, where_source, where_destination, timeout: 0)
  ensure_same_node(:lmove, [source, destination]) do |node|
    node.blmove(source, destination, where_source, where_destination, timeout: timeout)
  end
end

#blmovem(source, destination, where_source, where_destination, timeout: 0, count: nil, exactly: nil, order: nil) ⇒ Object

Remove multiple elements from the head/tail of a list, append/prepend them to another list and return them; or block until the request can be satisfied or the timeout expires.



577
578
579
580
581
582
583
# File 'lib/redis/distributed.rb', line 577

def blmovem(source, destination, where_source, where_destination, timeout: 0, count: nil, exactly: nil,
            order: nil)
  ensure_same_node(:blmovem, [source, destination]) do |node|
    node.blmovem(source, destination, where_source, where_destination,
                 timeout: timeout, count: count, exactly: exactly, order: order)
  end
end

#blmpop(timeout, *keys, modifier: "LEFT", count: nil) ⇒ Object

Iterate over keys, blocking and removing elements from the first non empty liist found.



703
704
705
706
707
# File 'lib/redis/distributed.rb', line 703

def blmpop(timeout, *keys, modifier: "LEFT", count: nil)
  ensure_same_node(:blmpop, keys) do |node|
    node.blmpop(timeout, *keys, modifier: modifier, count: count)
  end
end

#blpop(*args) ⇒ Object

Remove and get the first element in a list, or block until one is available.



642
643
644
# File 'lib/redis/distributed.rb', line 642

def blpop(*args)
  _bpop(:blpop, args)
end

#brpop(*args) ⇒ Object

Remove and get the last element in a list, or block until one is available.



660
661
662
# File 'lib/redis/distributed.rb', line 660

def brpop(*args)
  _bpop(:brpop, args)
end

#brpoplpush(source, destination, **options) ⇒ Object

Pop a value from a list, push it to another list and return it; or block until one is available.



666
667
668
669
670
# File 'lib/redis/distributed.rb', line 666

def brpoplpush(source, destination, **options)
  ensure_same_node(:brpoplpush, [source, destination]) do |node|
    node.brpoplpush(source, destination, **options)
  end
end

#bzmpop(timeout, *keys, modifier: "MIN", count: nil) ⇒ Object

Iterate over keys, blocking and removing members from the first non empty sorted set found.



886
887
888
889
890
# File 'lib/redis/distributed.rb', line 886

def bzmpop(timeout, *keys, modifier: "MIN", count: nil)
  ensure_same_node(:bzmpop, keys) do |node|
    node.bzmpop(timeout, *keys, modifier: modifier, count: count)
  end
end

#bzpopmax(*args) ⇒ Object



646
647
648
649
650
# File 'lib/redis/distributed.rb', line 646

def bzpopmax(*args)
  _bpop(:bzpopmax, args) do |reply|
    reply.is_a?(Array) ? [reply[0], reply[1], Floatify.call(reply[2])] : reply
  end
end

#bzpopmin(*args) ⇒ Object



652
653
654
655
656
# File 'lib/redis/distributed.rb', line 652

def bzpopmin(*args)
  _bpop(:bzpopmin, args) do |reply|
    reply.is_a?(Array) ? [reply[0], reply[1], Floatify.call(reply[2])] : reply
  end
end

#closeObject



78
79
80
# File 'lib/redis/distributed.rb', line 78

def close
  on_each_node :close
end

#copy(source, destination, **options) ⇒ Object

Copy a value from one key to another.



235
236
237
238
239
# File 'lib/redis/distributed.rb', line 235

def copy(source, destination, **options)
  ensure_same_node(:copy, [source, destination]) do |node|
    node.copy(source, destination, **options)
  end
end

#dbsizeObject

Return the number of keys in the selected database.



88
89
90
# File 'lib/redis/distributed.rb', line 88

def dbsize
  on_each_node :dbsize
end

#decr(key) ⇒ Object

Decrement the integer value of a key by one.



275
276
277
# File 'lib/redis/distributed.rb', line 275

def decr(key)
  node_for(key).decr(key)
end

#decrby(key, decrement) ⇒ Object

Decrement the integer value of a key by the given number.



280
281
282
# File 'lib/redis/distributed.rb', line 280

def decrby(key, decrement)
  node_for(key).decrby(key, decrement)
end

#del(*args) ⇒ Object

Delete a key.



188
189
190
191
192
193
194
# File 'lib/redis/distributed.rb', line 188

def del(*args)
  args.flatten!(1)
  keys_per_node = args.group_by { |key| node_for(key) }
  keys_per_node.inject(0) do |sum, (node, keys)|
    sum + node.del(*keys)
  end
end

#discardObject

Discard all commands issued after MULTI.

Raises:



1279
1280
1281
1282
1283
1284
1285
# File 'lib/redis/distributed.rb', line 1279

def discard
  raise CannotDistribute, :discard unless @watch_key

  result = node_for(@watch_key).discard
  @watch_key = nil
  result
end

#dump(key) ⇒ Object

Return a serialized version of the value stored at a key.



173
174
175
# File 'lib/redis/distributed.rb', line 173

def dump(key)
  node_for(key).dump(key)
end

#dupObject



1339
1340
1341
# File 'lib/redis/distributed.rb', line 1339

def dup
  self.class.new(@node_configs, @default_options)
end

#echo(value) ⇒ Object

Echo the given string.



69
70
71
# File 'lib/redis/distributed.rb', line 69

def echo(value)
  on_each_node :echo, value
end

#eval(*args) ⇒ Object

Evaluate Lua script.



1326
1327
1328
# File 'lib/redis/distributed.rb', line 1326

def eval(*args)
  _eval(:eval, args)
end

#evalsha(*args) ⇒ Object

Evaluate Lua script by its SHA.



1331
1332
1333
# File 'lib/redis/distributed.rb', line 1331

def evalsha(*args)
  _eval(:evalsha, args)
end

#execObject

Execute all commands issued after MULTI.

Raises:



1270
1271
1272
1273
1274
1275
1276
# File 'lib/redis/distributed.rb', line 1270

def exec
  raise CannotDistribute, :exec unless @watch_key

  result = node_for(@watch_key).exec
  @watch_key = nil
  result
end

#exists(*args) ⇒ Object

Determine if a key exists.



206
207
208
209
210
211
212
# File 'lib/redis/distributed.rb', line 206

def exists(*args)
  args.flatten!(1)
  keys_per_node = args.group_by { |key| node_for(key) }
  keys_per_node.inject(0) do |sum, (node, keys)|
    sum + node.exists(*keys)
  end
end

#exists?(*args) ⇒ Boolean

Determine if any of the keys exists.

Returns:

  • (Boolean)


215
216
217
218
219
220
221
222
# File 'lib/redis/distributed.rb', line 215

def exists?(*args)
  args.flatten!(1)
  keys_per_node = args.group_by { |key| node_for(key) }
  keys_per_node.each do |node, keys|
    return true if node.exists?(*keys)
  end
  false
end

#expire(key, seconds, **kwargs) ⇒ Object

Set a key's time to live in seconds.



133
134
135
# File 'lib/redis/distributed.rb', line 133

def expire(key, seconds, **kwargs)
  node_for(key).expire(key, seconds, **kwargs)
end

#expireat(key, unix_time, **kwargs) ⇒ Object

Set the expiration for a key as a UNIX timestamp.



138
139
140
# File 'lib/redis/distributed.rb', line 138

def expireat(key, unix_time, **kwargs)
  node_for(key).expireat(key, unix_time, **kwargs)
end

#expiretime(key) ⇒ Object

Get the expiration for a key as a UNIX timestamp.



143
144
145
# File 'lib/redis/distributed.rb', line 143

def expiretime(key)
  node_for(key).expiretime(key)
end

#flushallObject

Remove all keys from all databases.



93
94
95
# File 'lib/redis/distributed.rb', line 93

def flushall
  on_each_node :flushall
end

#flushdbObject

Remove all keys from the current database.



98
99
100
# File 'lib/redis/distributed.rb', line 98

def flushdb
  on_each_node :flushdb
end

#geoadd(key, *member, **options) ⇒ Object

Add one or more geospatial items to a sorted set.



1147
1148
1149
# File 'lib/redis/distributed.rb', line 1147

def geoadd(key, *member, **options)
  node_for(key).geoadd(key, *member, **options)
end

#geodist(key, member1, member2, unit = 'm') ⇒ Object

Get the distance between two members of a geospatial index.



1162
1163
1164
# File 'lib/redis/distributed.rb', line 1162

def geodist(key, member1, member2, unit = 'm')
  node_for(key).geodist(key, member1, member2, unit)
end

#geohash(key, member) ⇒ Object

Get geohash strings representing the position of one or more members.



1152
1153
1154
# File 'lib/redis/distributed.rb', line 1152

def geohash(key, member)
  node_for(key).geohash(key, member)
end

#geopos(key, member) ⇒ Object

Get longitude and latitude of one or more members of a geospatial index.



1157
1158
1159
# File 'lib/redis/distributed.rb', line 1157

def geopos(key, member)
  node_for(key).geopos(key, member)
end

#georadius(*args, **geoptions) ⇒ Object

Query a geospatial index for members within a given radius from a point.



1167
1168
1169
1170
# File 'lib/redis/distributed.rb', line 1167

def georadius(*args, **geoptions)
  key = args.first
  node_for(key).georadius(*args, **geoptions)
end

#georadiusbymember(*args, **geoptions) ⇒ Object

Query a geospatial index for members within a given radius from an existing member.



1173
1174
1175
1176
# File 'lib/redis/distributed.rb', line 1173

def georadiusbymember(*args, **geoptions)
  key = args.first
  node_for(key).georadiusbymember(*args, **geoptions)
end

#geosearch(key, **options) ⇒ Object

Search a geospatial index for members within a given shape from a center point.



1179
1180
1181
# File 'lib/redis/distributed.rb', line 1179

def geosearch(key, **options)
  node_for(key).geosearch(key, **options)
end

#geosearchstore(destination, source, **options) ⇒ Object

Like #geosearch, but stores the result in a destination key. Destination and source must hash to the same node; use a key tag to ensure that.



1185
1186
1187
1188
1189
# File 'lib/redis/distributed.rb', line 1185

def geosearchstore(destination, source, **options)
  ensure_same_node(:geosearchstore, [destination, source]) do |node|
    node.geosearchstore(destination, source, **options)
  end
end

#get(key) ⇒ Object

Get the value of a key.



338
339
340
# File 'lib/redis/distributed.rb', line 338

def get(key)
  node_for(key).get(key)
end

#getbit(key, offset) ⇒ Object

Returns the bit value at offset in the string value stored at key.



500
501
502
# File 'lib/redis/distributed.rb', line 500

def getbit(key, offset)
  node_for(key).getbit(key, offset)
end

#getdel(key) ⇒ Object

Get the value of a key and delete it.



343
344
345
# File 'lib/redis/distributed.rb', line 343

def getdel(key)
  node_for(key).getdel(key)
end

#getex(key, **options) ⇒ Object

Get the value of a key and sets its time to live based on options.



348
349
350
# File 'lib/redis/distributed.rb', line 348

def getex(key, **options)
  node_for(key).getex(key, **options)
end

#getrange(key, start, stop) ⇒ Object

Get a substring of the string stored at a key.



490
491
492
# File 'lib/redis/distributed.rb', line 490

def getrange(key, start, stop)
  node_for(key).getrange(key, start, stop)
end

#getset(key, value) ⇒ Object

Set the string value of a key and return its old value.



528
529
530
# File 'lib/redis/distributed.rb', line 528

def getset(key, value)
  node_for(key).getset(key, value)
end

#hdel(key, *fields) ⇒ Object

Delete one or more hash fields.



1050
1051
1052
1053
# File 'lib/redis/distributed.rb', line 1050

def hdel(key, *fields)
  fields.flatten!(1)
  node_for(key).hdel(key, fields)
end

#hexists(key, field) ⇒ Object

Determine if a hash field exists.



1056
1057
1058
# File 'lib/redis/distributed.rb', line 1056

def hexists(key, field)
  node_for(key).hexists(key, field)
end

#hexpire(key, ttl, *fields) ⇒ Object



1085
1086
1087
# File 'lib/redis/distributed.rb', line 1085

def hexpire(key, ttl, *fields)
  node_for(key).hexpire(key, ttl, *fields)
end

#hget(key, field) ⇒ Object

Get the value of a hash field.



1030
1031
1032
# File 'lib/redis/distributed.rb', line 1030

def hget(key, field)
  node_for(key).hget(key, field)
end

#hgetall(key) ⇒ Object

Get all the fields and values in a hash.



1081
1082
1083
# File 'lib/redis/distributed.rb', line 1081

def hgetall(key)
  node_for(key).hgetall(key)
end

#himport_discard(fieldset_name) ⇒ Object

Remove a fieldset from every ring node.



1133
1134
1135
1136
1137
# File 'lib/redis/distributed.rb', line 1133

def himport_discard(fieldset_name)
  results = on_each_node(:himport_discard, fieldset_name)
  @himport_fieldsets.delete(fieldset_name.to_s)
  results
end

#himport_discard_allObject

Remove all fieldsets from every ring node.



1140
1141
1142
1143
1144
# File 'lib/redis/distributed.rb', line 1140

def himport_discard_all
  results = on_each_node(:himport_discard_all)
  @himport_fieldsets.clear
  results
end

#himport_prepare(fieldset_name, *fields) ⇒ Object

Register a fieldset on every ring node. Fieldsets are scoped to a physical connection, so every node that may receive an himport_set for a key it owns needs the fieldset on its own connection.



1119
1120
1121
1122
1123
1124
# File 'lib/redis/distributed.rb', line 1119

def himport_prepare(fieldset_name, *fields)
  fields.flatten!(1)
  results = on_each_node(:himport_prepare, fieldset_name, fields)
  @himport_fieldsets[fieldset_name.to_s] = fields.dup.freeze
  results
end

#himport_set(key, fieldset_name, *values) ⇒ Object

Create or fully replace a hash from a fieldset prepared on the key's node.



1127
1128
1129
1130
# File 'lib/redis/distributed.rb', line 1127

def himport_set(key, fieldset_name, *values)
  values.flatten!(1)
  node_for(key).himport_set(key, fieldset_name, values)
end

#hincrby(key, field, increment) ⇒ Object

Increment the integer value of a hash field by the given integer number.



1061
1062
1063
# File 'lib/redis/distributed.rb', line 1061

def hincrby(key, field, increment)
  node_for(key).hincrby(key, field, increment)
end

#hincrbyfloat(key, field, increment) ⇒ Object

Increment the numeric value of a hash field by the given float number.



1066
1067
1068
# File 'lib/redis/distributed.rb', line 1066

def hincrbyfloat(key, field, increment)
  node_for(key).hincrbyfloat(key, field, increment)
end

#hkeys(key) ⇒ Object

Get all the fields in a hash.



1071
1072
1073
# File 'lib/redis/distributed.rb', line 1071

def hkeys(key)
  node_for(key).hkeys(key)
end

#hlen(key) ⇒ Object

Get the number of fields in a hash.



1006
1007
1008
# File 'lib/redis/distributed.rb', line 1006

def hlen(key)
  node_for(key).hlen(key)
end

#hmget(key, *fields) ⇒ Object

Get the values of all the given hash fields.



1035
1036
1037
1038
# File 'lib/redis/distributed.rb', line 1035

def hmget(key, *fields)
  fields.flatten!(1)
  node_for(key).hmget(key, fields)
end

#hmset(key, *attrs) ⇒ Object

Set multiple hash fields to multiple values.



1021
1022
1023
# File 'lib/redis/distributed.rb', line 1021

def hmset(key, *attrs)
  node_for(key).hmset(key, *attrs)
end

#hpexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Object



1108
1109
1110
# File 'lib/redis/distributed.rb', line 1108

def hpexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil)
  node_for(key).hpexpire(key, ttl, *fields, nx: nx, xx: xx, gt: gt, lt: lt)
end

#hpttl(key, *fields) ⇒ Object



1112
1113
1114
# File 'lib/redis/distributed.rb', line 1112

def hpttl(key, *fields)
  node_for(key).hpttl(key, *fields)
end

#hrandfield(key, count = nil, **options) ⇒ Object



1045
1046
1047
# File 'lib/redis/distributed.rb', line 1045

def hrandfield(key, count = nil, **options)
  node_for(key).hrandfield(key, count, **options)
end

#hscan(key, cursor, **options) ⇒ Object

Scan a hash



1094
1095
1096
# File 'lib/redis/distributed.rb', line 1094

def hscan(key, cursor, **options)
  node_for(key).hscan(key, cursor, **options)
end

#hscan_each(key, **options, &block) ⇒ Object

Scan a hash and return an enumerator



1099
1100
1101
# File 'lib/redis/distributed.rb', line 1099

def hscan_each(key, **options, &block)
  node_for(key).hscan_each(key, **options, &block)
end

#hset(key, *attrs) ⇒ Object

Set multiple hash fields to multiple values.



1011
1012
1013
# File 'lib/redis/distributed.rb', line 1011

def hset(key, *attrs)
  node_for(key).hset(key, *attrs)
end

#hsetnx(key, field, value) ⇒ Object

Set the value of a hash field, only if the field does not exist.



1016
1017
1018
# File 'lib/redis/distributed.rb', line 1016

def hsetnx(key, field, value)
  node_for(key).hsetnx(key, field, value)
end

#hstrlen(key, field) ⇒ Object

Get the length of the value stored in a hash field



1104
1105
1106
# File 'lib/redis/distributed.rb', line 1104

def hstrlen(key, field)
  node_for(key).hstrlen(key, field)
end

#httl(key, *fields) ⇒ Object



1089
1090
1091
# File 'lib/redis/distributed.rb', line 1089

def httl(key, *fields)
  node_for(key).httl(key, *fields)
end

#hvals(key) ⇒ Object

Get all the values in a hash.



1076
1077
1078
# File 'lib/redis/distributed.rb', line 1076

def hvals(key)
  node_for(key).hvals(key)
end

#incr(key) ⇒ Object

Increment the integer value of a key by one.



285
286
287
# File 'lib/redis/distributed.rb', line 285

def incr(key)
  node_for(key).incr(key)
end

#incrby(key, increment) ⇒ Object

Increment the integer value of a key by the given integer number.



290
291
292
# File 'lib/redis/distributed.rb', line 290

def incrby(key, increment)
  node_for(key).incrby(key, increment)
end

#incrbyfloat(key, increment) ⇒ Object

Increment the numeric value of a key by the given float number.



295
296
297
# File 'lib/redis/distributed.rb', line 295

def incrbyfloat(key, increment)
  node_for(key).incrbyfloat(key, increment)
end

#info(cmd = nil) ⇒ Object

Get information and statistics about the server.



103
104
105
# File 'lib/redis/distributed.rb', line 103

def info(cmd = nil)
  on_each_node :info, cmd
end

#inspectObject



1335
1336
1337
# File 'lib/redis/distributed.rb', line 1335

def inspect
  "#<Redis client v#{Redis::VERSION} for #{nodes.map(&:id).join(', ')}>"
end

#json_arrappend(key, path, *values, **options) ⇒ Object

Append one or more values to the JSON array at a path in the document stored under a key.



401
402
403
# File 'lib/redis/distributed.rb', line 401

def json_arrappend(key, path, *values, **options)
  node_for(key).json_arrappend(key, path, *values, **options)
end

#json_arrindex(key, path, value, **options) ⇒ Object

Return the index of the first occurrence of a scalar in the JSON array at a path.



406
407
408
# File 'lib/redis/distributed.rb', line 406

def json_arrindex(key, path, value, **options)
  node_for(key).json_arrindex(key, path, value, **options)
end

#json_arrinsert(key, path, index, *values, **options) ⇒ Object

Insert one or more values into the JSON array at a path in the document stored under a key.



411
412
413
# File 'lib/redis/distributed.rb', line 411

def json_arrinsert(key, path, index, *values, **options)
  node_for(key).json_arrinsert(key, path, index, *values, **options)
end

#json_arrlen(key, path = nil) ⇒ Object

Return the length of the JSON array at a path in the document stored under a key.



416
417
418
# File 'lib/redis/distributed.rb', line 416

def json_arrlen(key, path = nil)
  node_for(key).json_arrlen(key, path)
end

#json_arrpop(key, path = nil, index = nil, **options) ⇒ Object

Remove and return an element from the JSON array at a path in the document stored under a key.



421
422
423
# File 'lib/redis/distributed.rb', line 421

def json_arrpop(key, path = nil, index = nil, **options)
  node_for(key).json_arrpop(key, path, index, **options)
end

#json_arrtrim(key, path, start, stop) ⇒ Object

Trim the JSON array at a path in the document stored under a key to an inclusive range.



426
427
428
# File 'lib/redis/distributed.rb', line 426

def json_arrtrim(key, path, start, stop)
  node_for(key).json_arrtrim(key, path, start, stop)
end

#json_clear(key, path = nil) ⇒ Object

Clear the container and numeric JSON value(s) at a path in the document stored under a key.



391
392
393
# File 'lib/redis/distributed.rb', line 391

def json_clear(key, path = nil)
  node_for(key).json_clear(key, path)
end

#json_debug_memory(key, path = nil) ⇒ Object

Report the size in bytes of the JSON value(s) at a path in the document stored under a key.



466
467
468
# File 'lib/redis/distributed.rb', line 466

def json_debug_memory(key, path = nil)
  node_for(key).json_debug_memory(key, path)
end

#json_del(key, path = nil) ⇒ Object

Delete the JSON value(s) at a path in the document stored under a key.



381
382
383
# File 'lib/redis/distributed.rb', line 381

def json_del(key, path = nil)
  node_for(key).json_del(key, path)
end

#json_forget(key, path = nil) ⇒ Object

Delete the JSON value(s) at a path in the document stored under a key (alias of json_del).



386
387
388
# File 'lib/redis/distributed.rb', line 386

def json_forget(key, path = nil)
  node_for(key).json_forget(key, path)
end

#json_get(key, *paths, **options) ⇒ Object

Get the JSON value(s) at one or more paths in the document stored under a key.



358
359
360
# File 'lib/redis/distributed.rb', line 358

def json_get(key, *paths, **options)
  node_for(key).json_get(key, *paths, **options)
end

#json_merge(key, path, value, **options) ⇒ Object

Merge a JSON value into the document stored under a key at a path.



396
397
398
# File 'lib/redis/distributed.rb', line 396

def json_merge(key, path, value, **options)
  node_for(key).json_merge(key, path, value, **options)
end

#json_mget(*keys, path, **options) ⇒ Object

Get the values at a path from several keys. Keys are grouped by node, queried per node, and reassembled in the original key order.



370
371
372
373
374
375
376
377
378
# File 'lib/redis/distributed.rb', line 370

def json_mget(*keys, path, **options)
  keys.flatten!(1)
  values = keys.group_by { |key| node_for(key) }.each_with_object({}) do |(node, subkeys), acc|
    node.json_mget(*subkeys, path, **options).each_with_index do |value, i|
      acc[subkeys[i]] = value
    end
  end
  keys.map { |key| values[key] }
end

#json_msetObject

Set one or more JSON values. The keys may live on different nodes and the operation must be atomic, so it cannot be distributed.

Raises:



364
365
366
# File 'lib/redis/distributed.rb', line 364

def json_mset(*)
  raise CannotDistribute, :json_mset
end

#json_numincrby(key, path, number) ⇒ Object

Increment the numeric JSON value(s) at a path in the document stored under a key.



431
432
433
# File 'lib/redis/distributed.rb', line 431

def json_numincrby(key, path, number)
  node_for(key).json_numincrby(key, path, number)
end

#json_objkeys(key, path = nil) ⇒ Object

Return the key names of the JSON object(s) at a path in the document stored under a key.



441
442
443
# File 'lib/redis/distributed.rb', line 441

def json_objkeys(key, path = nil)
  node_for(key).json_objkeys(key, path)
end

#json_objlen(key, path = nil) ⇒ Object

Return the number of keys in the JSON object(s) at a path in the document stored under a key.



446
447
448
# File 'lib/redis/distributed.rb', line 446

def json_objlen(key, path = nil)
  node_for(key).json_objlen(key, path)
end

#json_set(key, path, value, **options) ⇒ Object

Set the JSON value at a path in the document stored under a key.



353
354
355
# File 'lib/redis/distributed.rb', line 353

def json_set(key, path, value, **options)
  node_for(key).json_set(key, path, value, **options)
end

#json_strappend(key, path, value, **options) ⇒ Object

Append a string to the JSON string(s) at a path in the document stored under a key.



456
457
458
# File 'lib/redis/distributed.rb', line 456

def json_strappend(key, path, value, **options)
  node_for(key).json_strappend(key, path, value, **options)
end

#json_strlen(key, path = nil) ⇒ Object

Return the length of the JSON string(s) at a path in the document stored under a key.



451
452
453
# File 'lib/redis/distributed.rb', line 451

def json_strlen(key, path = nil)
  node_for(key).json_strlen(key, path)
end

#json_toggle(key, path) ⇒ Object

Toggle the boolean JSON value(s) at a path in the document stored under a key.



461
462
463
# File 'lib/redis/distributed.rb', line 461

def json_toggle(key, path)
  node_for(key).json_toggle(key, path)
end

#json_type(key, path = nil) ⇒ Object

Return the type name(s) of the JSON value(s) at a path in the document stored under a key.



436
437
438
# File 'lib/redis/distributed.rb', line 436

def json_type(key, path = nil)
  node_for(key).json_type(key, path)
end

#keys(glob = "*") ⇒ Object

Find all keys matching the given pattern.



225
226
227
# File 'lib/redis/distributed.rb', line 225

def keys(glob = "*")
  on_each_node(:keys, glob).flatten
end

#lastsaveObject

Get the UNIX time stamp of the last successful save to disk.



108
109
110
# File 'lib/redis/distributed.rb', line 108

def lastsave
  on_each_node :lastsave
end

#lindex(key, index) ⇒ Object

Get an element from a list by its index.



673
674
675
# File 'lib/redis/distributed.rb', line 673

def lindex(key, index)
  node_for(key).lindex(key, index)
end

#linsert(key, where, pivot, value) ⇒ Object

Insert an element before or after another element in a list.



678
679
680
# File 'lib/redis/distributed.rb', line 678

def linsert(key, where, pivot, value)
  node_for(key).linsert(key, where, pivot, value)
end

#llen(key) ⇒ Object

Get the length of a list.



546
547
548
# File 'lib/redis/distributed.rb', line 546

def llen(key)
  node_for(key).llen(key)
end

#lmove(source, destination, where_source, where_destination) ⇒ Object

Remove the first/last element in a list, append/prepend it to another list and return it.



551
552
553
554
555
# File 'lib/redis/distributed.rb', line 551

def lmove(source, destination, where_source, where_destination)
  ensure_same_node(:lmove, [source, destination]) do |node|
    node.lmove(source, destination, where_source, where_destination)
  end
end

#lmovem(source, destination, where_source, where_destination, count: nil, exactly: nil, order: nil) ⇒ Object

Remove multiple elements from the head/tail of a list, append/prepend them to another list and return them.



559
560
561
562
563
564
# File 'lib/redis/distributed.rb', line 559

def lmovem(source, destination, where_source, where_destination, count: nil, exactly: nil, order: nil)
  ensure_same_node(:lmovem, [source, destination]) do |node|
    node.lmovem(source, destination, where_source, where_destination,
                count: count, exactly: exactly, order: order)
  end
end

#lmpop(*keys, modifier: "LEFT", count: nil) ⇒ Object

Iterate over keys, removing elements from the first non list found.



710
711
712
713
714
# File 'lib/redis/distributed.rb', line 710

def lmpop(*keys, modifier: "LEFT", count: nil)
  ensure_same_node(:lmpop, keys) do |node|
    node.lmpop(*keys, modifier: modifier, count: count)
  end
end

#lpop(key, count = nil) ⇒ Object

Remove and get the first elements in a list.



606
607
608
# File 'lib/redis/distributed.rb', line 606

def lpop(key, count = nil)
  node_for(key).lpop(key, count)
end

#lpush(key, value) ⇒ Object

Prepend one or more values to a list.



586
587
588
# File 'lib/redis/distributed.rb', line 586

def lpush(key, value)
  node_for(key).lpush(key, value)
end

#lpushx(key, value) ⇒ Object

Prepend a value to a list, only if the list exists.



591
592
593
# File 'lib/redis/distributed.rb', line 591

def lpushx(key, value)
  node_for(key).lpushx(key, value)
end

#lrange(key, start, stop) ⇒ Object

Get a range of elements from a list.



683
684
685
# File 'lib/redis/distributed.rb', line 683

def lrange(key, start, stop)
  node_for(key).lrange(key, start, stop)
end

#lrem(key, count, value) ⇒ Object

Remove elements from a list.



688
689
690
# File 'lib/redis/distributed.rb', line 688

def lrem(key, count, value)
  node_for(key).lrem(key, count, value)
end

#lset(key, index, value) ⇒ Object

Set the value of an element in a list by its index.



693
694
695
# File 'lib/redis/distributed.rb', line 693

def lset(key, index, value)
  node_for(key).lset(key, index, value)
end

#ltrim(key, start, stop) ⇒ Object

Trim a list to the specified range.



698
699
700
# File 'lib/redis/distributed.rb', line 698

def ltrim(key, start, stop)
  node_for(key).ltrim(key, start, stop)
end

#mapped_hmget(key, *fields) ⇒ Object



1040
1041
1042
1043
# File 'lib/redis/distributed.rb', line 1040

def mapped_hmget(key, *fields)
  fields.flatten!(1)
  node_for(key).mapped_hmget(key, fields)
end

#mapped_hmset(key, hash) ⇒ Object



1025
1026
1027
# File 'lib/redis/distributed.rb', line 1025

def mapped_hmset(key, hash)
  node_for(key).hmset(key, hash)
end

#mapped_mget(*keys) ⇒ Object

Get the values of all the given keys as a Hash.



477
478
479
480
481
482
# File 'lib/redis/distributed.rb', line 477

def mapped_mget(*keys)
  keys.flatten!(1)
  keys.group_by { |k| node_for k }.inject({}) do |results, (node, subkeys)|
    results.merge! node.mapped_mget(*subkeys)
  end
end

#mapped_mset(_hash) ⇒ Object

Raises:



324
325
326
# File 'lib/redis/distributed.rb', line 324

def mapped_mset(_hash)
  raise CannotDistribute, :mapped_mset
end

#mapped_msetnx(_hash) ⇒ Object

Raises:



333
334
335
# File 'lib/redis/distributed.rb', line 333

def mapped_msetnx(_hash)
  raise CannotDistribute, :mapped_msetnx
end

#mget(*keys) ⇒ Object

Get the values of all the given keys as an Array.



471
472
473
474
# File 'lib/redis/distributed.rb', line 471

def mget(*keys)
  keys.flatten!(1)
  mapped_mget(*keys).values_at(*keys)
end

#migrate(_key, _options) ⇒ Object

Transfer a key from the connected instance to another instance.

Raises:



183
184
185
# File 'lib/redis/distributed.rb', line 183

def migrate(_key, _options)
  raise CannotDistribute, :migrate
end

#monitorObject

Listen for all requests received by the server in real time.

Raises:

  • (NotImplementedError)


113
114
115
# File 'lib/redis/distributed.rb', line 113

def monitor
  raise NotImplementedError
end

#move(key, db) ⇒ Object

Move a key to another database.



230
231
232
# File 'lib/redis/distributed.rb', line 230

def move(key, db)
  node_for(key).move(key, db)
end

#msetObject

Set multiple keys to multiple values.

Raises:



320
321
322
# File 'lib/redis/distributed.rb', line 320

def mset(*)
  raise CannotDistribute, :mset
end

#msetnxObject

Set multiple keys to multiple values, only if none of the keys exist.

Raises:



329
330
331
# File 'lib/redis/distributed.rb', line 329

def msetnx(*)
  raise CannotDistribute, :msetnx
end

#multi(&block) ⇒ Object

Mark the start of a transaction block.

Raises:



1263
1264
1265
1266
1267
# File 'lib/redis/distributed.rb', line 1263

def multi(&block)
  raise CannotDistribute, :multi unless @watch_key

  node_for(@watch_key).multi(&block)
end

#node_for(key) ⇒ Object

Raises:



34
35
36
37
38
39
# File 'lib/redis/distributed.rb', line 34

def node_for(key)
  key = key_tag(key.to_s) || key.to_s
  raise CannotDistribute, :watch if @watch_key && @watch_key != key

  @ring.get_node(key)
end

#nodesObject



41
42
43
# File 'lib/redis/distributed.rb', line 41

def nodes
  @ring.nodes
end

#persist(key) ⇒ Object

Remove the expiration from a key.



128
129
130
# File 'lib/redis/distributed.rb', line 128

def persist(key)
  node_for(key).persist(key)
end

#pexpire(key, milliseconds, **kwarg) ⇒ Object

Set a key's time to live in milliseconds.



153
154
155
# File 'lib/redis/distributed.rb', line 153

def pexpire(key, milliseconds, **kwarg)
  node_for(key).pexpire(key, milliseconds, **kwarg)
end

#pexpireat(key, ms_unix_time, **kwarg) ⇒ Object

Set the expiration for a key as number of milliseconds from UNIX Epoch.



158
159
160
# File 'lib/redis/distributed.rb', line 158

def pexpireat(key, ms_unix_time, **kwarg)
  node_for(key).pexpireat(key, ms_unix_time, **kwarg)
end

#pexpiretime(key) ⇒ Object

Get the expiration for a key as number of milliseconds from UNIX Epoch.



163
164
165
# File 'lib/redis/distributed.rb', line 163

def pexpiretime(key)
  node_for(key).pexpiretime(key)
end

#pfadd(key, member) ⇒ Object

Add one or more members to a HyperLogLog structure.



1293
1294
1295
# File 'lib/redis/distributed.rb', line 1293

def pfadd(key, member)
  node_for(key).pfadd(key, member)
end

#pfcount(*keys) ⇒ Object

Get the approximate cardinality of members added to HyperLogLog structure.



1298
1299
1300
1301
1302
# File 'lib/redis/distributed.rb', line 1298

def pfcount(*keys)
  ensure_same_node(:pfcount, keys.flatten(1)) do |node|
    node.pfcount(keys)
  end
end

#pfmerge(dest_key, *source_key) ⇒ Object

Merge multiple HyperLogLog values into an unique value that will approximate the cardinality of the union of the observed Sets of the source HyperLogLog structures.



1306
1307
1308
1309
1310
# File 'lib/redis/distributed.rb', line 1306

def pfmerge(dest_key, *source_key)
  ensure_same_node(:pfmerge, [dest_key, *source_key]) do |node|
    node.pfmerge(dest_key, *source_key)
  end
end

#pingObject

Ping the server.



64
65
66
# File 'lib/redis/distributed.rb', line 64

def ping
  on_each_node :ping
end

#pipelinedObject

Raises:



1258
1259
1260
# File 'lib/redis/distributed.rb', line 1258

def pipelined
  raise CannotDistribute, :pipelined
end

#psetex(key, ttl, value) ⇒ Object

Set the time to live in milliseconds of a key.



310
311
312
# File 'lib/redis/distributed.rb', line 310

def psetex(key, ttl, value)
  node_for(key).psetex(key, ttl, value)
end

#psubscribe(*channels, &block) ⇒ Object

Listen for messages published to channels matching the given patterns. See the Redis Server PSUBSCRIBE documentation for further details

Raises:

  • (NotImplementedError)


1223
1224
1225
# File 'lib/redis/distributed.rb', line 1223

def psubscribe(*channels, &block)
  raise NotImplementedError
end

#pttl(key) ⇒ Object

Get the time to live (in milliseconds) for a key.



168
169
170
# File 'lib/redis/distributed.rb', line 168

def pttl(key)
  node_for(key).pttl(key)
end

#publish(channel, message) ⇒ Object

Post a message to a channel.



1192
1193
1194
# File 'lib/redis/distributed.rb', line 1192

def publish(channel, message)
  node_for(channel).publish(channel, message)
end

#punsubscribe(*channels) ⇒ Object

Stop listening for messages posted to channels matching the given patterns. See the Redis Server PUNSUBSCRIBE documentation for further details

Raises:

  • (NotImplementedError)


1231
1232
1233
# File 'lib/redis/distributed.rb', line 1231

def punsubscribe(*channels)
  raise NotImplementedError
end

#quitObject

Close the connection.



74
75
76
# File 'lib/redis/distributed.rb', line 74

def quit
  on_each_node :quit
end

#randomkeyObject

Return a random key from the keyspace.

Raises:



242
243
244
# File 'lib/redis/distributed.rb', line 242

def randomkey
  raise CannotDistribute, :randomkey
end

#rename(old_name, new_name) ⇒ Object

Rename a key.



247
248
249
250
251
# File 'lib/redis/distributed.rb', line 247

def rename(old_name, new_name)
  ensure_same_node(:rename, [old_name, new_name]) do |node|
    node.rename(old_name, new_name)
  end
end

#renamenx(old_name, new_name) ⇒ Object

Rename a key, only if the new key does not exist.



254
255
256
257
258
# File 'lib/redis/distributed.rb', line 254

def renamenx(old_name, new_name)
  ensure_same_node(:renamenx, [old_name, new_name]) do |node|
    node.renamenx(old_name, new_name)
  end
end

#restore(key, ttl, serialized_value, **options) ⇒ Object

Create a key using the serialized value, previously obtained using DUMP.



178
179
180
# File 'lib/redis/distributed.rb', line 178

def restore(key, ttl, serialized_value, **options)
  node_for(key).restore(key, ttl, serialized_value, **options)
end

#rpop(key, count = nil) ⇒ Object

Remove and get the last elements in a list.



611
612
613
# File 'lib/redis/distributed.rb', line 611

def rpop(key, count = nil)
  node_for(key).rpop(key, count)
end

#rpoplpush(source, destination) ⇒ Object

Remove the last element in a list, append it to another list and return it.



617
618
619
620
621
# File 'lib/redis/distributed.rb', line 617

def rpoplpush(source, destination)
  ensure_same_node(:rpoplpush, [source, destination]) do |node|
    node.rpoplpush(source, destination)
  end
end

#rpush(key, value) ⇒ Object

Append one or more values to a list.



596
597
598
# File 'lib/redis/distributed.rb', line 596

def rpush(key, value)
  node_for(key).rpush(key, value)
end

#rpushx(key, value) ⇒ Object

Append a value to a list, only if the list exists.



601
602
603
# File 'lib/redis/distributed.rb', line 601

def rpushx(key, value)
  node_for(key).rpushx(key, value)
end

#sadd(key, *members) ⇒ Object

Add one or more members to a set.



722
723
724
# File 'lib/redis/distributed.rb', line 722

def sadd(key, *members)
  node_for(key).sadd(key, *members)
end

#sadd?(key, *members) ⇒ Boolean

Add one or more members to a set.

Returns:

  • (Boolean)


727
728
729
# File 'lib/redis/distributed.rb', line 727

def sadd?(key, *members)
  node_for(key).sadd?(key, *members)
end

#saveObject

Synchronously save the dataset to disk.



118
119
120
# File 'lib/redis/distributed.rb', line 118

def save
  on_each_node :save
end

#scard(key) ⇒ Object

Get the number of members in a set.



717
718
719
# File 'lib/redis/distributed.rb', line 717

def scard(key)
  node_for(key).scard(key)
end

#script(subcommand, *args) ⇒ Object

Control remote script registry.



1288
1289
1290
# File 'lib/redis/distributed.rb', line 1288

def script(subcommand, *args)
  on_each_node(:script, subcommand, *args)
end

#sdiff(*keys) ⇒ Object

Subtract multiple sets.



784
785
786
787
788
789
# File 'lib/redis/distributed.rb', line 784

def sdiff(*keys)
  keys.flatten!(1)
  ensure_same_node(:sdiff, keys) do |node|
    node.sdiff(keys)
  end
end

#sdiffcard(*keys, limit: nil) ⇒ Object

Get the number of members in the difference between the first set and all successive sets.



801
802
803
804
805
806
# File 'lib/redis/distributed.rb', line 801

def sdiffcard(*keys, limit: nil)
  keys.flatten!(1)
  ensure_same_node(:sdiffcard, keys) do |node|
    node.sdiffcard(keys, limit: limit)
  end
end

#sdiffstore(destination, *keys) ⇒ Object

Subtract multiple sets and store the resulting set in a key.



792
793
794
795
796
797
# File 'lib/redis/distributed.rb', line 792

def sdiffstore(destination, *keys)
  keys.flatten!(1)
  ensure_same_node(:sdiffstore, [destination].concat(keys)) do |node|
    node.sdiffstore(destination, keys)
  end
end

#select(db) ⇒ Object

Change the selected database for the current connection.



59
60
61
# File 'lib/redis/distributed.rb', line 59

def select(db)
  on_each_node :select, db
end

#set(key, value, **options) ⇒ Object

Set the string value of a key.



300
301
302
# File 'lib/redis/distributed.rb', line 300

def set(key, value, **options)
  node_for(key).set(key, value, **options)
end

#setbit(key, offset, value) ⇒ Object

Sets or clears the bit at offset in the string value stored at key.



495
496
497
# File 'lib/redis/distributed.rb', line 495

def setbit(key, offset, value)
  node_for(key).setbit(key, offset, value)
end

#setex(key, ttl, value) ⇒ Object

Set the time to live in seconds of a key.



305
306
307
# File 'lib/redis/distributed.rb', line 305

def setex(key, ttl, value)
  node_for(key).setex(key, ttl, value)
end

#setnx(key, value) ⇒ Object

Set the value of a key, only if the key does not exist.



315
316
317
# File 'lib/redis/distributed.rb', line 315

def setnx(key, value)
  node_for(key).setnx(key, value)
end

#setrange(key, offset, value) ⇒ Object

Overwrite part of a string at key starting at the specified offset.



485
486
487
# File 'lib/redis/distributed.rb', line 485

def setrange(key, offset, value)
  node_for(key).setrange(key, offset, value)
end

#sinter(*keys) ⇒ Object

Intersect multiple sets.



809
810
811
812
813
814
# File 'lib/redis/distributed.rb', line 809

def sinter(*keys)
  keys.flatten!(1)
  ensure_same_node(:sinter, keys) do |node|
    node.sinter(keys)
  end
end

#sinterstore(destination, *keys) ⇒ Object

Intersect multiple sets and store the resulting set in a key.



817
818
819
820
821
822
# File 'lib/redis/distributed.rb', line 817

def sinterstore(destination, *keys)
  keys.flatten!(1)
  ensure_same_node(:sinterstore, [destination].concat(keys)) do |node|
    node.sinterstore(destination, keys)
  end
end

#sismember(key, member) ⇒ Object

Determine if a given value is a member of a set.



759
760
761
# File 'lib/redis/distributed.rb', line 759

def sismember(key, member)
  node_for(key).sismember(key, member)
end

#smembers(key) ⇒ Object

Get all the members in a set.



769
770
771
# File 'lib/redis/distributed.rb', line 769

def smembers(key)
  node_for(key).smembers(key)
end

#smismember(key, *members) ⇒ Object

Determine if multiple values are members of a set.



764
765
766
# File 'lib/redis/distributed.rb', line 764

def smismember(key, *members)
  node_for(key).smismember(key, *members)
end

#smove(source, destination, member) ⇒ Object

Move a member from one set to another.



752
753
754
755
756
# File 'lib/redis/distributed.rb', line 752

def smove(source, destination, member)
  ensure_same_node(:smove, [source, destination]) do |node|
    node.smove(source, destination, member)
  end
end

#sort(key, **options) ⇒ Object

Sort the elements in a list, set or sorted set.



261
262
263
264
265
266
267
# File 'lib/redis/distributed.rb', line 261

def sort(key, **options)
  keys = [key, options[:by], options[:store], *Array(options[:get])].compact

  ensure_same_node(:sort, keys) do |node|
    node.sort(key, **options)
  end
end

#spop(key, count = nil) ⇒ Object

Remove and return a random member from a set.



742
743
744
# File 'lib/redis/distributed.rb', line 742

def spop(key, count = nil)
  node_for(key).spop(key, count)
end

#srandmember(key, count = nil) ⇒ Object

Get a random member from a set.



747
748
749
# File 'lib/redis/distributed.rb', line 747

def srandmember(key, count = nil)
  node_for(key).srandmember(key, count)
end

#srem(key, *members) ⇒ Object

Remove one or more members from a set.



732
733
734
# File 'lib/redis/distributed.rb', line 732

def srem(key, *members)
  node_for(key).srem(key, *members)
end

#srem?(key, *members) ⇒ Boolean

Remove one or more members from a set.

Returns:

  • (Boolean)


737
738
739
# File 'lib/redis/distributed.rb', line 737

def srem?(key, *members)
  node_for(key).srem?(key, *members)
end

#sscan(key, cursor, **options) ⇒ Object

Scan a set



774
775
776
# File 'lib/redis/distributed.rb', line 774

def sscan(key, cursor, **options)
  node_for(key).sscan(key, cursor, **options)
end

#sscan_each(key, **options, &block) ⇒ Object

Scan a set and return an enumerator



779
780
781
# File 'lib/redis/distributed.rb', line 779

def sscan_each(key, **options, &block)
  node_for(key).sscan_each(key, **options, &block)
end

#strlen(key) ⇒ Object

Get the length of the value stored in a key.



533
534
535
# File 'lib/redis/distributed.rb', line 533

def strlen(key)
  node_for(key).strlen(key)
end

#subscribe(channel, *channels, &block) ⇒ Object

Listen for messages published to the given channels.



1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
# File 'lib/redis/distributed.rb', line 1201

def subscribe(channel, *channels, &block)
  if channels.empty?
    @subscribed_node = node_for(channel)
    @subscribed_node.subscribe(channel, &block)
  else
    ensure_same_node(:subscribe, [channel] + channels) do |node|
      @subscribed_node = node
      node.subscribe(channel, *channels, &block)
    end
  end
end

#subscribed?Boolean

Returns:

  • (Boolean)


1196
1197
1198
# File 'lib/redis/distributed.rb', line 1196

def subscribed?
  !!@subscribed_node
end

#sunion(*keys) ⇒ Object

Add multiple sets.



825
826
827
828
829
830
# File 'lib/redis/distributed.rb', line 825

def sunion(*keys)
  keys.flatten!(1)
  ensure_same_node(:sunion, keys) do |node|
    node.sunion(keys)
  end
end

#sunioncard(*keys, approx: false, limit: nil) ⇒ Object

Get the number of distinct members in the union of multiple sets.



841
842
843
844
845
846
# File 'lib/redis/distributed.rb', line 841

def sunioncard(*keys, approx: false, limit: nil)
  keys.flatten!(1)
  ensure_same_node(:sunioncard, keys) do |node|
    node.sunioncard(keys, approx: approx, limit: limit)
  end
end

#sunionstore(destination, *keys) ⇒ Object

Add multiple sets and store the resulting set in a key.



833
834
835
836
837
838
# File 'lib/redis/distributed.rb', line 833

def sunionstore(destination, *keys)
  keys.flatten!(1)
  ensure_same_node(:sunionstore, [destination].concat(keys)) do |node|
    node.sunionstore(destination, keys)
  end
end

#timeObject

Get server time: an UNIX timestamp and the elapsed microseconds in the current second.



123
124
125
# File 'lib/redis/distributed.rb', line 123

def time
  on_each_node :time
end

#ttl(key) ⇒ Object

Get the time to live (in seconds) for a key.



148
149
150
# File 'lib/redis/distributed.rb', line 148

def ttl(key)
  node_for(key).ttl(key)
end

#type(key) ⇒ Object

Determine the type stored at key.



270
271
272
# File 'lib/redis/distributed.rb', line 270

def type(key)
  node_for(key).type(key)
end

Unlink keys.



197
198
199
200
201
202
203
# File 'lib/redis/distributed.rb', line 197

def unlink(*args)
  args.flatten!(1)
  keys_per_node = args.group_by { |key| node_for(key) }
  keys_per_node.inject(0) do |sum, (node, keys)|
    sum + node.unlink(*keys)
  end
end

#unsubscribe(*channels) ⇒ Object

Stop listening for messages posted to the given channels.

Raises:



1214
1215
1216
1217
1218
# File 'lib/redis/distributed.rb', line 1214

def unsubscribe(*channels)
  raise SubscriptionError, "Can't unsubscribe if not subscribed." unless subscribed?

  @subscribed_node.unsubscribe(*channels)
end

#unwatchObject

Forget about all watched keys.

Raises:



1250
1251
1252
1253
1254
1255
1256
# File 'lib/redis/distributed.rb', line 1250

def unwatch
  raise CannotDistribute, :unwatch unless @watch_key

  result = node_for(@watch_key).unwatch
  @watch_key = nil
  result
end

#watch(*keys, &block) ⇒ Object

Watch the given keys to determine execution of the MULTI/EXEC block.



1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
# File 'lib/redis/distributed.rb', line 1236

def watch(*keys, &block)
  ensure_same_node(:watch, keys) do |node|
    @watch_key = key_tag(keys.first) || keys.first.to_s

    begin
      node.watch(*keys, &block)
    rescue StandardError
      @watch_key = nil
      raise
    end
  end
end

#zadd(key, *args) ⇒ Object

Add one or more members to a sorted set, or update the score for members that already exist.



855
856
857
# File 'lib/redis/distributed.rb', line 855

def zadd(key, *args)
  node_for(key).zadd(key, *args)
end

#zcard(key) ⇒ Object

Get the number of members in a sorted set.



849
850
851
# File 'lib/redis/distributed.rb', line 849

def zcard(key)
  node_for(key).zcard(key)
end

#zcount(key, min, max) ⇒ Object

Get the number of members in a particular score range.



951
952
953
# File 'lib/redis/distributed.rb', line 951

def zcount(key, min, max)
  node_for(key).zcount(key, min, max)
end

#zdiff(*keys, **options) ⇒ Object

Return the difference between the first and all successive input sorted sets.



989
990
991
992
993
994
# File 'lib/redis/distributed.rb', line 989

def zdiff(*keys, **options)
  keys.flatten!(1)
  ensure_same_node(:zdiff, keys) do |node|
    node.zdiff(keys, **options)
  end
end

#zdiffstore(destination, *keys, **options) ⇒ Object

Compute the difference between the first and all successive input sorted sets and store the resulting sorted set in a new key.



998
999
1000
1001
1002
1003
# File 'lib/redis/distributed.rb', line 998

def zdiffstore(destination, *keys, **options)
  keys.flatten!(1)
  ensure_same_node(:zdiffstore, [destination] + keys) do |node|
    node.zdiffstore(destination, keys, **options)
  end
end

#zincrby(key, increment, member) ⇒ Object

Increment the score of a member in a sorted set.



861
862
863
# File 'lib/redis/distributed.rb', line 861

def zincrby(key, increment, member)
  node_for(key).zincrby(key, increment, member)
end

#zinter(*keys, **options) ⇒ Object

Get the intersection of multiple sorted sets



956
957
958
959
960
961
# File 'lib/redis/distributed.rb', line 956

def zinter(*keys, **options)
  keys.flatten!(1)
  ensure_same_node(:zinter, keys) do |node|
    node.zinter(keys, **options)
  end
end

#zinterstore(destination, *keys, **options) ⇒ Object

Intersect multiple sorted sets and store the resulting sorted set in a new key.



965
966
967
968
969
970
# File 'lib/redis/distributed.rb', line 965

def zinterstore(destination, *keys, **options)
  keys.flatten!(1)
  ensure_same_node(:zinterstore, [destination].concat(keys)) do |node|
    node.zinterstore(destination, keys, **options)
  end
end

#zmpop(*keys, modifier: "MIN", count: nil) ⇒ Object

Iterate over keys, removing members from the first non empty sorted set found.



893
894
895
896
897
# File 'lib/redis/distributed.rb', line 893

def zmpop(*keys, modifier: "MIN", count: nil)
  ensure_same_node(:zmpop, keys) do |node|
    node.zmpop(*keys, modifier: modifier, count: count)
  end
end

#zmscore(key, *members) ⇒ Object

Get the scores associated with the given members in a sorted set.



881
882
883
# File 'lib/redis/distributed.rb', line 881

def zmscore(key, *members)
  node_for(key).zmscore(key, *members)
end

#zrandmember(key, count = nil, **options) ⇒ Object

Get one or more random members from a sorted set.



876
877
878
# File 'lib/redis/distributed.rb', line 876

def zrandmember(key, count = nil, **options)
  node_for(key).zrandmember(key, count, **options)
end

#zrange(key, start, stop, **options) ⇒ Object

Return a range of members in a sorted set, by index, score or lexicographical ordering.



900
901
902
# File 'lib/redis/distributed.rb', line 900

def zrange(key, start, stop, **options)
  node_for(key).zrange(key, start, stop, **options)
end

#zrangebyscore(key, min, max, **options) ⇒ Object

Return a range of members in a sorted set, by score.



935
936
937
# File 'lib/redis/distributed.rb', line 935

def zrangebyscore(key, min, max, **options)
  node_for(key).zrangebyscore(key, min, max, **options)
end

#zrangestore(dest_key, src_key, start, stop, **options) ⇒ Object

Select a range of members in a sorted set, by index, score or lexicographical ordering and store the resulting sorted set in a new key.



906
907
908
909
910
# File 'lib/redis/distributed.rb', line 906

def zrangestore(dest_key, src_key, start, stop, **options)
  ensure_same_node(:zrangestore, [dest_key, src_key]) do |node|
    node.zrangestore(dest_key, src_key, start, stop, **options)
  end
end

#zrank(key, member, **options) ⇒ Object

Determine the index of a member in a sorted set.



919
920
921
# File 'lib/redis/distributed.rb', line 919

def zrank(key, member, **options)
  node_for(key).zrank(key, member, **options)
end

#zrem(key, member) ⇒ Object

Remove one or more members from a sorted set.



866
867
868
# File 'lib/redis/distributed.rb', line 866

def zrem(key, member)
  node_for(key).zrem(key, member)
end

#zremrangebyrank(key, start, stop) ⇒ Object

Remove all members in a sorted set within the given indexes.



930
931
932
# File 'lib/redis/distributed.rb', line 930

def zremrangebyrank(key, start, stop)
  node_for(key).zremrangebyrank(key, start, stop)
end

#zremrangebyscore(key, min, max) ⇒ Object

Remove all members in a sorted set within the given scores.



946
947
948
# File 'lib/redis/distributed.rb', line 946

def zremrangebyscore(key, min, max)
  node_for(key).zremrangebyscore(key, min, max)
end

#zrevrange(key, start, stop, **options) ⇒ Object

Return a range of members in a sorted set, by index, with scores ordered from high to low.



914
915
916
# File 'lib/redis/distributed.rb', line 914

def zrevrange(key, start, stop, **options)
  node_for(key).zrevrange(key, start, stop, **options)
end

#zrevrangebyscore(key, max, min, **options) ⇒ Object

Return a range of members in a sorted set, by score, with scores ordered from high to low.



941
942
943
# File 'lib/redis/distributed.rb', line 941

def zrevrangebyscore(key, max, min, **options)
  node_for(key).zrevrangebyscore(key, max, min, **options)
end

#zrevrank(key, member, **options) ⇒ Object

Determine the index of a member in a sorted set, with scores ordered from high to low.



925
926
927
# File 'lib/redis/distributed.rb', line 925

def zrevrank(key, member, **options)
  node_for(key).zrevrank(key, member, **options)
end

#zscore(key, member) ⇒ Object

Get the score associated with the given member in a sorted set.



871
872
873
# File 'lib/redis/distributed.rb', line 871

def zscore(key, member)
  node_for(key).zscore(key, member)
end

#zunion(*keys, **options) ⇒ Object

Return the union of multiple sorted sets.



973
974
975
976
977
978
# File 'lib/redis/distributed.rb', line 973

def zunion(*keys, **options)
  keys.flatten!(1)
  ensure_same_node(:zunion, keys) do |node|
    node.zunion(keys, **options)
  end
end

#zunionstore(destination, *keys, **options) ⇒ Object

Add multiple sorted sets and store the resulting sorted set in a new key.



981
982
983
984
985
986
# File 'lib/redis/distributed.rb', line 981

def zunionstore(destination, *keys, **options)
  keys.flatten!(1)
  ensure_same_node(:zunionstore, [destination].concat(keys)) do |node|
    node.zunionstore(destination, keys, **options)
  end
end