class Redis::Distributed

Attributes

ring[R]

Public Class Methods

new(node_configs, options = {}) click to toggle source
# 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
  @watch_key = nil
end

Public Instance Methods

[](key) click to toggle source
# File lib/redis/distributed.rb, line 396
def [](key)
  get(key)
end
[]=(key, value) click to toggle source
# File lib/redis/distributed.rb, line 400
def []=(key, value)
  set(key, value)
end
_bpop(cmd, args) click to toggle source
# File lib/redis/distributed.rb, line 462
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) click to toggle source
# File lib/redis/distributed.rb, line 968
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) click to toggle source
# File lib/redis/distributed.rb, line 41
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) click to toggle source

Append a value to a key.

# File lib/redis/distributed.rb, line 365
def append(key, value)
  node_for(key).append(key, value)
end
bgsave() click to toggle source

Asynchronously save the dataset to disk.

# File lib/redis/distributed.rb, line 68
def bgsave
  on_each_node :bgsave
end
bitcount(key, start = 0, stop = -1) click to toggle source

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

# File lib/redis/distributed.rb, line 370
def bitcount(key, start = 0, stop = -1)
  node_for(key).bitcount(key, start, stop)
end
bitop(operation, destkey, *keys) click to toggle source

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

# File lib/redis/distributed.rb, line 375
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) click to toggle source

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

# File lib/redis/distributed.rb, line 382
def bitpos(key, bit, start = nil, stop = nil)
  node_for(key).bitpos(key, bit, start, stop)
end
blmove(source, destination, where_source, where_destination, timeout: 0) click to toggle source

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

# File lib/redis/distributed.rb, line 418
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
blpop(*args) click to toggle source

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

# File lib/redis/distributed.rb, line 488
def blpop(*args)
  _bpop(:blpop, args)
end
brpop(*args) click to toggle source

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

# File lib/redis/distributed.rb, line 494
def brpop(*args)
  _bpop(:brpop, args)
end
brpoplpush(source, destination, deprecated_timeout = 0, **options) click to toggle source

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

# File lib/redis/distributed.rb, line 500
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
copy(source, destination, **options) click to toggle source

Copy a value from one key to another.

# File lib/redis/distributed.rb, line 215
def copy(source, destination, **options)
  ensure_same_node(:copy, [source, destination]) do |node|
    node.copy(source, destination, **options)
  end
end
dbsize() click to toggle source

Return the number of keys in the selected database.

# File lib/redis/distributed.rb, line 73
def dbsize
  on_each_node :dbsize
end
decr(key) click to toggle source

Decrement the integer value of a key by one.

# File lib/redis/distributed.rb, line 255
def decr(key)
  node_for(key).decr(key)
end
decrby(key, decrement) click to toggle source

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

# File lib/redis/distributed.rb, line 260
def decrby(key, decrement)
  node_for(key).decrby(key, decrement)
end
del(*args) click to toggle source

Delete a key.

# File lib/redis/distributed.rb, line 163
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
discard() click to toggle source

Discard all commands issued after MULTI.

# File lib/redis/distributed.rb, line 935
def discard
  raise CannotDistribute, :discard unless @watch_key

  result = node_for(@watch_key).discard
  @watch_key = nil
  result
end
dump(key) click to toggle source

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

# File lib/redis/distributed.rb, line 148
def dump(key)
  node_for(key).dump(key)
end
dup() click to toggle source
# File lib/redis/distributed.rb, line 995
def dup
  self.class.new(@node_configs, @default_options)
end
echo(value) click to toggle source

Echo the given string.

# File lib/redis/distributed.rb, line 58
def echo(value)
  on_each_node :echo, value
end
eval(*args) click to toggle source

Evaluate Lua script.

# File lib/redis/distributed.rb, line 982
def eval(*args)
  _eval(:eval, args)
end
evalsha(*args) click to toggle source

Evaluate Lua script by its SHA.

# File lib/redis/distributed.rb, line 987
def evalsha(*args)
  _eval(:evalsha, args)
