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



20
21
22
23
24
25
26
27
# File 'lib/redis/distributed.rb', line 20

def initialize(node_configs, options = {})
  @tag = options[:tag] || /^\{(.+?)\}/
  @ring = options[:ring] || HashRing.new
  @node_configs = node_configs.dup
  @default_options = options.dup
  node_configs.each { |node_config| add_node(node_config) }
  @subscribed_node = 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



379
380
381
# File 'lib/redis/distributed.rb', line 379

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



383
384
385
# File 'lib/redis/distributed.rb', line 383

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

#_bpop(cmd, args) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/redis/distributed.rb', line 430

def _bpop(cmd, args)
  timeout = if args.last.is_a?(Hash)
    options = args.pop
    options[:timeout]
  elsif args.last.respond_to?(:to_int)
    # Issue deprecation notice in obnoxious mode...
    args.pop.to_int
  end

  if args.size > 1
    # Issue deprecation notice in obnoxious mode...
  end

  keys = args.flatten

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

#_eval(cmd, args) ⇒ Object



855
856
857
858
859
860
861
862
863
864
865
866
# File 'lib/redis/distributed.rb', line 855

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



37
38
39
40
41
# File 'lib/redis/distributed.rb', line 37

def add_node(options)
  options = { url: options } if options.is_a?(String)
  options = @default_options.merge(options)
  @ring.add_node Redis.new(options)
end

#append(key, value) ⇒ Object

Append a value to a key.



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

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

#bgsaveObject

Asynchronously save the dataset to disk.



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

def bgsave
  on_each_node :bgsave
end

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

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



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

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

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

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



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

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

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

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



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

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

#blpop(*args) ⇒ Object

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



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

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.



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

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

#brpoplpush(source, destination, deprecated_timeout = 0, **options) ⇒ Object

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



468
469
470
471
472
# File 'lib/redis/distributed.rb', line 468

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

#dbsizeObject

Return the number of keys in the selected database.



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

def dbsize
  on_each_node :dbsize
end

#decr(key) ⇒ Object

Decrement the integer value of a key by one.



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

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

#decrby(key, decrement) ⇒ Object

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



253
254
255
# File 'lib/redis/distributed.rb', line 253

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

#del(*args) ⇒ Object

Delete a key.



159
160
161
162
163
164
# File 'lib/redis/distributed.rb', line 159

def del(*args)
  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:



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

def discard
  raise CannotDistribute, :discard
end

#dump(key) ⇒ Object

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



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

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

#dupObject



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

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

#echo(value) ⇒ Object

Echo the given string.



54
55
56
# File 'lib/redis/distributed.rb', line 54

def echo(value)
  on_each_node :echo, value
end

#eval(*args) ⇒ Object

Evaluate Lua script.



869
870
871
# File 'lib/redis/distributed.rb', line 869

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

#evalsha(*args) ⇒ Object

Evaluate Lua script by its SHA.



874
875
876
# File 'lib/redis/distributed.rb', line 874

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

#execObject

Execute all commands issued after MULTI.

Raises:



821
822
823
# File 'lib/redis/distributed.rb', line 821

def exec
  raise CannotDistribute, :exec
end

#exists(*args) ⇒ Object

Determine if a key exists.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/redis/distributed.rb', line 175

def exists(*args)
  if !Redis.exists_returns_integer && args.size == 1
    message = "`Redis#exists(key)` will return an Integer in redis-rb 4.3, if you want to keep the old behavior, " \
      "use `exists?` instead. To opt-in to the new behavior now you can set Redis.exists_returns_integer = true. " \
      "(#{::Kernel.caller(1, 1).first})\n"

    if defined?(::Warning)
      ::Warning.warn(message)
    else
      warn(message)
    end
    exists?(*args)
  else
    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
end

#exists?(*args) ⇒ Boolean

Determine if any of the keys exists.



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

def exists?(*args)
  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) ⇒ Object

Set a key’s time to live in seconds.



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

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

#expireat(key, unix_time) ⇒ Object

Set the expiration for a key as a UNIX timestamp.



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

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

#flushallObject

Remove all keys from all databases.



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

def flushall
  on_each_node :flushall
end

#flushdbObject

Remove all keys from the current database.



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

def flushdb
  on_each_node :flushdb
end

#get(key) ⇒ Object

