Class: Redis::Cluster::Slot

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

Overview

Keep slot and node key map for Redis Cluster Client

Constant Summary collapse

ROLE_SLAVE =
'slave'

Instance Method Summary collapse

Constructor Details

#initialize(available_slots, node_flags = {}, with_replica = false) ⇒ Slot

Returns a new instance of Slot.



9
10
11
12
13
# File 'lib/redis/cluster/slot.rb', line 9

def initialize(available_slots, node_flags = {}, with_replica = false)
  @with_replica = with_replica
  @node_flags = node_flags
  @map = build_slot_node_key_map(available_slots)
end

Instance Method Details

#exists?(slot) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/redis/cluster/slot.rb', line 15

def exists?(slot)
  @map.key?(slot)
end

#find_node_key_of_master(slot) ⇒ Object



19
20
21
22
23
# File 'lib/redis/cluster/slot.rb', line 19

def find_node_key_of_master(slot)
  return nil unless exists?(slot)

  @map[slot][:master]
end

#find_node_key_of_slave(slot) ⇒ Object



25
26
27
28
29
30
# File 'lib/redis/cluster/slot.rb', line 25

def find_node_key_of_slave(slot)
  return nil unless exists?(slot)
  return find_node_key_of_master(slot) if replica_disabled?

  @map[slot][:slaves].sample
end

#put(slot, node_key) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/redis/cluster/slot.rb', line 32

def put(slot, node_key)
  # Since we're sharing a hash for build_slot_node_key_map, duplicate it
  # if it already exists instead of preserving as-is.
  @map[slot] = @map[slot] ? @map[slot].dup : { master: nil, slaves: [] }

  if master?(node_key)
    @map[slot][:master] = node_key
  elsif !@map[slot][:slaves].include?(node_key)
    @map[slot][:slaves] << node_key
  end

  nil
end