end
exec() click to toggle source

Execute all commands issued after MULTI.

# File lib/redis/distributed.rb, line 926
def exec
  raise CannotDistribute, :exec unless @watch_key

  result = node_for(@watch_key).exec
  @watch_key = nil
  result
end
exists(*args) click to toggle source

Determine if a key exists.

# File lib/redis/distributed.rb, line 179
def exists(*args)
  if !Redis.exists_returns_integer && args.size == 1
    ::Redis.deprecate!(
      "`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"
    )
    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) click to toggle source

Determine if any of the keys exists.

# 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) click to toggle source

Set a key’s time to live in seconds.

# File lib/redis/distributed.rb, line 118
def expire(key, seconds)
  node_for(key).expire(key, seconds)
end
expireat(key, unix_time) click to toggle source

Set the expiration for a key as a UNIX timestamp.

# File lib/redis/distributed.rb, line 123
def expireat(key, unix_time)
  node_for(key).expireat(key, unix_time)
end
flushall() click to toggle source

Remove all keys from all databases.

# File lib/redis/distributed.rb, line 78
def flushall
  on_each_node :flushall
end
flushdb() click to toggle source

Remove all keys from the current database.

# File lib/redis/distributed.rb, line 83
def flushdb
  on_each_node :flushdb
end
get(key) click to toggle source

Get the value of a key.

# File lib/redis/distributed.rb, line 318
def get(key)
  node_for(key).get(key)
end
getbit(key, offset) click to toggle source

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

# File lib/redis/distributed.rb, line 360
def getbit(key, offset)
  node_for(key).getbit(key, offset)
end
getdel(key) click to toggle source

Get the value of a key and delete it.

# File lib/redis/distributed.rb, line 323
def getdel(key)
  node_for(key).getdel(key)
end
getex(key, **options) click to toggle source

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

# File lib/redis/distributed.rb, line 328
def getex(key, **options)
  node_for(key).getex(key, **options)
end
getrange(key, start, stop) click to toggle source

Get a substring of the string stored at a key.

# File lib/redis/distributed.rb, line 350
def getrange(key, start, stop)
  node_for(key).getrange(key, start, stop)
end
getset(key, value) click to toggle source

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

# File lib/redis/distributed.rb, line 387
def getset(key, value)
  node_for(key).getset(key, value)
end
hdel(key, *fields) click to toggle source

Delete one or more hash fields.

# File lib/redis/distributed.rb, line 815
def hdel(key, *fields)
  node_for(key).hdel(key, *fields)
end
hexists(key, field) click to toggle source

Determine if a hash field exists.

# File lib/redis/distributed.rb, line 820
def hexists(key, field)
  node_for(key).hexists(key, field)
end
hget(key, field) click to toggle source

Get the value of a hash field.

# File lib/redis/distributed.rb, line 797
def hget(key, field)
  node_for(key).hget(key, field)
end
hgetall(key) click to toggle source

Get all the fields and values in a hash.

# File lib/redis/distributed.rb, line 845
def hgetall(key)
  node_for(key).hgetall(key)
end
hincrby(key, field, increment) click to toggle source

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

# File lib/redis/distributed.rb, line 825
def hincrby(key, field, increment)
  node_for(key).hincrby(key, field, increment)
end
hincrbyfloat(key, field, increment) click to toggle source

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

# File lib/redis/distributed.rb, line 830
def hincrbyfloat(key, field, increment)
  node_for(key).hincrbyfloat(key, field, increment)
end
hkeys(key) click to toggle source

Get all the fields in a hash.

# File lib/redis/distributed.rb, line 835
def hkeys(key)
  node_for(key).hkeys(key)
end
hlen(key) click to toggle source

Get the number of fields in a hash.

# File lib/redis/distributed.rb, line 773
def hlen(key)
  node_for(key).hlen(key)
end
hmget(key, *fields) click to toggle source

Get the values of all the given hash fields.

