Class: Redis::Distributed

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

Defined Under Namespace

Classes: CannotDistribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Distributed.



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

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

Instance Attribute Details

#ring ⇒ Object (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



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

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



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

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

#_bpop(cmd, args) ⇒ Object



683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# File 'lib/redis/distributed.rb', line 683

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

  args.flatten!(1)

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

#_eval(cmd, args) ⇒ Object



1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
# File 'lib/redis/distributed.rb', line 1791

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

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

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

#add_node(options) ⇒ Object



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

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

#append(key, value) ⇒ Object

Append a value to a key.



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

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

#arcount(key) ⇒ Object

Get the number of non-empty elements in an array.



1682
1683
1684
# File 'lib/redis/distributed.rb', line 1682

def arcount(key)
  node_for(key).arcount(key)
end

#ardel(key, *indices) ⇒ Object

Delete elements at the specified indices in an array.



1687
1688
1689
# File 'lib/redis/distributed.rb', line 1687

def ardel(key, *indices)
  node_for(key).ardel(key, *indices)
end

#ardelrange(key, *ranges) ⇒ Object

Delete elements in one or more inclusive index ranges.



1692
1693
1694
# File 'lib/redis/distributed.rb', line 1692

def ardelrange(key, *ranges)
  node_for(key).ardelrange(key, *ranges)
end

#arget(key, index) ⇒ Object

Get the value at an index in an array.



1657
1658
1659
# File 'lib/redis/distributed.rb', line 1657

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

#argetrange(key, start, stop) ⇒ Object

Get values in a range of indices.



1672
1673
1674
# File 'lib/redis/distributed.rb', line 1672

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

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

Search array elements within an index range using textual predicates.



1727
1728
1729
# File 'lib/redis/distributed.rb', line 1727

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

#arinfo(key, full: false) ⇒ Object

Get metadata about an array.



1737
1738
1739
# File 'lib/redis/distributed.rb', line 1737

def arinfo(key, full: false)
  node_for(key).arinfo(key, full: full)
end

#arinsert(key, *values) ⇒ Object

Insert one or more values at the array's insert cursor.



1697
1698
1699
# File 'lib/redis/distributed.rb', line 1697

def arinsert(key, *values)
  node_for(key).arinsert(key, *values)
end

#arlastitems(key, count, rev: false) ⇒ Object

Get the most recently inserted elements.



1712
1713
1714
# File 'lib/redis/distributed.rb', line 1712

def arlastitems(key, count, rev: false)
  node_for(key).arlastitems(key, count, rev: rev)
end

#arlen(key) ⇒ Object

Get the length of an array (max index + 1).



1677
1678
1679
# File 'lib/redis/distributed.rb', line 1677

def arlen(key)
  node_for(key).arlen(key)
end

#armget(key, *indices) ⇒ Object

Get values at multiple indices in an array.



1667
1668
1669
# File 'lib/redis/distributed.rb', line 1667

def armget(key, *indices)
  node_for(key).armget(key, *indices)
end

#armset(key, *pairs) ⇒ Object

Set multiple index-value pairs in an array.



1662
1663
1664
# File 'lib/redis/distributed.rb', line 1662

def armset(key, *pairs)
  node_for(key).armset(key, *pairs)
end

#arnext(key) ⇒ Object

Get the next index ARINSERT would use.



1707
1708
1709
# File 'lib/redis/distributed.rb', line 1707

def arnext(key)
  node_for(key).arnext(key)
end

#arop(key, start, stop, operation, value: nil) ⇒ Object

Perform an aggregate operation on the elements in a range.



1732
1733
1734
# File 'lib/redis/distributed.rb', line 1732

def arop(key, start, stop, operation, value: nil)
  node_for(key).arop(key, start, stop, operation, value: value)
end

#arring(key, size, *values) ⇒ Object

Insert one or more values into an array used as a fixed-size ring buffer.



1717
1718
1719
# File 'lib/redis/distributed.rb', line 1717

def arring(key, size, *values)
  node_for(key).arring(key, size, *values)
end

#arscan(key, start, stop, limit: nil) ⇒ Object

Iterate existing elements in an index range.



1722
1723
1724
# File 'lib/redis/distributed.rb', line 1722

def arscan(key, start, stop, limit: nil)
  node_for(key).arscan(key, start, stop, limit: limit)
end

#arseek(key, index) ⇒ Object

Set the insert cursor of an array to a specific index.



1702
1703
1704
# File 'lib/redis/distributed.rb', line 1702

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

#arset(key, index, *values) ⇒ Object

Set one or more contiguous values starting at an index in an array.



1652
1653
1654
# File 'lib/redis/distributed.rb', line 1652

def arset(key, index, *values)
  node_for(key).arset(key, index, *values)
end

#bgsave ⇒ Object

Asynchronously save the dataset to disk.



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

def bgsave
  on_each_node :bgsave
end

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

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



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

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

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

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



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

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

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

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



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

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

#bitrangeintarray(key, start, stop, scale: nil, limit: nil, zeros: false) ⇒ Object

Return the offsets of the set (or clear) bits in a range of the string value stored at key.



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