Get the value of a key.



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

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.



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

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

#getrange(key, start, stop) ⇒ Object

Get a substring of the string stored at a key.



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

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.



370
371
372
# File 'lib/redis/distributed.rb', line 370

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

#hdel(key, *fields) ⇒ Object

Delete one or more hash fields.



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

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

#hexists(key, field) ⇒ Object

Determine if a hash field exists.



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

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

#hget(key, field) ⇒ Object

Get the value of a hash field.



713
714
715
# File 'lib/redis/distributed.rb', line 713

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

#hgetall(key) ⇒ Object

Get all the fields and values in a hash.



757
758
759
# File 'lib/redis/distributed.rb', line 757

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

#hincrby(key, field, increment) ⇒ Object

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



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

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.



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

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

#hkeys(key) ⇒ Object

Get all the fields in a hash.



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

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

#hlen(key) ⇒ Object

Get the number of fields in a hash.



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

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

#hmget(key, *fields) ⇒ Object

Get the values of all the given hash fields.



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

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

#hmset(key, *attrs) ⇒ Object

Set multiple hash fields to multiple values.



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

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

#hset(key, *attrs) ⇒ Object

Set multiple hash fields to multiple values.



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

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.



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

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

#hvals(key) ⇒ Object

Get all the values in a hash.



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

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

#incr(key) ⇒ Object

Increment the integer value of a key by one.



258
259
260
# File 'lib/redis/distributed.rb', line 258

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.



263
264
265
# File 'lib/redis/distributed.rb', line 263

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.



268
269
270
# File 'lib/redis/distributed.rb', line 268

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

#info(cmd = nil) ⇒ Object

Get information and statistics about the server.



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

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

#inspectObject



878
879
880
# File 'lib/redis/distributed.rb', line 878

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

#keys(glob = "*") ⇒ Object

Find all keys matching the given pattern.



205
206
207
# File 'lib/redis/distributed.rb', line 205

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

#lastsaveObject

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



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

def lastsave
  on_each_node :lastsave
end

#lindex(key, index) ⇒ Object

Get an element from a list by its index.



475
476
477
# File 'lib/redis/distributed.rb', line 475

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.



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

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

#llen(key) ⇒ Object

Get the length of a list.



388
389
390
# File 'lib/redis/distributed.rb', line 388

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

#lpop(key) ⇒ Object

Remove and get the first element in a list.



413
414
415
# File 'lib/redis/distributed.rb', line 413

def lpop(key)
  node_for(key).lpop(key)
end

#lpush(key, value) ⇒ Object

Prepend one or more values to a list.



393
394
395
# File 'lib/redis/distributed.rb', line 393

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.



398
399
400
# File 'lib/redis/distributed.rb', line 398

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

#lrange(key, start, stop) ⇒ Object

Get a range of elements from a list.



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

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

#lrem(key, count, value) ⇒ Object

Remove elements from a list.



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

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.



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

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.



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

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

#mapped_hmget(key, *fields) ⇒ Object



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

def mapped_hmget(key, *fields)
  Hash[*fields.zip(hmget(key, *fields)).flatten]
end

#mapped_hmset(key, hash) ⇒ Object



708
709
710
# File 'lib/redis/distributed.rb', line 708

def mapped_hmset(key, hash)
  node_for(key).hmset(key, *hash.to_a.flatten)
end

#mapped_mget(*keys) ⇒ Object

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



321
322
323
324
325
# File 'lib/redis/distributed.rb', line 321

def mapped_mget(*keys)
  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:



297
298
299
# File 'lib/redis/distributed.rb', line 297

def mapped_mset(_hash)
  raise CannotDistribute, :mapped_mset
end

#mapped_msetnx(_hash) ⇒ Object

Raises:



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

def mapped_msetnx(_hash)
  raise CannotDistribute, :mapped_msetnx
end

#mget(*keys) ⇒ Object

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



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

def mget(*keys)
  mapped_mget(*keys).values_at(*keys)
end

#migrate(_key, _options) ⇒ Object

Transfer a key from the connected instance to another instance.

Raises:



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

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

#monitorObject

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

Raises:

  • (NotImplementedError)


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

def monitor
  raise NotImplementedError
end

#move(key, db) ⇒ Object

Move a key to another database.



210
211
212
# File 'lib/redis/distributed.rb', line 210

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