# File lib/redis/distributed.rb, line 802
def hmget(key, *fields)
  node_for(key).hmget(key, *fields)
end
hmset(key, *attrs) click to toggle source

Set multiple hash fields to multiple values.

# File lib/redis/distributed.rb, line 788
def hmset(key, *attrs)
  node_for(key).hmset(key, *attrs)
end
hrandfield(key, count = nil, **options) click to toggle source
# File lib/redis/distributed.rb, line 810
def hrandfield(key, count = nil, **options)
  node_for(key).hrandfield(key, count, **options)
end
hset(key, *attrs) click to toggle source

Set multiple hash fields to multiple values.

# File lib/redis/distributed.rb, line 778
def hset(key, *attrs)
  node_for(key).hset(key, *attrs)
end
hsetnx(key, field, value) click to toggle source

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

# File lib/redis/distributed.rb, line 783
def hsetnx(key, field, value)
  node_for(key).hsetnx(key, field, value)
end
hvals(key) click to toggle source

Get all the values in a hash.

# File lib/redis/distributed.rb, line 840
def hvals(key)
  node_for(key).hvals(key)
end
incr(key) click to toggle source

Increment the integer value of a key by one.

# File lib/redis/distributed.rb, line 265
def incr(key)
  node_for(key).incr(key)
end
incrby(key, increment) click to toggle source

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

# File lib/redis/distributed.rb, line 270
def incrby(key, increment)
  node_for(key).incrby(key, increment)
end
incrbyfloat(key, increment) click to toggle source

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

# File lib/redis/distributed.rb, line 275
def incrbyfloat(key, increment)
  node_for(key).incrbyfloat(key, increment)
end
info(cmd = nil) click to toggle source

Get information and statistics about the server.

# File lib/redis/distributed.rb, line 88
def info(cmd = nil)
  on_each_node :info, cmd
end
inspect() click to toggle source
# File lib/redis/distributed.rb, line 991
def inspect
  "#<Redis client v#{Redis::VERSION} for #{nodes.map(&:id).join(', ')}>"
end
keys(glob = "*") click to toggle source

Find all keys matching the given pattern.

# File lib/redis/distributed.rb, line 205
def keys(glob = "*")
  on_each_node(:keys, glob).flatten
end
lastsave() click to toggle source

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

# File lib/redis/distributed.rb, line 93
def lastsave
  on_each_node :lastsave
end
lindex(key, index) click to toggle source

Get an element from a list by its index.

# File lib/redis/distributed.rb, line 507
def lindex(key, index)
  node_for(key).lindex(key, index)
end
linsert(key, where, pivot, value) click to toggle source

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

# File lib/redis/distributed.rb, line 512
def linsert(key, where, pivot, value)
  node_for(key).linsert(key, where, pivot, value)
end
llen(key) click to toggle source

Get the length of a list.

# File lib/redis/distributed.rb, line 405
def llen(key)
  node_for(key).llen(key)
end
lmove(source, destination, where_source, where_destination) click to toggle source

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

# File lib/redis/distributed.rb, line 410
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
lpop(key, count = nil) click to toggle source

Remove and get the first elements in a list.

# File lib/redis/distributed.rb, line 445
def lpop(key, count = nil)
  node_for(key).lpop(key, count)
end
lpush(key, value) click to toggle source

Prepend one or more values to a list.

# File lib/redis/distributed.rb, line 425
def lpush(key, value)
  node_for(key).lpush(key, value)
end
lpushx(key, value) click to toggle source

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

# File lib/redis/distributed.rb, line 430
def lpushx(key, value)
  node_for(key).lpushx(key, value)
end
lrange(key, start, stop) click to toggle source

Get a range of elements from a list.

# File lib/redis/distributed.rb, line 517
def lrange(key, start, stop)
  node_for(key).lrange(key, start, stop)
end
lrem(key, count, value) click to toggle source

Remove elements from a list.

# File lib/redis/distributed.rb, line 522
def lrem(key, count, value)
  node_for(key).lrem(key, count, value)