def bitrangeintarray(key, start, stop, scale: nil, limit: nil, zeros: false)
  node_for(key).bitrangeintarray(key, start, stop, scale: scale, limit: limit, zeros: zeros)
end

#bless_clear(key, flag) ⇒ Object

Remove a protection flag from a key.



1752
1753
1754
# File 'lib/redis/distributed.rb', line 1752

def bless_clear(key, flag)
  node_for(key).bless_clear(key, flag)
end

#bless_get(key) ⇒ Object

Return the active protection flags of a key.



1742
1743
1744
# File 'lib/redis/distributed.rb', line 1742

def bless_get(key)
  node_for(key).bless_get(key)
end

#bless_scan(_cursor, _flag, **_options) ⇒ Object

A BLESS SCAN cursor is only meaningful on the node that issued it, so a single cursor cannot drive an iteration across independent ring nodes. Use #bless_scan_each, which iterates every node in turn.

Raises:

  • (NotImplementedError)


1759
1760
1761
# File 'lib/redis/distributed.rb', line 1759

def bless_scan(_cursor, _flag, **_options)
  raise NotImplementedError
end

#bless_scan_each(flag, **options, &block) ⇒ Object

Iterate every key that carries the given protection flag, node by node in ring order.



1765
1766
1767
1768
1769
# File 'lib/redis/distributed.rb', line 1765

def bless_scan_each(flag, **options, &block)
  return to_enum(:bless_scan_each, flag, **options) unless block_given?

  nodes.each { |node| node.bless_scan_each(flag, **options, &block) }
end

#bless_set(key, flag) ⇒ Object

Add a protection flag to a key.



1747
1748
1749
# File 'lib/redis/distributed.rb', line 1747

def bless_set(key, flag)
  node_for(key).bless_set(key, flag)
end

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

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



628
629
630
631
632
# File 'lib/redis/distributed.rb', line 628

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

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

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



637
638
639
640
641
642
643
# File 'lib/redis/distributed.rb', line 637

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

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

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



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

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

#blpop(*args) ⇒ Object

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



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

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.



720
721
722
# File 'lib/redis/distributed.rb', line 720

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.



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

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

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

Atomically move the lowest- or highest-scoring members from one sorted set to another, or block until a member is available or the timeout expires.



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

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

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

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



1008
1009
1010
1011
1012
# File 'lib/redis/distributed.rb', line 1008

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

#bzpopmax(*args) ⇒ Object



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

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

#bzpopmin(*args) ⇒ Object



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

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

#close ⇒ Object



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

def close
  on_each_node :close
end

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

Copy a value from one key to another.



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

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

#dbsize ⇒ Object

Return the number of keys in the selected database.



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

def dbsize
  on_each_node :dbsize
end

#decr(key) ⇒ Object

Decrement the integer value of a key by one.



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

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

#decrby(key, decrement) ⇒ Object

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



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

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

#del(*args) ⇒ Object

Delete a key.



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

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

#delex(key, **options) ⇒ Object

Delete a key, optionally only if its current value or value digest matches a condition.



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

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

#digest(key) ⇒ Object

Compute a digest of the string value stored at a key.



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

def digest(key)
  node_for(key).digest(key)
end

#discard ⇒ Object

Discard all commands issued after MULTI.

Raises:



1537
1538
1539
1540
1541
1542
1543
1544
# File 'lib/redis/distributed.rb', line 1537

def discard
  key = transaction_key
  raise CannotDistribute, :discard unless key

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

#dump(key) ⇒ Object

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



200
201
202
# File 'lib/redis/distributed.rb', line 200

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

#dup ⇒ Object



1828
1829
1830
# File 'lib/redis/distributed.rb', line 1828

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

#echo(value) ⇒ Object

Echo the given string.



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

def echo(value)
  on_each_node :echo, value
end

#eval(*args) ⇒ Object

Evaluate Lua script.



1805
1806
1807
# File 'lib/redis/distributed.rb', line 1805

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

#evalsha(*args) ⇒ Object

Evaluate Lua script by its SHA.



1810
1811
1812
# File 'lib/redis/distributed.rb', line 1810

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

#exec ⇒ Object

Execute all commands issued after MULTI.

Raises:



1527
1528
1529
1530
1531
1532
1533
1534
# File 'lib/redis/distributed.rb', line 1527

def exec
  key = transaction_key
  raise CannotDistribute, :exec unless key

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

#exists(*args) ⇒ Object

Determine if a key exists.



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

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

#exists?(*args) ⇒ Boolean

Determine if any of the keys exists.

Returns:

  • (Boolean)


242
243
244
245
246
247
248
249
# File 'lib/redis/distributed.rb', line 242

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

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

Set a key's time to live in seconds.



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

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

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

Set the expiration for a key as a UNIX timestamp.



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

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

#expiretime(key) ⇒ Object

Get the expiration for a key as a UNIX timestamp.



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

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

#fcall(*args) ⇒ Object

Invoke a Redis Function.



1815
1816
1817
# File 'lib/redis/distributed.rb', line 1815

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

#fcall_ro(*args) ⇒ Object

Invoke a read-only Redis Function.



1820
1821
1822
# File 'lib/redis/distributed.rb', line 1820

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

