Class: Redis2::Distributed

Inherits:
Object
  • Object
show all
Defined in:
lib/redis2/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.



18
19
20
21
22
23
24
25
# File 'lib/redis2/distributed.rb', line 18

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.



16
17
18
# File 'lib/redis2/distributed.rb', line 16

def ring
  @ring
end

Instance Method Details

#[](key) ⇒ Object



341
342
343
# File 'lib/redis2/distributed.rb', line 341

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



345
346
347
# File 'lib/redis2/distributed.rb', line 345

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

#_bpop(cmd, args) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/redis2/distributed.rb', line 392

def _bpop(cmd, args)
  options = {}

  case args.last
  when Hash
    options = args.pop
  when Integer
    # Issue deprecation notice in obnoxious mode...
    options[:timeout] = args.pop
  end

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

  keys = args.flatten

  ensure_same_node(cmd, keys) do |node|
    node.__send__(cmd, keys, options)
  end
end

#_eval(cmd, args) ⇒ Object



789
790
791
792
793
794
795
796
797
798
799
800
# File 'lib/redis2/distributed.rb', line 789

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



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

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

#append(key, value) ⇒ Object

Append a value to a key.



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

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

#bgsaveObject

Asynchronously save the dataset to disk.



62
63
64
# File 'lib/redis2/distributed.rb', line 62

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.



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

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.



320
321
322
323
324
# File 'lib/redis2/distributed.rb', line 320

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.



327
328
329
# File 'lib/redis2/distributed.rb', line 327

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.



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

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.



422
423
424
# File 'lib/redis2/distributed.rb', line 422

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.



428
429
430
431
432
433
434
435
436
437
438
# File 'lib/redis2/distributed.rb', line 428

def brpoplpush(source, destination, options = {})
  case options
  when Integer
    # Issue deprecation notice in obnoxious mode...
    options = { :timeout => options }
  end

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

#dbsizeObject

Return the number of keys in the selected database.



67
68
69
# File 'lib/redis2/distributed.rb', line 67

def dbsize
  on_each_node :dbsize
end

#decr(key) ⇒ Object

Decrement the integer value of a key by one.



213
214
215
# File 'lib/redis2/distributed.rb', line 213

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

#decrby(key, decrement) ⇒ Object

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



218
219
220
# File 'lib/redis2/distributed.rb', line 218

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

#del(*args) ⇒ Object

Delete a key.



157
158
159
160
161
162
# File 'lib/redis2/distributed.rb', line 157

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:



780
781
782
# File 'lib/redis2/distributed.rb', line 780

def discard
  raise CannotDistribute, :discard
end

#dump(key) ⇒ Object

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



142
143
144
# File 'lib/redis2/distributed.rb', line 142

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

#dupObject



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

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

#echo(value) ⇒ Object

Echo the given string.



52
53
54
# File 'lib/redis2/distributed.rb', line 52

def echo(value)
  on_each_node :echo, value
end

#eval(*args) ⇒ Object

Evaluate Lua script.



803
804
805
# File 'lib/redis2/distributed.rb', line 803

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

#evalsha(*args) ⇒ Object

Evaluate Lua script by its SHA.



808
809
810
# File 'lib/redis2/distributed.rb', line 808

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

#execObject

Execute all commands issued after MULTI.

Raises:



775
776
777
# File 'lib/redis2/distributed.rb', line 775

def exec
  raise CannotDistribute, :exec
end

#exists(key) ⇒ Object

Determine if a key exists.



165
166
167
# File 'lib/redis2/distributed.rb', line 165

def exists(key)
  node_for(key).exists(key)
end

#expire(key, seconds) ⇒ Object

Set a key's time to live in seconds.



112
113
114
# File 'lib/redis2/distributed.rb', line 112

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.



117
118
119
# File 'lib/redis2/distributed.rb', line 117

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

#flushallObject

Remove all keys from all databases.



72
73
74
# File 'lib/redis2/distributed.rb', line 72

def flushall
  on_each_node :flushall
end

#flushdbObject

Remove all keys from the current database.



77
78
79
# File 'lib/redis2/distributed.rb', line 77

def flushdb
  on_each_node :flushdb
end

#get(key) ⇒ Object

Get the value of a key.



276
277
278
# File 'lib/redis2/distributed.rb', line 276

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.



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

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.



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

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.



332
333
334
# File 'lib/redis2/distributed.rb', line 332

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