end
lset(key, index, value) click to toggle source

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

# File lib/redis/distributed.rb, line 527
def lset(key, index, value)
  node_for(key).lset(key, index, value)
end
ltrim(key, start, stop) click to toggle source

Trim a list to the specified range.

# File lib/redis/distributed.rb, line 532
def ltrim(key, start, stop)
  node_for(key).ltrim(key, start, stop)
end
mapped_hmget(key, *fields) click to toggle source
# File lib/redis/distributed.rb, line 806
def mapped_hmget(key, *fields)
  Hash[*fields.zip(hmget(key, *fields)).flatten]
end
mapped_hmset(key, hash) click to toggle source
# File lib/redis/distributed.rb, line 792
def mapped_hmset(key, hash)
  node_for(key).hmset(key, *hash.to_a.flatten)
end
mapped_mget(*keys) click to toggle source

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

# File lib/redis/distributed.rb, line 338
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) click to toggle source
# File lib/redis/distributed.rb, line 304
def mapped_mset(_hash)
  raise CannotDistribute, :mapped_mset
end
mapped_msetnx(_hash) click to toggle source
# File lib/redis/distributed.rb, line 313
def mapped_msetnx(_hash)
  raise CannotDistribute, :mapped_msetnx
end
mget(*keys) click to toggle source

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

# File lib/redis/distributed.rb, line 333
def mget(*keys)
  mapped_mget(*keys).values_at(*keys)
end
migrate(_key, _options) click to toggle source

Transfer a key from the connected instance to another instance.

# File lib/redis/distributed.rb, line 158
def migrate(_key, _options)
  raise CannotDistribute, :migrate
end
monitor() click to toggle source

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

# File lib/redis/distributed.rb, line 98
def monitor
  raise NotImplementedError
end
move(key, db) click to toggle source

Move a key to another database.

# File lib/redis/distributed.rb, line 210
def move(key, db)
  node_for(key).move(key, db)
end
mset(*_args) click to toggle source

Set multiple keys to multiple values.

# File lib/redis/distributed.rb, line 300
def mset(*_args)
  raise CannotDistribute, :mset
end
msetnx(*_args) click to toggle source

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

# File lib/redis/distributed.rb, line 309
def msetnx(*_args)
  raise CannotDistribute, :msetnx
end
multi(&block) click to toggle source

Mark the start of a transaction block.

# File lib/redis/distributed.rb, line 917
def multi(&block)
  raise CannotDistribute, :multi unless @watch_key

  result = node_for(@watch_key).multi(&block)
  @watch_key = nil if block_given?
  result
end
node_for(key) click to toggle source
# File lib/redis/distributed.rb, line 30
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
nodes() click to toggle source
# File lib/redis/distributed.rb, line 37
def nodes
  @ring.nodes
end
persist(key) click to toggle source

Remove the expiration from a key.

# File lib/redis/distributed.rb, line 113
def persist(key)
  node_for(key).persist(key)
end
pexpire(key, milliseconds) click to toggle source

Set a key’s time to live in milliseconds.

# File lib/redis/distributed.rb, line 133
def pexpire(key, milliseconds)
  node_for(key).pexpire(key, milliseconds)
end
pexpireat(key, ms_unix_time) click to toggle source

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

# File lib/redis/distributed.rb, line 138
def pexpireat(key, ms_unix_time)
  node_for(key).pexpireat(key, ms_unix_time)
end
pfadd(key, member) click to toggle source

Add one or more members to a HyperLogLog structure.

# File lib/redis/distributed.rb, line 949
def pfadd(key, member)
  node_for(key).pfadd(key, member)
end
pfcount(*keys) click to toggle source

Get the approximate cardinality of members added to HyperLogLog structure.

# File lib/redis/distributed.rb, line 954
def pfcount(*keys)
  ensure_same_node(:pfcount, keys.flatten(1)) do |node|
    node.pfcount(keys)
  end