#flushall ⇒ Object

Remove all keys from all databases.



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

def flushall
  on_each_node :flushall
end

#flushdb ⇒ Object

Remove all keys from the current database.



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

def flushdb
  on_each_node :flushdb
end

#function(subcommand, *args, **options) ⇒ Object

Manage Redis Functions libraries.

Function libraries are server-global rather than per-database, so the fan-out runs once per physical server: ring nodes differing only by database would re-run mutations (e.g. FUNCTION LOAD) against the same server and fail on collision.



1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
# File 'lib/redis/distributed.rb', line 1556

def function(subcommand, *args, **options)
  # Both RedisClient::Config and RedisClient::SentinelConfig expose the address;
  # for a sentinel-backed node it is the currently resolved master.
  function_nodes = nodes.uniq do |node|
    config = node._client.config
    [config.path, config.host, config.port]
  end

  if subcommand.to_s.downcase == "kill"
    # Only the node actually running a function replies OK; idle nodes raise NOTBUSY.
    # Succeed when any node killed a function, mirroring the cluster client's
    # one_succeeded fan-out semantics.
    not_busy = nil
    killed = function_nodes.filter_map do |node|
      node.function(:kill)
    rescue CommandError => e
      raise unless e.message.start_with?("NOTBUSY")

      not_busy = e
      nil
    end

    raise not_busy if killed.empty? && not_busy

    killed.first
  else
    function_nodes.map { |node| node.function(subcommand, *args, **options) }
  end
end

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

Add one or more geospatial items to a sorted set.



1377
1378
1379
# File 'lib/redis/distributed.rb', line 1377

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

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

Get the distance between two members of a geospatial index.



1392
1393
1394
# File 'lib/redis/distributed.rb', line 1392

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

#geohash(key, member) ⇒ Object

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



1382
1383
1384
# File 'lib/redis/distributed.rb', line 1382

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

#geopos(key, member) ⇒ Object

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



1387
1388
1389
# File 'lib/redis/distributed.rb', line 1387

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

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

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



1397
1398
1399
1400
# File 'lib/redis/distributed.rb', line 1397

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

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

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



1403
1404
1405
1406
# File 'lib/redis/distributed.rb', line 1403

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

#geosearch(key, **options) ⇒ Object

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



1409
1410
1411
# File 'lib/redis/distributed.rb', line 1409

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

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

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



1415
1416
1417
1418
1419
# File 'lib/redis/distributed.rb', line 1415

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

#get(key) ⇒ Object

Get the value of a key.



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

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.



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

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

#getdel(key) ⇒ Object

Get the value of a key and delete it.



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

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

#getex(key, **options) ⇒ Object

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



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

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

#getrange(key, start, stop) ⇒ Object

Get a substring of the string stored at a key.



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

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.



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

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

#hdel(key, *fields) ⇒ Object

Delete one or more hash fields.



1202
1203
1204
1205
# File 'lib/redis/distributed.rb', line 1202

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

#hexists(key, field) ⇒ Object

Determine if a hash field exists.



1225
1226
1227
# File 'lib/redis/distributed.rb', line 1225

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

#hexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>

Set the time to live in seconds for one or more hash fields.

Parameters:

  • key (String)
  • ttl (Integer)
  • fields (Array<String>)
  • options (Hash) —

    a customizable set of options

Returns:

  • (Array<Integer>) —

    per-field feedback, see Redis::Commands::Hashes#hexpire



1261
1262
1263
# File 'lib/redis/distributed.rb', line 1261

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

#hexpireat(key, unix_time_seconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>

Set the expiration for one or more hash fields as an absolute Unix timestamp in seconds.

Parameters:

  • key (String)
  • unix_time_seconds (Integer) —

    absolute expiration timestamp in seconds since epoch

  • fields (Array<String>)
  • options (Hash) —

    a customizable set of options

Returns:

  • (Array<Integer>) —

    per-field feedback, see Redis::Commands::Hashes#hexpireat



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

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

#hexpiretime(key, *fields) ⇒ Array<Integer>

Get the expiration of one or more hash fields as an absolute Unix timestamp in seconds.

Parameters:

  • key (String)
  • fields (Array<String>)

Returns:

  • (Array<Integer>) —

    per-field timestamps in seconds, see Redis::Commands::Hashes#hexpiretime



1322
1323
1324
# File 'lib/redis/distributed.rb', line 1322

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

#hget(key, field) ⇒ Object

Get the value of a hash field.



1182
1183
1184
# File 'lib/redis/distributed.rb', line 1182

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

#hgetall(key) ⇒ Object

Get all the fields and values in a hash.



1250
1251
1252
# File 'lib/redis/distributed.rb', line 1250

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

#hgetdel(key, *fields) ⇒ Object

Get the value of one or more hash fields and delete them.



1208
1209
1210
1211
# File 'lib/redis/distributed.rb', line 1208

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

#hgetex(key, *fields, ex: nil, px: nil, exat: nil, pxat: nil, persist: false) ⇒ Object

Get the value of one or more hash fields and optionally set their expiration.



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

