Class: RedisCluster::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_cluster/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Node

basic requires:

{host: xxx.xxx.xx.xx, port: xxx}

redis cluster don’t support select db, use default 0



12
13
14
15
# File 'lib/redis_cluster/node.rb', line 12

def initialize(opts)
  @options = opts
  @slots = []
end

Instance Attribute Details

#slotsObject

slots is a range array: [1..100, 300..500]



5
6
7
# File 'lib/redis_cluster/node.rb', line 5

def slots
  @slots
end

Class Method Details

.redis(options) ⇒ Object



41
42
43
# File 'lib/redis_cluster/node.rb', line 41

def self.redis(options)
  ::Redis.new(options)
end

Instance Method Details

#askingObject



29
30
31
# File 'lib/redis_cluster/node.rb', line 29

def asking
  execute(:asking)
end

#connectionObject



37
38
39
# File 'lib/redis_cluster/node.rb', line 37

def connection
  @connection ||= self.class.redis(@options)
end

#execute(method, args) ⇒ Object



33
34
35
# File 'lib/redis_cluster/node.rb', line 33

def execute(method, args)
  connection.send(method, *args)
end

#has_slot?(slot) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_slot?(slot)
  slots.any? {|range| range.include? slot }
end

#host_hashObject



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

def host_hash
  {host: @options[:host], port: @options[:port]}
end

#nameObject



17
18
19
# File 'lib/redis_cluster/node.rb', line 17

def name
  "#{@options[:host]}:#{@options[:port]}"
end