end
pfmerge(dest_key, *source_key) click to toggle source

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.

# File lib/redis/distributed.rb, line 962
def pfmerge(dest_key, *source_key)
  ensure_same_node(:pfmerge, [dest_key, *source_key]) do |node|
    node.pfmerge(dest_key, *source_key)
  end
end
ping() click to toggle source

Ping the server.

# File lib/redis/distributed.rb, line 53
def ping
  on_each_node :ping
end
pipelined() click to toggle source
# File lib/redis/distributed.rb, line 912
def pipelined
  raise CannotDistribute, :pipelined
end
psetex(key, ttl, value) click to toggle source

Set the time to live in milliseconds of a key.

# File lib/redis/distributed.rb, line 290
def psetex(key, ttl, value)
  node_for(key).psetex(key, ttl, value)
end
psubscribe(*channels, &block) click to toggle source

Listen for messages published to channels matching the given patterns.

# File lib/redis/distributed.rb, line 879
def psubscribe(*channels, &block)
  raise NotImplementedError
end
pttl(key) click to toggle source

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

# File lib/redis/distributed.rb, line 143
def pttl(key)
  node_for(key).pttl(key)
end
publish(channel, message) click to toggle source

Post a message to a channel.

# File lib/redis/distributed.rb, line 850
def publish(channel, message)
  node_for(channel).publish(channel, message)
end
punsubscribe(*channels) click to toggle source

Stop listening for messages posted to channels matching the given patterns.

# File lib/redis/distributed.rb, line 885
def punsubscribe(*channels)
  raise NotImplementedError
end
quit() click to toggle source

Close the connection.

# File lib/redis/distributed.rb, line 63
def quit
  on_each_node :quit
end
randomkey() click to toggle source

Return a random key from the keyspace.

# File lib/redis/distributed.rb, line 222
def randomkey
  raise CannotDistribute, :randomkey
end
rename(old_name, new_name) click to toggle source

Rename a key.

# File lib/redis/distributed.rb, line 227
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) click to toggle source

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

# File lib/redis/distributed.rb, line 234
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) click to toggle source

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

# File lib/redis/distributed.rb, line 153
def restore(key, ttl, serialized_value, **options)
  node_for(key).restore(key, ttl, serialized_value, **options)
end
rpop(key, count = nil) click to toggle source

Remove and get the last elements in a list.

# File lib/redis/distributed.rb, line 450
def rpop(key, count = nil)
  node_for(key).rpop(key, count)
end
rpoplpush(source, destination) click to toggle source

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

# File lib/redis/distributed.rb, line 456
def rpoplpush(source, destination)
  ensure_same_node(:rpoplpush, [source, destination]) do |node|
    node.rpoplpush(source, destination)
  end
end
rpush(key, value) click to toggle source

Append one or more values to a list.

# File lib/redis/distributed.rb, line 435
def rpush(key, value)
  node_for(key).rpush(key, value)
end
rpushx(key, value) click to toggle source

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

# File lib/redis/distributed.rb, line 440
def rpushx(key, value)
  node_for(key).rpushx(key, value)
end
sadd(key, member) click to toggle source

Add one or more members to a set.

# File lib/redis/distributed.rb, line 542
def sadd(key, member)
  node_for(key).sadd(key, member)
end
save() click to toggle source

Synchronously save the dataset to disk.

# File lib/redis/distributed.rb, line 103
def save
  on_each_node :save
end
scard(key) click to toggle source

Get the number of members in a set.

# File lib/redis/distributed.rb, line 537
def scard(key)
  node_for(key).scard(key)
end
script(subcommand, *args) click to toggle source

Control remote script registry.

# File lib/redis/distributed.rb, line 944
def script(subcommand, *args)
  on_each_node(:script, subcommand, *args)
end
sdiff(*keys) click to toggle source

Subtract multiple sets.

# File lib/redis/distributed.rb, line 594
def sdiff(*keys)
  ensure_same_node(:sdiff, keys) do |node|
    node.sdiff(*keys)
  end