def hgetex(key, *fields, ex: nil, px: nil, exat: nil, pxat: nil, persist: false)
  fields.flatten!(1)
  node_for(key).hgetex(key, *fields, ex: ex, px: px, exat: exat, pxat: pxat, persist: persist)
end

#himport_discard(fieldset_name) ⇒ Object

Remove a fieldset from every ring node.



1363
1364
1365
1366
1367
# File 'lib/redis/distributed.rb', line 1363

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

#himport_discard_all ⇒ Object

Remove all fieldsets from every ring node.



1370
1371
1372
1373
1374
# File 'lib/redis/distributed.rb', line 1370

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

#himport_prepare(fieldset_name, *fields) ⇒ Object

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



1348
1349
1350
1351
1352
1353
1354
# File 'lib/redis/distributed.rb', line 1348

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

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

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



1357
1358
1359
1360
# File 'lib/redis/distributed.rb', line 1357

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

#hincrby(key, field, increment) ⇒ Object

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



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

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.



1235
1236
1237
# File 'lib/redis/distributed.rb', line 1235

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

#hkeys(key) ⇒ Object

Get all the fields in a hash.



1240
1241
1242
# File 'lib/redis/distributed.rb', line 1240

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

#hlen(key) ⇒ Object

Get the number of fields in a hash.



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

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

#hmget(key, *fields) ⇒ Object

Get the values of all the given hash fields.



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

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

#hmset(key, *attrs) ⇒ Object

Set multiple hash fields to multiple values.



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

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

#hotkeys_get ⇒ Object

Raises:

  • (NotImplementedError)


126
127
128
# File 'lib/redis/distributed.rb', line 126

def hotkeys_get
  raise NotImplementedError, HOTKEYS_NOT_SUPPORTED
end

#hotkeys_reset ⇒ Object

Raises:

  • (NotImplementedError)


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

def hotkeys_reset
  raise NotImplementedError, HOTKEYS_NOT_SUPPORTED
end

#hotkeys_start ⇒ Object

Raises:

  • (NotImplementedError)


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

def hotkeys_start(**)
  raise NotImplementedError, HOTKEYS_NOT_SUPPORTED
end

#hotkeys_stop ⇒ Object

Raises:

  • (NotImplementedError)


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

def hotkeys_stop
  raise NotImplementedError, HOTKEYS_NOT_SUPPORTED
end

#hpersist(key, *fields) ⇒ Array<Integer>

Remove the expiration from one or more hash fields, making them persistent.

Parameters:

  • key (String)
  • fields (Array<String>)

Returns:

  • (Array<Integer>) —

    per-field feedback, see Redis::Commands::Hashes#hpersist



1341
1342
1343
# File 'lib/redis/distributed.rb', line 1341

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

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



1284
1285
1286
# File 'lib/redis/distributed.rb', line 1284

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

#hpexpireat(key, unix_time_milliseconds, *fields, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Array<Integer>

Set the expiration for one or more hash fields as an absolute Unix timestamp in milliseconds.

Parameters:

  • key (String)
  • unix_time_milliseconds (Integer) —

    absolute expiration timestamp in milliseconds since epoch

  • fields (Array<String>)
  • options (Hash) —

    a customizable set of options

Returns:

  • (Array<Integer>) —

    per-field feedback, see Redis::Commands::Hashes#hpexpireat



1312
1313
1314
# File 'lib/redis/distributed.rb', line 1312

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

#hpexpiretime(key, *fields) ⇒ Array<Integer>

Get the expiration of one or more hash fields as an absolute Unix timestamp in milliseconds.

Parameters:

  • key (String)
  • fields (Array<String>)

Returns:

  • (Array<Integer>) —

    per-field timestamps in milliseconds, see Redis::Commands::Hashes#hpexpiretime



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

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

#hpttl(key, *fields) ⇒ Object



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

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

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



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

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

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

Scan a hash



1270
1271
1272
# File 'lib/redis/distributed.rb', line 1270

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

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

Scan a hash and return an enumerator



1275
1276
1277
# File 'lib/redis/distributed.rb', line 1275

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

#hset(key, *attrs) ⇒ Object

Set multiple hash fields to multiple values.



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

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

#hsetex(key, *attrs, fnx: nil, fxx: nil, ex: nil, px: nil, exat: nil, pxat: nil, keepttl: false) ⇒ Object

Set the value of one or more hash fields, and optionally their expiration.



1220
1221
1222
# File 'lib/redis/distributed.rb', line 1220

def hsetex(key, *attrs, fnx: nil, fxx: nil, ex: nil, px: nil, exat: nil, pxat: nil, keepttl: false)
  node_for(key).hsetex(key, *attrs, fnx: fnx, fxx: fxx, ex: ex, px: px, exat: exat, pxat: pxat, keepttl: keepttl)
end

#hsetnx(key, field, value) ⇒ Object

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



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

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

#hstrlen(key, field) ⇒ Object

Get the length of the value stored in a hash field



1280
1281
1282
# File 'lib/redis/distributed.rb', line 1280

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

#httl(key, *fields) ⇒ Object



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

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

#hvals(key) ⇒ Object

Get all the values in a hash.



1245
1246
1247
# File 'lib/redis/distributed.rb', line 1245

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

#incr(key) ⇒ Object

