Class: RedisClient::Cluster::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_client/cluster/command.rb

Defined Under Namespace

Classes: Detail

Constant Summary collapse

EMPTY_STRING =
''
LEFT_BRACKET =
'{'
RIGHT_BRACKET =
'}'
EMPTY_HASH =
{}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commands) ⇒ Command

Returns a new instance of Command.



57
58
59
# File 'lib/redis_client/cluster/command.rb', line 57

def initialize(commands)
  @commands = commands || EMPTY_HASH
end

Class Method Details

.load(nodes) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/redis_client/cluster/command.rb', line 24

def load(nodes)
  cmd = errors = nil

  nodes&.each do |node|
    reply = node.call('COMMAND')
    commands = parse_command_reply(reply)
    cmd = ::RedisClient::Cluster::Command.new(commands)
    break
  rescue ::RedisClient::Error => e
    errors ||= []
    errors << e
  end

  return cmd unless cmd.nil?

  raise ::RedisClient::Cluster::InitialSetupError, errors
end

Instance Method Details

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/redis_client/cluster/command.rb', line 80

def exists?(name)
  @commands.key?(::RedisClient::Cluster::NormalizedCmdName.instance.get_by_name(name))
end

#extract_first_key(command) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/redis_client/cluster/command.rb', line 61

def extract_first_key(command)
  i = determine_first_key_position(command)
  return EMPTY_STRING if i == 0

  key = (command[i].is_a?(Array) ? command[i].flatten.first : command[i]).to_s
  hash_tag = extract_hash_tag(key)
  hash_tag.empty? ? key : hash_tag
end

#should_send_to_primary?(command) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/redis_client/cluster/command.rb', line 70

def should_send_to_primary?(command)
  name = ::RedisClient::Cluster::NormalizedCmdName.instance.get_by_command(command)
  @commands[name]&.write?
end

#should_send_to_replica?(command) ⇒ Boolean

Returns:

  • (Boolean)


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

def should_send_to_replica?(command)
  name = ::RedisClient::Cluster::NormalizedCmdName.instance.get_by_command(command)
  @commands[name]&.readonly?
end