#hdel(key, field) ⇒ Object

Delete one or more hash fields.



682
683
684
# File 'lib/redis2/distributed.rb', line 682

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

#hexists(key, field) ⇒ Object

Determine if a hash field exists.



687
688
689
# File 'lib/redis2/distributed.rb', line 687

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

#hget(key, field) ⇒ Object

Get the value of a hash field.



668
669
670
# File 'lib/redis2/distributed.rb', line 668

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

#hgetall(key) ⇒ Object

Get all the fields and values in a hash.



712
713
714
# File 'lib/redis2/distributed.rb', line 712

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.



692
693
694
# File 'lib/redis2/distributed.rb', line 692

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.



697
698
699
# File 'lib/redis2/distributed.rb', line 697

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

#hkeys(key) ⇒ Object

Get all the fields in a hash.



702
703
704
# File 'lib/redis2/distributed.rb', line 702

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

#hlen(key) ⇒ Object

Get the number of fields in a hash.



644
645
646
# File 'lib/redis2/distributed.rb', line 644

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

#hmget(key, *fields) ⇒ Object

Get the values of all the given hash fields.



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

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

#hmset(key, *attrs) ⇒ Object

Set multiple hash fields to multiple values.



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

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

#hset(key, field, value) ⇒ Object

Set the string value of a hash field.



649
650
651
# File 'lib/redis2/distributed.rb', line 649

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

#hsetnx(key, field, value) ⇒ Object

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



654
655
656
# File 'lib/redis2/distributed.rb', line 654

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

#hvals(key) ⇒ Object

Get all the values in a hash.



707
708
709
# File 'lib/redis2/distributed.rb', line 707

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

#incr(key) ⇒ Object

Increment the integer value of a key by one.



223
224
225
# File 'lib/redis2/distributed.rb', line 223

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.



228
229
230
# File 'lib/redis2/distributed.rb', line 228

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.



233
234
235
# File 'lib/redis2/distributed.rb', line 233

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

#info(cmd = nil) ⇒ Object

Get information and statistics about the server.



82
83
84
# File 'lib/redis2/distributed.rb', line 82

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

#inspectObject



812
813
814
# File 'lib/redis2/distributed.rb', line 812

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

#keys(glob = "*") ⇒ Object

Find all keys matching the given pattern.



170
171
172
# File 'lib/redis2/distributed.rb', line 170

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

#lastsaveObject

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



87
88
89
# File 'lib/redis2/distributed.rb', line 87

def lastsave
  on_each_node :lastsave
end

#lindex(key, index) ⇒ Object

Get an element from a list by its index.



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

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.



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

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

#llen(key) ⇒ Object

Get the length of a list.



350
351
352
# File 'lib/redis2/distributed.rb', line 350

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

#lpop(key) ⇒ Object

Remove and get the first element in a list.



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

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

#lpush(key, value) ⇒ Object

Prepend one or more values to a list.



355
356
357
# File 'lib/redis2/distributed.rb', line 355

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.



360
361
362
# File 'lib/redis2/distributed.rb', line 360

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

#lrange(key, start, stop) ⇒ Object

Get a range of elements from a list.



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

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

#lrem(key, count, value) ⇒ Object

Remove elements from a list.



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

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.



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

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.



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

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

#mapped_hmget(key, *fields) ⇒ Object



677
678
679
# File 'lib/redis2/distributed.rb', line 677

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

#mapped_hmset(key, hash) ⇒ Object



663
664
665
# File 'lib/redis2/distributed.rb', line 663

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

#mapped_mget(*keys) ⇒ Object

Raises:



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

def mapped_mget(*keys)
  raise CannotDistribute, :mapped_mget
end

#mapped_mset(hash) ⇒ Object

Raises:



262
263
264
# File 'lib/redis2/distributed.rb', line 262

def mapped_mset(hash)
  raise CannotDistribute, :mapped_mset
end

#mapped_msetnx(hash) ⇒ Object

Raises:



271
272
273
# File 'lib/redis2/distributed.rb', line 271

def mapped_msetnx(hash)
  raise CannotDistribute, :mapped_msetnx
end

#mget(*keys) ⇒ Object

Get the values of all the given keys.

Raises:



281
282
283
# File 'lib/redis2/distributed.rb', line 281

def mget(*keys)
  raise CannotDistribute, :mget
end

#migrate(key, options) ⇒ Object

Transfer a key from the connected instance to another instance.