Increment the integer value of a key by one.



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

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.



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

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.



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

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

#increx(key, **options) ⇒ Object

Increment the numeric value of a key atomically, with optional bounds and expiration control.



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

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

#info(*sections) ⇒ Object

Get information and statistics about the server.



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

def info(*sections)
  on_each_node :info, *sections
end

#inspect ⇒ Object



1824
1825
1826
# File 'lib/redis/distributed.rb', line 1824

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

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

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



449
450
451
# File 'lib/redis/distributed.rb', line 449

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

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

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



454
455
456
# File 'lib/redis/distributed.rb', line 454

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

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

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



459
460
461
# File 'lib/redis/distributed.rb', line 459

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

#json_arrlen(key, path = nil) ⇒ Object

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



464
465
466
# File 'lib/redis/distributed.rb', line 464

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

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

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



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

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

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

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



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

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

#json_clear(key, path = nil) ⇒ Object

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



439
440
441
# File 'lib/redis/distributed.rb', line 439

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

#json_debug_memory(key, path = nil) ⇒ Object

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



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

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

#json_del(key, path = nil) ⇒ Object

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



429
430
431
# File 'lib/redis/distributed.rb', line 429

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

#json_forget(key, path = nil) ⇒ Object

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



434
435
436
# File 'lib/redis/distributed.rb', line 434

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

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

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



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

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

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

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



444
445
446
# File 'lib/redis/distributed.rb', line 444

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

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

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



418
419
420
421
422
423
424
425
426
# File 'lib/redis/distributed.rb', line 418

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

#json_mset ⇒ Object

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

Raises:



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

def json_mset(*)
  raise CannotDistribute, :json_mset
end

#json_numincrby(key, path, number) ⇒ Object

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



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

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

#json_objkeys(key, path = nil) ⇒ Object

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



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

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

#json_objlen(key, path = nil) ⇒ Object

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



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

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

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

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



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

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

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

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



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

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

#json_strlen(key, path = nil) ⇒ Object

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



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

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

#json_toggle(key, path) ⇒ Object

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



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

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

#json_type(key, path = nil) ⇒ Object

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



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

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

#keys(glob = "*") ⇒ Object

Find all keys matching the given pattern.



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

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

#lastsave ⇒ Object

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



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

def lastsave
  on_each_node :lastsave
end

#lcs(key1, key2, **options) ⇒ Object

Find the longest common subsequence of the strings stored at two keys.



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

def lcs(key1, key2, **options)
  ensure_same_node(:lcs, [key1, key2]) do |node|
    node.lcs(key1, key2, **options)
  end
end

#lindex(key, index) ⇒ Object

Get an element from a list by its index.



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

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.



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

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

#llen(key) ⇒ Object

Get the length of a list.



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

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

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

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



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

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

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

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



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

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

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

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



770
771
772
773
774
# File 'lib/redis/distributed.rb', line 770

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

#lpop(key, count = nil) ⇒ Object

Remove and get the first elements in a list.



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

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

#lpush(key, value) ⇒ Object

Prepend one or more values to a list.



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

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.



651
652
653
# File 'lib/redis/distributed.rb', line 651

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

#lrange(key, start, stop) ⇒ Object

Get a range of elements from a list.



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

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

#lrem(key, count, value) ⇒ Object

Remove elements from a list.



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

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.



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

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.



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

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

#mapped_hmget(key, *fields) ⇒ Object



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

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

#mapped_hmset(key, hash) ⇒ Object



1177
1178
1179
# File 'lib/redis/distributed.rb', line 1177

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

#mapped_mget(*keys) ⇒ Object

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



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

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

#mapped_mset(_hash) ⇒ Object

Raises:



362
363
364
# File 'lib/redis/distributed.rb', line 362

def mapped_mset(_hash)
  raise CannotDistribute, :mapped_mset
end

#mapped_msetnx(_hash) ⇒ Object

Raises:



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

def mapped_msetnx(_hash)
  raise CannotDistribute, :mapped_msetnx
end

#mget(*keys) ⇒ Object

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



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

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

#migrate(_key, _options) ⇒ Object

Transfer a key from the connected instance to another instance.

Raises:



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

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

#monitor ⇒ Object

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

Raises:

  • (NotImplementedError)


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

def monitor
  raise NotImplementedError
end

#move(key, db) ⇒ Object

Move a key to another database.



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

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

#mset ⇒ Object

Set multiple keys to multiple values.

Raises:



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

def mset(*)
  raise CannotDistribute, :mset
end

#msetnx ⇒ Object

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

Raises:



367
368
369
# File 'lib/redis/distributed.rb', line 367

def msetnx(*)
  raise CannotDistribute, :msetnx
end

#multi(&block) ⇒ Object

Mark the start of a transaction block.

Raises:



1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
# File 'lib/redis/distributed.rb', line 1499

def multi(&block)
  key = transaction_key
  raise CannotDistribute, :multi unless key

  node = node_for(key)
  begin
    result = node.multi(&block)
  rescue StandardError
    # If the block raised, no MULTI/EXEC was sent and the server still watches the keys. The
    # error may come from another connection, so the UNWATCH is always attempted.
    begin
      unwatch_after_error(key)
    ensure
      release_watch_key(key)
    end
    raise
  end

  begin
    # A block that queues no command sends no MULTI/EXEC, so the server still watches the keys.
    node.unwatch if result == []
  ensure
    release_watch_key(key)
  end
  result
