Class: Redis::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/cluster.rb,
lib/redis/errors.rb,
lib/redis/cluster/node.rb,
lib/redis/cluster/slot.rb,
lib/redis/cluster/option.rb,
lib/redis/cluster/command.rb,
lib/redis/cluster/node_key.rb,
lib/redis/cluster/node_loader.rb,
lib/redis/cluster/slot_loader.rb,
lib/redis/cluster/command_loader.rb,
lib/redis/cluster/key_slot_converter.rb

Overview

Redis Cluster client

Copyright © 2013 Salvatore Sanfilippo <[email protected]>

Defined Under Namespace

Modules: CommandLoader, KeySlotConverter, NodeKey, NodeLoader, SlotLoader Classes: AmbiguousNodeError, Command, CommandErrorCollection, CrossSlotPipeliningError, Node, Option, OrchestrationCommandNotSupported, Slot

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Cluster

Returns a new instance of Cluster.



24
25
26
27
28
# File 'lib/redis/cluster.rb', line 24

def initialize(options = {})
  @option = Option.new(options)
  @node, @slot = fetch_cluster_info!(@option)
  @command = fetch_command_details(@node)
end

Instance Method Details

#call(command, &block) ⇒ Object



71
72
73
# File 'lib/redis/cluster.rb', line 71

def call(command, &block)
  send_command(command, &block)
end

#call_loop(command, timeout = 0, &block) ⇒ Object



75
76
77
78
# File 'lib/redis/cluster.rb', line 75

def call_loop(command, timeout = 0, &block)
  node = assign_node(command)
  try_send(node, :call_loop, command, timeout, &block)
end

#call_pipeline(pipeline) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/redis/cluster.rb', line 80

def call_pipeline(pipeline)
  node_keys = pipeline.commands.map { |cmd| find_node_key(cmd, primary_only: true) }.compact.uniq
  if node_keys.size > 1
    raise(CrossSlotPipeliningError,
          pipeline.commands.map { |cmd| @command.extract_first_key(cmd) }.reject(&:empty?).uniq)
  end

  try_send(find_node(node_keys.first), :call_pipeline, pipeline)
end

#call_with_timeout(command, timeout, &block) ⇒ Object



90
91
92
93
# File 'lib/redis/cluster.rb', line 90

def call_with_timeout(command, timeout, &block)
  node = assign_node(command)
  try_send(node, :call_with_timeout, command, timeout, &block)
end

#call_without_timeout(command, &block) ⇒ Object



95
96
97
# File 'lib/redis/cluster.rb', line 95

def call_without_timeout(command, &block)
  call_with_timeout(command, 0, &block)
end

#connected?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/redis/cluster.rb', line 46

def connected?
  @node.any?(&:connected?)
end

#connection_infoObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/redis/cluster.rb', line 55

def connection_info
  @node.sort_by(&:id).map do |client|
    {
      host: client.host,
      port: client.port,
      db: client.db,
      id: client.id,
      location: client.location
    }
  end
end

#dbObject

db feature is disabled in cluster mode



35
36
37
# File 'lib/redis/cluster.rb', line 35

def db
  0
end

#db=(_db) ⇒ Object

db feature is disabled in cluster mode



40
# File 'lib/redis/cluster.rb', line 40

def db=(_db); end

#disconnectObject



50
51
52
53
# File 'lib/redis/cluster.rb', line 50

def disconnect
  @node.each(&:disconnect)
  true
end

#idObject



30
31
32
# File 'lib/redis/cluster.rb', line 30

def id
  @node.map(&:id).sort.join(' ')
end

#process(commands, &block) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/redis/cluster.rb', line 99

def process(commands, &block)
  if commands.size == 1 &&
     %w[unsubscribe punsubscribe].include?(commands.first.first.to_s.downcase) &&
     commands.first.size == 1

    # Node is indeterminate. We do just a best-effort try here.
    @node.process_all(commands, &block)
  else
    node = assign_node(commands.first)
    try_send(node, :process, commands, &block)
  end
end

#timeoutObject



42
43
44
# File 'lib/redis/cluster.rb', line 42

def timeout
  @node.first.timeout
end

#with_reconnect(val = true, &block) ⇒ Object



67
68
69
# File 'lib/redis/cluster.rb', line 67

def with_reconnect(val = true, &block)
  try_send(@node.sample, :with_reconnect, val, &block)
end