Class: Renoir::Client
- Inherits:
-
Object
- Object
- Renoir::Client
- Defined in:
- lib/renoir/client.rb
Constant Summary collapse
- REDIS_CLUSTER_HASH_SLOTS =
16_384
- DEFAULT_OPTIONS =
{ cluster_nodes: [ ["127.0.0.1", 6379] ], max_redirection: 10, max_connection_error: 5, connect_retry_random_factor: 0.1, connect_retry_interval: 0.001, # 1 ms connection_adapter: :redis, }.freeze
Instance Method Summary collapse
-
#call(*command) {|Object| ... } ⇒ Object
Call a Redis command.
-
#close ⇒ Object
Close all holding connections.
-
#each_node {|Object| ... } ⇒ Enumerable
Enumerate connections of cluster nodes.
-
#eval(*args) {|Object| ... } ⇒ Object
Call EVAL command.
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
-
#method_missing(command, *args, &block) ⇒ Object
Delegated to #call.
-
#multi {|Renoir::Pipeline| ... } ⇒ Object
Pipeline commands and call them with MULTI/EXEC.
-
#pipelined {|Renoir::Pipeline| ... } ⇒ Object
Pipeline commands and call them.
Constructor Details
#initialize(options) ⇒ Client
Returns a new instance of Client.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/renoir/client.rb', line 36 def initialize() @connections = {} @cluster_info = ClusterInfo.new @refresh_slots = true = .map { |k, v| [k.to_sym, v] }.to_h @options = DEFAULT_OPTIONS.merge() @logger = @options[:logger] @adapter_class = ConnectionAdapters.const_get(@options[:connection_adapter].to_s.capitalize) cluster_nodes = @options.delete(:cluster_nodes) fail "the cluster_nodes option must contain at least one node" if cluster_nodes.empty? cluster_nodes.each do |node| host, port = case node when Array node when Hash [node[:host], node[:port]] when String node.split(":") else fail "invalid entry in cluster_nodes option: #{node}" end port ||= 6379 @cluster_info.add_node(host, port.to_i) end @connections_mutex = Mutex.new @refresh_slots_mutex = Mutex.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(command, *args, &block) ⇒ Object
Delegated to #call.
142 143 144 |
# File 'lib/renoir/client.rb', line 142 def method_missing(command, *args, &block) call(command, *args, &block) end |
Instance Method Details
#call(*command) {|Object| ... } ⇒ Object
Call a Redis command.
111 112 113 114 115 116 |
# File 'lib/renoir/client.rb', line 111 def call(*command, &block) slot = get_slot_from_commands([command]) refresh_slots call_with_redirection(slot, [command], &block)[0] end |
#close ⇒ Object
Close all holding connections.
119 120 121 122 123 |
# File 'lib/renoir/client.rb', line 119 def close while entry = @connections.shift entry[1].close end end |
#each_node {|Object| ... } ⇒ Enumerable
Enumerate connections of cluster nodes.
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/renoir/client.rb', line 129 def each_node return enum_for(:each_node) unless block_given? @refresh_slots = true refresh_slots @cluster_info.nodes.each do |node| fetch_connection(node).with_raw_connection do |conn| yield conn end end end |
#eval(*args) {|Object| ... } ⇒ Object
Call EVAL command.
73 74 75 |
# File 'lib/renoir/client.rb', line 73 def eval(*args, &block) call(:eval, *args, &block) end |
#multi {|Renoir::Pipeline| ... } ⇒ Object
Note:
Return value of Pipeline methods is useless since "future variable" is not yet supported.
Pipeline commands and call them with MULTI/EXEC.
83 84 85 86 87 88 89 |
# File 'lib/renoir/client.rb', line 83 def multi(&block) commands = pipeline_commands(&block) slot = get_slot_from_commands(commands) refresh_slots call_with_redirection(slot, [[:multi]] + commands + [[:exec]]) end |
#pipelined {|Renoir::Pipeline| ... } ⇒ Object
Note:
Return value of Pipeline methods is useless since "future variable" is not yet supported.
Pipeline commands and call them.
97 98 99 100 101 102 103 |
# File 'lib/renoir/client.rb', line 97 def pipelined(&block) commands = pipeline_commands(&block) slot = get_slot_from_commands(commands) refresh_slots call_with_redirection(slot, commands) end |