end

#node_for(key) ⇒ Object

Raises:



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

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

  @ring.get_node(key)
end

#nodes ⇒ Object



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

def nodes
  @ring.nodes
end

#persist(key) ⇒ Object

Remove the expiration from a key.



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

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

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

Set a key's time to live in milliseconds.



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

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

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

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



185
186
187
# File 'lib/redis/distributed.rb', line 185

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

#pexpiretime(key) ⇒ Object

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



190
191
192
# File 'lib/redis/distributed.rb', line 190

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

#pfadd(key, member) ⇒ Object

Add one or more members to a HyperLogLog structure.



1772
1773
1774
# File 'lib/redis/distributed.rb', line 1772

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

#pfcount(*keys) ⇒ Object

Get the approximate cardinality of members added to HyperLogLog structure.



1777
1778
1779
1780
1781
# File 'lib/redis/distributed.rb', line 1777

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.



1785
1786
1787
1788
1789
# File 'lib/redis/distributed.rb', line 1785

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

Ping the server.



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

def ping
  on_each_node :ping
end

#pipelined ⇒ Object

Raises:



1494
1495
1496
# File 'lib/redis/distributed.rb', line 1494

def pipelined
  raise CannotDistribute, :pipelined
end

#psetex(key, ttl, value) ⇒ Object

Set the time to live in milliseconds of a key.



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

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

#psubscribe(*channels, &block) ⇒ Object

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

Raises:

  • (NotImplementedError)


1453
1454
1455
# File 'lib/redis/distributed.rb', line 1453

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

#pttl(key) ⇒ Object

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



195
196
197
# File 'lib/redis/distributed.rb', line 195

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

#publish(channel, message) ⇒ Object

Post a message to a channel.



1422
1423
1424
# File 'lib/redis/distributed.rb', line 1422

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

#punsubscribe(*channels) ⇒ Object

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

Raises:

  • (NotImplementedError)


1461
1462
1463
# File 'lib/redis/distributed.rb', line 1461

def punsubscribe(*channels)
  raise NotImplementedError
end

#quit ⇒ Object

Close the connection.



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

def quit
  on_each_node :quit
end

#randomkey ⇒ Object

Return a random key from the keyspace.

Raises:



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

def randomkey
  raise CannotDistribute, :randomkey
end

#rename(old_name, new_name) ⇒ Object

Rename a key.



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

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.



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

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.



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

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

#rpop(key, count = nil) ⇒ Object

Remove and get the last elements in a list.



671
672
673
# File 'lib/redis/distributed.rb', line 671

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

#rpoplpush(source, destination) ⇒ Object

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



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

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.



656
657
658
# File 'lib/redis/distributed.rb', line 656

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.



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

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

#sadd(key, *members) ⇒ Object

Add one or more members to a set.



782
783
784
# File 'lib/redis/distributed.rb', line 782

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

#sadd?(key, *members) ⇒ Boolean

Add one or more members to a set.

Returns:

  • (Boolean)


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

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

#saddex(key, *members, mnx: nil, mxx: nil, ex: nil, px: nil, exat: nil, pxat: nil, keepttl: false) ⇒ Object

Add one or more members to a set and optionally set their expiration.



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

def saddex(key, *members, mnx: nil, mxx: nil, ex: nil, px: nil, exat: nil, pxat: nil, keepttl: false)
  node_for(key).saddex(key, *members, mnx: mnx, mxx: mxx, ex: ex, px: px, exat: exat, pxat: pxat, keepttl: keepttl)
end

#save ⇒ Object

Synchronously save the dataset to disk.



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

def save
  on_each_node :save
end

#scard(key) ⇒ Object

Get the number of members in a set.



777
778
779
# File 'lib/redis/distributed.rb', line 777

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

#script(subcommand, *args) ⇒ Object

Control remote script registry.



1547
1548
1549
# File 'lib/redis/distributed.rb', line 1547

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

#sdiff(*keys) ⇒ Object

Subtract multiple sets.



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

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

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

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



915
916
917
918
919
920
# File 'lib/redis/distributed.rb', line 915

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

#sdiffstore(destination, *keys) ⇒ Object

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



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

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

#select(db) ⇒ Object

Change the selected database for the current connection.



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

def select(db)
  on_each_node :select, db
end

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

Set the string value of a key.



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

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.



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

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.



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

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.



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

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.



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

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

#sexpire(key, ttl, *members, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Object

Set the time to live in seconds for one or more set members.



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

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

#sexpireat(key, unix_time_seconds, *members, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Object

Set the expiration for one or more set members as an absolute Unix timestamp in seconds.



845
846
847
# File 'lib/redis/distributed.rb', line 845

def sexpireat(key, unix_time_seconds, *members, nx: nil, xx: nil, gt: nil, lt: nil)
  node_for(key).sexpireat(key, unix_time_seconds, *members, nx: nx, xx: xx, gt: gt, lt: lt)