end
sdiffstore(destination, *keys) click to toggle source

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

# File lib/redis/distributed.rb, line 601
def sdiffstore(destination, *keys)
  ensure_same_node(:sdiffstore, [destination] + keys) do |node|
    node.sdiffstore(destination, *keys)
  end
end
select(db) click to toggle source

Change the selected database for the current connection.

# File lib/redis/distributed.rb, line 48
def select(db)
  on_each_node :select, db
end
set(key, value, **options) click to toggle source

Set the string value of a key.

# File lib/redis/distributed.rb, line 280
def set(key, value, **options)
  node_for(key).set(key, value, **options)
end
setbit(key, offset, value) click to toggle source

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

# File lib/redis/distributed.rb, line 355
def setbit(key, offset, value)
  node_for(key).setbit(key, offset, value)
end
setex(key, ttl, value) click to toggle source

Set the time to live in seconds of a key.

# File lib/redis/distributed.rb, line 285
def setex(key, ttl, value)
  node_for(key).setex(key, ttl, value)
end
setnx(key, value) click to toggle source

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

# File lib/redis/distributed.rb, line 295
def setnx(key, value)
  node_for(key).setnx(key, value)
end
setrange(key, offset, value) click to toggle source

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

# File lib/redis/distributed.rb, line 345
def setrange(key, offset, value)
  node_for(key).setrange(key, offset, value)
end
sinter(*keys) click to toggle source

Intersect multiple sets.

# File lib/redis/distributed.rb, line 608
def sinter(*keys)
  ensure_same_node(:sinter, keys) do |node|
    node.sinter(*keys)
  end
end
sinterstore(destination, *keys) click to toggle source

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

# File lib/redis/distributed.rb, line 615
def sinterstore(destination, *keys)
  ensure_same_node(:sinterstore, [destination] + keys) do |node|
    node.sinterstore(destination, *keys)
  end
end
sismember(key, member) click to toggle source

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

# File lib/redis/distributed.rb, line 569
def sismember(key, member)
  node_for(key).sismember(key, member)
end
smembers(key) click to toggle source

Get all the members in a set.

# File lib/redis/distributed.rb, line 579
def smembers(key)
  node_for(key).smembers(key)
end
smismember(key, *members) click to toggle source

Determine if multiple values are members of a set.

# File lib/redis/distributed.rb, line 574
def smismember(key, *members)
  node_for(key).smismember(key, *members)
end
smove(source, destination, member) click to toggle source

Move a member from one set to another.

# File lib/redis/distributed.rb, line 562
def smove(source, destination, member)
  ensure_same_node(:smove, [source, destination]) do |node|
    node.smove(source, destination, member)
  end
end
sort(key, **options) click to toggle source

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

# File lib/redis/distributed.rb, line 241
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) click to toggle source

Remove and return a random member from a set.

# File lib/redis/distributed.rb, line 552
def spop(key, count = nil)
  node_for(key).spop(key, count)
end
srandmember(key, count = nil) click to toggle source

Get a random member from a set.

# File lib/redis/distributed.rb, line 557
def srandmember(key, count = nil)
  node_for(key).srandmember(key, count)
end
srem(key, member) click to toggle source

Remove one or more members from a set.

# File lib/redis/distributed.rb, line 547
def srem(key, member)
  node_for(key).srem(key, member)
end
sscan(key, cursor, **options) click to toggle source

Scan a set

# File lib/redis/distributed.rb, line 584
def sscan(key, cursor, **options)
  node_for(key).sscan(key, cursor, **options)
end
sscan_each(key, **options, &block) click to toggle source

Scan a set and return an enumerator

# File lib/redis/distributed.rb, line 589
def sscan_each(key, **options, &block)
  node_for(key).sscan_each(key, **options, &block)
end
strlen(key) click to toggle source

Get the length of the value stored in a key.

# File lib/redis/distributed.rb, line 392
def strlen(key)
  node_for(key).strlen(key)