#mset(*_args) ⇒ Object

Set multiple keys to multiple values.

Raises:



293
294
295
# File 'lib/redis/distributed.rb', line 293

def mset(*_args)
  raise CannotDistribute, :mset
end

#msetnx(*_args) ⇒ Object

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

Raises:



302
303
304
# File 'lib/redis/distributed.rb', line 302

def msetnx(*_args)
  raise CannotDistribute, :msetnx
end

#multiObject

Mark the start of a transaction block.

Raises:



816
817
818
# File 'lib/redis/distributed.rb', line 816

def multi
  raise CannotDistribute, :multi
end

#node_for(key) ⇒ Object



29
30
31
# File 'lib/redis/distributed.rb', line 29

def node_for(key)
  @ring.get_node(key_tag(key.to_s) || key.to_s)
end

#nodesObject



33
34
35
# File 'lib/redis/distributed.rb', line 33

def nodes
  @ring.nodes
end

#persist(key) ⇒ Object

Remove the expiration from a key.



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

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

#pexpire(key, milliseconds) ⇒ Object

Set a key’s time to live in milliseconds.



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

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

#pexpireat(key, ms_unix_time) ⇒ Object

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



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

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

#pfadd(key, member) ⇒ Object

Add one or more members to a HyperLogLog structure.



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

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

#pfcount(*keys) ⇒ Object

Get the approximate cardinality of members added to HyperLogLog structure.



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

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.



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

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.



49
50
51
# File 'lib/redis/distributed.rb', line 49

def ping
  on_each_node :ping
end

#pipelinedObject

Raises:



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

def pipelined
  raise CannotDistribute, :pipelined
end

#psetex(key, ttl, value) ⇒ Object

Set the time to live in milliseconds of a key.



283
284
285
# File 'lib/redis/distributed.rb', line 283

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.

Raises:

  • (NotImplementedError)


791
792
793
# File 'lib/redis/distributed.rb', line 791

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

#pttl(key) ⇒ Object

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



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

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

#publish(channel, message) ⇒ Object

Post a message to a channel.



762
763
764
# File 'lib/redis/distributed.rb', line 762

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.

Raises:

  • (NotImplementedError)


797
798
799
# File 'lib/redis/distributed.rb', line 797

def punsubscribe(*channels)
  raise NotImplementedError
end

#quitObject

Close the connection.



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

def quit
  on_each_node :quit
end

#randomkeyObject

Return a random key from the keyspace.

Raises:



215
216
217
# File 'lib/redis/distributed.rb', line 215

def randomkey
  raise CannotDistribute, :randomkey
end

#rename(old_name, new_name) ⇒ Object

Rename a key.



220
221
222
223
224
# File 'lib/redis/distributed.rb', line 220

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.



227
228
229
230
231
# File 'lib/redis/distributed.rb', line 227

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.



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

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

#rpop(key) ⇒ Object

Remove and get the last element in a list.



418
419
420
# File 'lib/redis/distributed.rb', line 418

def rpop(key)
  node_for(key).rpop(key)
end

#rpoplpush(source, destination) ⇒ Object

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



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

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.



403
404
405
# File 'lib/redis/distributed.rb', line 403

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.



408
409
410
# File 'lib/redis/distributed.rb', line 408

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

#sadd(key, member) ⇒ Object

Add one or more members to a set.



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

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

#saveObject

Synchronously save the dataset to disk.



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

def save
  on_each_node :save
end

#scard(key) ⇒ Object

Get the number of members in a set.



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

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

#script(subcommand, *args) ⇒ Object

Control remote script registry.



831
832
833
# File 'lib/redis/distributed.rb', line 831

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

#sdiff(*keys) ⇒ Object

Subtract multiple sets.



557
558
559
560
561
# File 'lib/redis/distributed.rb', line 557

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

#sdiffstore(destination, *keys) ⇒ Object

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



564
565
566
567
568
# File 'lib/redis/distributed.rb', line 564

def sdiffstore(destination, *keys)
  ensure_same_node(:sdiffstore, [destination] + keys) do |node|
    node.sdiffstore(destination, *keys)
  end
end

#select(db) ⇒ Object

Change the selected database for the current connection.



44
45
46
# File 'lib/redis/distributed.rb', line 44

def select(db)
  on_each_node :select, db
end

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

Set the string value of a key.