end

#sexpiretime(key, *members) ⇒ Object

Get the expiration of one or more set members as an absolute Unix timestamp in seconds.



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

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

#sinter(*keys) ⇒ Object

Intersect multiple sets.



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

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

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

Get the number of members in the intersection of multiple sets.



931
932
933
934
935
936
# File 'lib/redis/distributed.rb', line 931

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

#sinterstore(destination, *keys) ⇒ Object

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



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

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

#sismember(key, member) ⇒ Object

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



824
825
826
# File 'lib/redis/distributed.rb', line 824

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

#smembers(key) ⇒ Object

Get all the members in a set.



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

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

#smismember(key, *members) ⇒ Object

Determine if multiple values are members of a set.



829
830
831
# File 'lib/redis/distributed.rb', line 829

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

#smove(source, destination, member) ⇒ Object

Move a member from one set to another.



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

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.



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

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

#sort_ro(key, **options) ⇒ Object

Sort the elements in a list, set or sorted set without storing the result.



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

def sort_ro(key, **options)
  ensure_same_node(:sort_ro, sort_keys(key, options)) do |node|
    node.sort_ro(key, **options)
  end
end

#spersist(key, *members) ⇒ Object

Remove the expiration from one or more set members.



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

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

#spexpire(key, ttl, *members, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Object

Set the time to live in milliseconds for one or more set members.



839
840
841
# File 'lib/redis/distributed.rb', line 839

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

#spexpireat(key, unix_time_milliseconds, *members, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Object

Set the expiration for one or more set members as an absolute Unix timestamp in milliseconds.



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

def spexpireat(key, unix_time_milliseconds, *members, nx: nil, xx: nil, gt: nil, lt: nil)
  node_for(key).spexpireat(key, unix_time_milliseconds, *members, nx: nx, xx: xx, gt: gt, lt: lt)
end

#spexpiretime(key, *members) ⇒ Object

Get the expiration of one or more set members as an absolute Unix timestamp in milliseconds.



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

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

#spop(key, count = nil) ⇒ Object

Remove and return a random member from a set.



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

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

#spttl(key, *members) ⇒ Object

Get the time to live in milliseconds for one or more set members.



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

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

#srandmember(key, count = nil) ⇒ Object

Get a random member from a set.



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

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

#srem(key, *members) ⇒ Object

Remove one or more members from a set.



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

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

#srem?(key, *members) ⇒ Boolean

Remove one or more members from a set.

Returns:

  • (Boolean)


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

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

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

Scan a set



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

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



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

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.



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

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

#sttl(key, *members) ⇒ Object

Get the time to live in seconds for one or more set members.



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

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

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

Listen for messages published to the given channels.



1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
# File 'lib/redis/distributed.rb', line 1431

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)


1426
1427
1428
# File 'lib/redis/distributed.rb', line 1426

def subscribed?
  !!@subscribed_node
end

#sunion(*keys) ⇒ Object

Add multiple sets.



947
948
949
950
951
952
# File 'lib/redis/distributed.rb', line 947

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

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

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



963
964
965
966
967
968
# File 'lib/redis/distributed.rb', line 963

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

#sunionstore(destination, *keys) ⇒ Object

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



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

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

#time ⇒ Object

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



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

def time
  on_each_node :time
end

#ttl(key) ⇒ Object

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



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

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

#type(key) ⇒ Object

Determine the type stored at key.



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

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

Unlink keys.



224
225
226
227
228
229
230
# File 'lib/redis/distributed.rb', line 224

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

#unsubscribe(*channels) ⇒ Object

Stop listening for messages posted to the given channels.

Raises:



1444
1445
1446
1447
1448
# File 'lib/redis/distributed.rb', line 1444

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

  @subscribed_node.unsubscribe(*channels)
end

#unwatch ⇒ Object

Forget about all watched keys.

Raises:



1485
1486
1487
1488
1489
1490
1491
1492
# File 'lib/redis/distributed.rb', line 1485

def unwatch
  key = transaction_key
  raise CannotDistribute, :unwatch unless key

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

#vadd(key, vector, element, **options) ⇒ Object

Add a new element into a vector set, or update its vector if it already exists.



1587
1588
1589
# File 'lib/redis/distributed.rb', line 1587

def vadd(key, vector, element, **options)
  node_for(key).vadd(key, vector, element, **options)
end

#vcard(key) ⇒ Object

Return the number of elements in a vector set.



1592
1593
1594
# File 'lib/redis/distributed.rb', line 1592

def vcard(key)
  node_for(key).vcard(key)
end

#vdim(key) ⇒ Object

Return the number of dimensions of the vectors in a vector set.



1597
1598
1599
# File 'lib/redis/distributed.rb', line 1597

def vdim(key)
  node_for(key).vdim(key)
end

#vemb(key, element, **options) ⇒ Object

Return the approximate vector associated with an element in a vector set.



1602
1603
1604
# File 'lib/redis/distributed.rb', line 1602

def vemb(key, element, **options)
  node_for(key).vemb(key, element, **options)
end

#vgetattr(key, element, **options) ⇒ Object

Return the JSON attributes associated with an element in a vector set.