end
subscribe(channel, *channels, &block) click to toggle source

Listen for messages published to the given channels.

# File lib/redis/distributed.rb, line 859
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?() click to toggle source
# File lib/redis/distributed.rb, line 854
def subscribed?
  !!@subscribed_node
end
sunion(*keys) click to toggle source

Add multiple sets.

# File lib/redis/distributed.rb, line 622
def sunion(*keys)
  ensure_same_node(:sunion, keys) do |node|
    node.sunion(*keys)
  end
end
sunionstore(destination, *keys) click to toggle source

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

# File lib/redis/distributed.rb, line 629
def sunionstore(destination, *keys)
  ensure_same_node(:sunionstore, [destination] + keys) do |node|
    node.sunionstore(destination, *keys)
  end
end
time() click to toggle source

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

# File lib/redis/distributed.rb, line 108
def time
  on_each_node :time
end
ttl(key) click to toggle source

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

# File lib/redis/distributed.rb, line 128
def ttl(key)
  node_for(key).ttl(key)
end
type(key) click to toggle source

Determine the type stored at key.

# File lib/redis/distributed.rb, line 250
def type(key)
  node_for(key).type(key)
end
unsubscribe(*channels) click to toggle source

Stop listening for messages posted to the given channels.

# File lib/redis/distributed.rb, line 872
def unsubscribe(*channels)
  raise "Can't unsubscribe if not subscribed." unless subscribed?

  @subscribed_node.unsubscribe(*channels)
end
unwatch() click to toggle source

Forget about all watched keys.

# File lib/redis/distributed.rb, line 904
def unwatch
  raise CannotDistribute, :unwatch unless @watch_key

  result = node_for(@watch_key).unwatch
  @watch_key = nil
  result
end
watch(*keys, &block) click to toggle source

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

# File lib/redis/distributed.rb, line 890
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) click to toggle source

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

# File lib/redis/distributed.rb, line 642
def zadd(key, *args)
  node_for(key).zadd(key, *args)
end
zcard(key) click to toggle source

Get the number of members in a sorted set.

# File lib/redis/distributed.rb, line 636
def zcard(key)
  node_for(key).zcard(key)
end
zcount(key, min, max) click to toggle source

Get the number of members in a particular score range.

# File lib/redis/distributed.rb, line 724
def zcount(key, min, max)
  node_for(key).zcount(key, min, max)
end
zdiff(*keys, **options) click to toggle source

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

# File lib/redis/distributed.rb, line 758
def zdiff(*keys, **options)
  ensure_same_node(:zdiff, keys) do |node|
    node.zdiff(*keys, **options)
  end
end
zdiffstore(destination, keys, **options) click to toggle source

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

# File lib/redis/distributed.rb, line 766
def zdiffstore(destination, keys, **options)
  ensure_same_node(:zdiffstore, [destination] + keys) do |node|
    node.zdiffstore(destination, keys, **options)
  end
end
zincrby(key, increment, member) click to toggle source

Increment the score of a member in a sorted set.

# File lib/redis/distributed.rb, line 648
def zincrby(key, increment, member)
  node_for(key).zincrby(key, increment, member)
end
zinter(*keys, **options) click to toggle source

Get the intersection of multiple sorted sets

# File lib/redis/distributed.rb, line 729
def zinter(*keys, **options)
  ensure_same_node(:zinter, keys) do |node|
    node.zinter(*keys, **options)
  end
end
zinterstore(destination, keys, **options) click to toggle source

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

# File lib/redis/distributed.rb, line 737
def zinterstore(destination, keys, **options)
  ensure_same_node(:zinterstore, [destination] + keys) do |node|
    node.zinterstore(destination, keys, **options)
  end
end
zmscore(key, *members) click to toggle source

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

# File lib/redis/distributed.rb, line 668
def zmscore(key, *members)
  node_for(key).zmscore(key, *members)
end
zrandmember(key, count = nil, **options) click to toggle source