273
274
275
# File 'lib/redis/distributed.rb', line 273

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.



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

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.



278
279
280
# File 'lib/redis/distributed.rb', line 278

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.



288
289
290
# File 'lib/redis/distributed.rb', line 288

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.



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

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

#sinter(*keys) ⇒ Object

Intersect multiple sets.



571
572
573
574
575
# File 'lib/redis/distributed.rb', line 571

def sinter(*keys)
  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.



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

def sinterstore(destination, *keys)
  ensure_same_node(:sinterstore, [destination] + keys) do |node|
    node.sinterstore(destination, *keys)
  end
end

#sismember(key, member) ⇒ Object

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



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

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

#smembers(key) ⇒ Object

Get all the members in a set.



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

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

#smove(source, destination, member) ⇒ Object

Move a member from one set to another.



530
531
532
533
534
# File 'lib/redis/distributed.rb', line 530

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.



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

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.



520
521
522
# File 'lib/redis/distributed.rb', line 520

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

#srandmember(key, count = nil) ⇒ Object

Get a random member from a set.



525
526
527
# File 'lib/redis/distributed.rb', line 525

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

#srem(key, member) ⇒ Object

Remove one or more members from a set.



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

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

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

Scan a set



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

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



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

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.



375
376
377
# File 'lib/redis/distributed.rb', line 375

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

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

Listen for messages published to the given channels.



771
772
773
774
775
776
777
778
779
780
781
# File 'lib/redis/distributed.rb', line 771

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



766
767
768
# File 'lib/redis/distributed.rb', line 766

def subscribed?
  !!@subscribed_node
end

#sunion(*keys) ⇒ Object

Add multiple sets.



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

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

#sunionstore(destination, *keys) ⇒ Object

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



592
593
594
595
596
# File 'lib/redis/distributed.rb', line 592

def sunionstore(destination, *keys)
  ensure_same_node(:sunionstore, [destination] + keys) do |node|
    node.sunionstore(destination, *keys)
  end
end

#timeObject

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



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

def time
  on_each_node :time
end

#ttl(key) ⇒ Object

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



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

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

#type(key) ⇒ Object

Determine the type stored at key.



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

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

Unlink keys.



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

def unlink(*args)
  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.



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

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

  @subscribed_node.unsubscribe(*channels)
end

#unwatchObject

Forget about all watched keys.

Raises:



807
808
809
# File 'lib/redis/distributed.rb', line 807

def unwatch
  raise CannotDistribute, :unwatch
end

#watch(*_keys) ⇒ Object

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

Raises:



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

def watch(*_keys)
  raise CannotDistribute, :watch
end

#zadd(key, *args) ⇒ Object

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



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

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

#zcard(key) ⇒ Object

Get the number of members in a sorted set.



599
600
601
# File 'lib/redis/distributed.rb', line 599

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

#zcount(key, min, max) ⇒ Object

Get the number of members in a particular score range.



669
670
671
# File 'lib/redis/distributed.rb', line 669

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

#zincrby(key, increment, member) ⇒ Object

Increment the score of a member in a sorted set.



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

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

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

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



675
676
677
678
679
# File 'lib/redis/distributed.rb', line 675

def zinterstore(destination, keys, **options)
  ensure_same_node(:zinterstore, [destination] + keys) do |node|
    node.zinterstore(destination, keys, **options)
  end
end

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

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



626
627
628
# File 'lib/redis/distributed.rb', line 626

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.



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

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

#zrank(key, member) ⇒ Object

Determine the index of a member in a sorted set.



637
638
639
# File 'lib/redis/distributed.rb', line 637

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

#zrem(key, member) ⇒ Object

Remove one or more members from a sorted set.



616
617
618
# File 'lib/redis/distributed.rb', line 616

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.



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

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.



664
665
666
# File 'lib/redis/distributed.rb', line 664

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.



632
633
634
# File 'lib/redis/distributed.rb', line 632

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.



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

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

#zrevrank(key, member) ⇒ Object

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



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

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

#zscore(key, member) ⇒ Object

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



621
622
623
# File 'lib/redis/distributed.rb', line 621

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

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

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



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

def zunionstore(destination, keys, **options)
  ensure_same_node(:zunionstore, [destination] + keys) do |node|
    node.zunionstore(destination, keys, **options)
  end
end