Raises:



152
153
154
# File 'lib/redis2/distributed.rb', line 152

def migrate(key, options)
  raise CannotDistribute, :migrate
end

#monitorObject

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

Raises:

  • (NotImplementedError)


92
93
94
# File 'lib/redis2/distributed.rb', line 92

def monitor
  raise NotImplementedError
end

#move(key, db) ⇒ Object

Move a key to another database.



175
176
177
# File 'lib/redis2/distributed.rb', line 175

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

#mset(*args) ⇒ Object

Set multiple keys to multiple values.

Raises:



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

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

#msetnx(*args) ⇒ Object

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

Raises:



267
268
269
# File 'lib/redis2/distributed.rb', line 267

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

#multiObject

Mark the start of a transaction block.

Raises:



770
771
772
# File 'lib/redis2/distributed.rb', line 770

def multi
  raise CannotDistribute, :multi
end

#node_for(key) ⇒ Object



27
28
29
# File 'lib/redis2/distributed.rb', line 27

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

#nodesObject



31
32
33
# File 'lib/redis2/distributed.rb', line 31

def nodes
  @ring.nodes
end

#persist(key) ⇒ Object

Remove the expiration from a key.



107
108
109
# File 'lib/redis2/distributed.rb', line 107

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

#pexpire(key, milliseconds) ⇒ Object

Set a key's time to live in milliseconds.



127
128
129
# File 'lib/redis2/distributed.rb', line 127

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.



132
133
134
# File 'lib/redis2/distributed.rb', line 132

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

#pingObject

Ping the server.



47
48
49
# File 'lib/redis2/distributed.rb', line 47

def ping
  on_each_node :ping
end

#pipelinedObject

Raises:



765
766
767
# File 'lib/redis2/distributed.rb', line 765

def pipelined
  raise CannotDistribute, :pipelined
end

#psetex(key, ttl, value) ⇒ Object

Set the time to live in milliseconds of a key.



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

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)


745
746
747
# File 'lib/redis2/distributed.rb', line 745

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

#pttl(key) ⇒ Object

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



137
138
139
# File 'lib/redis2/distributed.rb', line 137

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

#publish(channel, message) ⇒ Object

Post a message to a channel.



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

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)


751
752
753
# File 'lib/redis2/distributed.rb', line 751

def punsubscribe(*channels)
  raise NotImplementedError
end

#quitObject

Close the connection.



57
58
59
# File 'lib/redis2/distributed.rb', line 57

def quit
  on_each_node :quit
end

#randomkeyObject

Return a random key from the keyspace.

Raises:



180
181
182
# File 'lib/redis2/distributed.rb', line 180

def randomkey
  raise CannotDistribute, :randomkey
end

#rename(old_name, new_name) ⇒ Object

Rename a key.



185
186
187
188
189
# File 'lib/redis2/distributed.rb', line 185

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.



192
193
194
195
196
# File 'lib/redis2/distributed.rb', line 192

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) ⇒ Object

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



147
148
149
# File 'lib/redis2/distributed.rb', line 147

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

#rpop(key) ⇒ Object

Remove and get the last element in a list.



380
381
382
# File 'lib/redis2/distributed.rb', line 380

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.



386
387
388
389
390
# File 'lib/redis2/distributed.rb', line 386

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.



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

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.



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

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

#sadd(key, member) ⇒ Object

Add one or more members to a set.



476
477
478
# File 'lib/redis2/distributed.rb', line 476

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

#saveObject

Synchronously save the dataset to disk.



97
98
99
# File 'lib/redis2/distributed.rb', line 97

def save
  on_each_node :save
end

#scard(key) ⇒ Object

Get the number of members in a set.



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

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

#script(subcommand, *args) ⇒ Object

Control remote script registry.



785
786
787
# File 'lib/redis2/distributed.rb', line 785

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

#sdiff(*keys) ⇒ Object

Subtract multiple sets.



513
514
515
516
517
# File 'lib/redis2/distributed.rb', line 513

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.



520
521
522
523
524
# File 'lib/redis2/distributed.rb', line 520

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.



42
43
44
# File 'lib/redis2/distributed.rb', line 42

def select(db)
  on_each_node :select, db
end

#set(key, value, options = {}) ⇒ Object

Set the string value of a key.



238
239
240
# File 'lib/redis2/distributed.rb', line 238

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.



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

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.



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

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.



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

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.



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

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

#sinter(*keys) ⇒ Object