1612
1613
1614
# File 'lib/redis/distributed.rb', line 1612

def vgetattr(key, element, **options)
  node_for(key).vgetattr(key, element, **options)
end

#vinfo(key) ⇒ Object

Return metadata and internal details about a vector set.



1637
1638
1639
# File 'lib/redis/distributed.rb', line 1637

def vinfo(key)
  node_for(key).vinfo(key)
end

#vismember(key, element) ⇒ Object

Check if an element exists in a vector set.



1627
1628
1629
# File 'lib/redis/distributed.rb', line 1627

def vismember(key, element)
  node_for(key).vismember(key, element)
end

Return the neighbors of an element at each layer in the HNSW graph.



1632
1633
1634
# File 'lib/redis/distributed.rb', line 1632

def vlinks(key, element, **options)
  node_for(key).vlinks(key, element, **options)
end

#vrandmember(key, count = nil) ⇒ Object

Return one or more random elements from a vector set.



1642
1643
1644
# File 'lib/redis/distributed.rb', line 1642

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

#vrange(key, start, stop, count = nil) ⇒ Object

Return elements of a vector set in a lexicographical range.



1647
1648
1649
# File 'lib/redis/distributed.rb', line 1647

def vrange(key, start, stop, count = nil)
  node_for(key).vrange(key, start, stop, count)
end

#vrem(key, element) ⇒ Object

Remove an element from a vector set.



1622
1623
1624
# File 'lib/redis/distributed.rb', line 1622

def vrem(key, element)
  node_for(key).vrem(key, element)
end

#vsetattr(key, element, attributes) ⇒ Object

Associate, update or delete the JSON attributes of an element in a vector set.



1607
1608
1609
# File 'lib/redis/distributed.rb', line 1607

def vsetattr(key, element, attributes)
  node_for(key).vsetattr(key, element, attributes)
end

#vsim(key, **options) ⇒ Object

Return elements of a vector set by vector similarity.



1617
1618
1619
# File 'lib/redis/distributed.rb', line 1617

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

#waitaof(numlocal, numreplicas, timeout) ⇒ Object

Block until each node has fsynced this ring's preceding writes to its AOF and/or its replicas'. Returns one [local, replicas] pair per node.



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

def waitaof(numlocal, numreplicas, timeout)
  on_each_node :waitaof, numlocal, numreplicas, timeout
end

#watch(*keys, &block) ⇒ Object

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



1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
# File 'lib/redis/distributed.rb', line 1466

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

    begin
      node.watch(*keys, &block)
    rescue StandardError => error
      # The block can end the first watch with #multi and then watch a key on another node.
      # Redis#watch skips UNWATCH on a connection error, which may come from another client.
      current = @watch_key
      @watch_key = @released_watch_key = nil
      unwatch_after_error(current) if current && (current != watch_key || error.is_a?(ConnectionError))
      raise
    end
  end
end

#zadd(key, *args) ⇒ Object

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



977
978
979
# File 'lib/redis/distributed.rb', line 977

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

#zcard(key) ⇒ Object

Get the number of members in a sorted set.



971
972
973
# File 'lib/redis/distributed.rb', line 971

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

#zcount(key, min, max) ⇒ Object

Get the number of members in a particular score range.



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

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

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

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



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

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

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

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



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

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

#zincrby(key, increment, member) ⇒ Object

Increment the score of a member in a sorted set.



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

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

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

Get the intersection of multiple sorted sets



1100
1101
1102
1103
1104
1105
# File 'lib/redis/distributed.rb', line 1100

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

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

Get the number of members in the intersection of multiple sorted sets.



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

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

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

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



1117
1118
1119
1120
1121
1122
# File 'lib/redis/distributed.rb', line 1117

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

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

Atomically move members from one sorted set to another, preserving their scores.



1022
1023
1024
1025
1026
# File 'lib/redis/distributed.rb', line 1022

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

#zmovemember(source, destination, members, **options) ⇒ Object

Atomically move the given members from one sorted set to another, preserving their scores.



1037
1038
1039
1040
1041
# File 'lib/redis/distributed.rb', line 1037

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

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

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



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

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

#zmscore(key, *members) ⇒ Object

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



1003
1004
1005
# File 'lib/redis/distributed.rb', line 1003

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

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

Get one or more random members from a sorted set.



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

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

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

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



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

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.



1079
1080
1081
# File 'lib/redis/distributed.rb', line 1079

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

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

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



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

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

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

Determine the index of a member in a sorted set.



1063
1064
1065
# File 'lib/redis/distributed.rb', line 1063

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

#zrem(key, member) ⇒ Object

Remove one or more members from a sorted set.



988
989
990
# File 'lib/redis/distributed.rb', line 988

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.



1074
1075
1076
# File 'lib/redis/distributed.rb', line 1074

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.



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

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.



1058
1059
1060
# File 'lib/redis/distributed.rb', line 1058

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.



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

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

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

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



1069
1070
1071
# File 'lib/redis/distributed.rb', line 1069

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

#zscore(key, member) ⇒ Object

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



993
994
995
# File 'lib/redis/distributed.rb', line 993

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

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

Return the union of multiple sorted sets.



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

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

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

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



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

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