Class: Redis::Cluster::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/redis/cluster/node.rb

Overview

Keep client list of node for Redis Cluster Client

Constant Summary collapse

ReloadNeeded =
Class.new(StandardError)
ROLE_SLAVE =
'slave'

Instance Method Summary collapse

Constructor Details

#initialize(options, node_flags = {}, with_replica = false) ⇒ Node

Returns a new instance of Node.



15
16
17
18
19
# File 'lib/redis/cluster/node.rb', line 15

def initialize(options, node_flags = {}, with_replica = false)
  @with_replica = with_replica
  @node_flags = node_flags
  @clients = build_clients(options)
end

Instance Method Details

#call_all(command, &block) ⇒ Object



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

def call_all(command, &block)
  try_map { |_, client| client.call(command, &block) }.values
end

#call_master(command, &block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/redis/cluster/node.rb', line 39

def call_master(command, &block)
  try_map do |node_key, client|
    next if slave?(node_key)

    client.call(command, &block)
  end.values
end

#call_slave(command, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/redis/cluster/node.rb', line 47

def call_slave(command, &block)
  return call_master(command, &block) if replica_disabled?

  try_map do |node_key, client|
    next if master?(node_key)

    client.call(command, &block)
  end.values
end

#each(&block) ⇒ Object



21
22
23
# File 'lib/redis/cluster/node.rb', line 21

def each(&block)
  @clients.values.each(&block)
end

#find_by(node_key) ⇒ Object



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

def find_by(node_key)
  @clients.fetch(node_key)
rescue KeyError
  raise ReloadNeeded
end

#process_all(commands, &block) ⇒ Object



57
58
59
# File 'lib/redis/cluster/node.rb', line 57

def process_all(commands, &block)
  try_map { |_, client| client.process(commands, &block) }.values
end

#sampleObject



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

def sample
  @clients.values.sample
end