Intersect multiple sets.



527
528
529
530
531
# File 'lib/redis2/distributed.rb', line 527

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.



534
535
536
537
538
# File 'lib/redis2/distributed.rb', line 534

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.



503
504
505
# File 'lib/redis2/distributed.rb', line 503

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

#smembers(key) ⇒ Object

Get all the members in a set.



508
509
510
# File 'lib/redis2/distributed.rb', line 508

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

#smove(source, destination, member) ⇒ Object

Move a member from one set to another.



496
497
498
499
500
# File 'lib/redis2/distributed.rb', line 496

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.



199
200
201
202
203
204
205
# File 'lib/redis2/distributed.rb', line 199

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) ⇒ Object

Remove and return a random member from a set.



486
487
488
# File 'lib/redis2/distributed.rb', line 486

def spop(key)
  node_for(key).spop(key)
end

#srandmember(key, count = nil) ⇒ Object

Get a random member from a set.



491
492
493
# File 'lib/redis2/distributed.rb', line 491

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

#srem(key, member) ⇒ Object

Remove one or more members from a set.



481
482
483
# File 'lib/redis2/distributed.rb', line 481

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

#strlen(key) ⇒ Object

Get the length of the value stored in a key.



337
338
339
# File 'lib/redis2/distributed.rb', line 337

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

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

Listen for messages published to the given channels.



726
727
728
729
730
731
732
733
734
735
736
# File 'lib/redis2/distributed.rb', line 726

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)


721
722
723
# File 'lib/redis2/distributed.rb', line 721

def subscribed?
  !! @subscribed_node
end

#sunion(*keys) ⇒ Object

Add multiple sets.



541
542
543
544
545
# File 'lib/redis2/distributed.rb', line 541

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.



548
549
550
551
552
# File 'lib/redis2/distributed.rb', line 548

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.



102
103
104
# File 'lib/redis2/distributed.rb', line 102

def time
  on_each_node :time
end

#ttl(key) ⇒ Object

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



122
123
124
# File 'lib/redis2/distributed.rb', line 122

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

#type(key) ⇒ Object

Determine the type stored at key.



208
209
210
# File 'lib/redis2/distributed.rb', line 208

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

#unsubscribe(*channels) ⇒ Object

Stop listening for messages posted to the given channels.

Raises:

  • (RuntimeError)


739
740
741
742
# File 'lib/redis2/distributed.rb', line 739

def unsubscribe(*channels)
  raise RuntimeError, "Can't unsubscribe if not subscribed." unless subscribed?
  @subscribed_node.unsubscribe(*channels)
end

#unwatchObject

Forget about all watched keys.

Raises:



761
762
763
# File 'lib/redis2/distributed.rb', line 761

def unwatch
  raise CannotDistribute, :unwatch
end

#watch(*keys) ⇒ Object

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

Raises:



756
757
758
# File 'lib/redis2/distributed.rb', line 756

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.



561
562
563
# File 'lib/redis2/distributed.rb', line 561

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

#zcard(key) ⇒ Object

Get the number of members in a sorted set.



555
556
557
# File 'lib/redis2/distributed.rb', line 555

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

#zcount(key, min, max) ⇒ Object

Get the number of members in a particular score range.



624
625
626
# File 'lib/redis2/distributed.rb', line 624

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.



566
567
568
# File 'lib/redis2/distributed.rb', line 566

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.



630
631
632
633
634
# File 'lib/redis2/distributed.rb', line 630

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.



581
582
583
# File 'lib/redis2/distributed.rb', line 581

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.



608
609
610
# File 'lib/redis2/distributed.rb', line 608

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.



592
593
594
# File 'lib/redis2/distributed.rb', line 592

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

#zrem(key, member) ⇒ Object

Remove one or more members from a sorted set.



571
572
573
# File 'lib/redis2/distributed.rb', line 571

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.



603
604
605
# File 'lib/redis2/distributed.rb', line 603

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.



619
620
621
# File 'lib/redis2/distributed.rb', line 619

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.



587
588
589
# File 'lib/redis2/distributed.rb', line 587

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.



614
615
616
# File 'lib/redis2/distributed.rb', line 614

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.



598
599
600
# File 'lib/redis2/distributed.rb', line 598

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.



576
577
578
# File 'lib/redis2/distributed.rb', line 576

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.



637
638
639
640
641
# File 'lib/redis2/distributed.rb', line 637

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