Get one or more random members from a sorted set.

# File lib/redis/distributed.rb, line 663
def zrandmember(key, count = nil, **options)
  node_for(key).zrandmember(key, count, **options)
end
zrange(key, start, stop, **options) click to toggle source

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

# File lib/redis/distributed.rb, line 673
def zrange(key, start, stop, **options)
  node_for(key).zrange(key, start, stop, **options)
end
zrangebyscore(key, min, max, **options) click to toggle source

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

# File lib/redis/distributed.rb, line 708
def zrangebyscore(key, min, max, **options)
  node_for(key).zrangebyscore(key, min, max, **options)
end
zrangestore(dest_key, src_key, start, stop, **options) click to toggle source

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.

# File lib/redis/distributed.rb, line 679
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) click to toggle source

Determine the index of a member in a sorted set.

# File lib/redis/distributed.rb, line 692
def zrank(key, member)
  node_for(key).zrank(key, member)
end
zrem(key, member) click to toggle source

Remove one or more members from a sorted set.

# File lib/redis/distributed.rb, line 653
def zrem(key, member)
  node_for(key).zrem(key, member)
end
zremrangebyrank(key, start, stop) click to toggle source

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

# File lib/redis/distributed.rb, line 703
def zremrangebyrank(key, start, stop)
  node_for(key).zremrangebyrank(key, start, stop)
end
zremrangebyscore(key, min, max) click to toggle source

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

# File lib/redis/distributed.rb, line 719
def zremrangebyscore(key, min, max)
  node_for(key).zremrangebyscore(key, min, max)
end
zrevrange(key, start, stop, **options) click to toggle source

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

# File lib/redis/distributed.rb, line 687
def zrevrange(key, start, stop, **options)
  node_for(key).zrevrange(key, start, stop, **options)
end
zrevrangebyscore(key, max, min, **options) click to toggle source

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

# File lib/redis/distributed.rb, line 714
def zrevrangebyscore(key, max, min, **options)
  node_for(key).zrevrangebyscore(key, max, min, **options)
end
zrevrank(key, member) click to toggle source

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

# File lib/redis/distributed.rb, line 698
def zrevrank(key, member)
  node_for(key).zrevrank(key, member)
end
zscore(key, member) click to toggle source

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

# File lib/redis/distributed.rb, line 658
def zscore(key, member)
  node_for(key).zscore(key, member)
end
zunion(*keys, **options) click to toggle source

Return the union of multiple sorted sets.

# File lib/redis/distributed.rb, line 744
def zunion(*keys, **options)
  ensure_same_node(:zunion, keys) do |node|
    node.zunion(*keys, **options)
  end
end
zunionstore(destination, keys, **options) click to toggle source

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

# File lib/redis/distributed.rb, line 751
def zunionstore(destination, keys, **options)
  ensure_same_node(:zunionstore, [destination] + keys) do |node|
    node.zunionstore(destination, keys, **options)
  end
end

Protected Instance Methods

ensure_same_node(command, keys) { |node_for(first)| ... } click to toggle source
# File lib/redis/distributed.rb, line 1015
def ensure_same_node(command, keys)
  all = true

  tags = keys.map do |key|
    tag = key_tag(key)
    all = false unless tag
    tag
  end

  if (all && tags.uniq.size != 1) || (!all && keys.uniq.size != 1)
    # Not 1 unique tag or not 1 unique key
    raise CannotDistribute, command
  end

  yield(node_for(keys.first))
end
key_tag(key) click to toggle source
# File lib/redis/distributed.rb, line 1011
def key_tag(key)
  key.to_s[@tag, 1] if @tag
end
node_index_for(key) click to toggle source
# File lib/redis/distributed.rb, line 1007
def node_index_for(key)
  nodes.index(node_for(key))
end
on_each_node(command, *args) click to toggle source
# File lib/redis/distributed.rb, line 1001
def on_each_node(command, *args)
  nodes.map do |node|
    node.send(command, *args)
  end
end