Class: Redis::Client
- Inherits:
-
Object
- Object
- Redis::Client
- Defined in:
- lib/redis/client.rb
Instance Attribute Summary collapse
-
#command_map ⇒ Object
readonly
Returns the value of attribute command_map.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#db ⇒ Object
Returns the value of attribute db.
-
#host ⇒ Object
Returns the value of attribute host.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#password ⇒ Object
Returns the value of attribute password.
-
#path ⇒ Object
Returns the value of attribute path.
-
#port ⇒ Object
Returns the value of attribute port.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #call(command, &block) ⇒ Object
- #call_loop(command) ⇒ Object
- #call_pipeline(pipeline) ⇒ Object
- #call_pipelined(commands) ⇒ Object
- #call_without_timeout(command, &blk) ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #id ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #io ⇒ Object
- #location ⇒ Object
- #process(commands) ⇒ Object
- #read ⇒ Object
- #reconnect ⇒ Object
- #without_reconnect ⇒ Object
- #without_socket_timeout ⇒ Object
- #write(command) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/redis/client.rb', line 10 def initialize( = {}) @path = [:path] if @path.nil? @host = [:host] || "127.0.0.1" @port = ([:port] || 6379).to_i end @db = ([:db] || 0).to_i @timeout = ([:timeout] || 5).to_f @password = [:password] @logger = [:logger] @reconnect = true @connection = Connection.drivers.last.new @command_map = {} end |
Instance Attribute Details
#command_map ⇒ Object (readonly)
Returns the value of attribute command_map.
8 9 10 |
# File 'lib/redis/client.rb', line 8 def command_map @command_map end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
7 8 9 |
# File 'lib/redis/client.rb', line 7 def connection @connection end |
#db ⇒ Object
Returns the value of attribute db.
5 6 7 |
# File 'lib/redis/client.rb', line 5 def db @db end |
#host ⇒ Object
Returns the value of attribute host.
5 6 7 |
# File 'lib/redis/client.rb', line 5 def host @host end |
#logger ⇒ Object
Returns the value of attribute logger.
5 6 7 |
# File 'lib/redis/client.rb', line 5 def logger @logger end |
#password ⇒ Object
Returns the value of attribute password.
5 6 7 |
# File 'lib/redis/client.rb', line 5 def password @password end |
#path ⇒ Object
Returns the value of attribute path.
5 6 7 |
# File 'lib/redis/client.rb', line 5 def path @path end |
#port ⇒ Object
Returns the value of attribute port.
5 6 7 |
# File 'lib/redis/client.rb', line 5 def port @port end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
6 7 8 |
# File 'lib/redis/client.rb', line 6 def timeout @timeout end |
Instance Method Details
#call(command, &block) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/redis/client.rb', line 41 def call(command, &block) reply = process([command]) { read } raise reply if reply.is_a?(CommandError) if block block.call(reply) else reply end end |
#call_loop(command) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/redis/client.rb', line 52 def call_loop(command) error = nil result = without_socket_timeout do process([command]) do loop do reply = read if reply.is_a?(CommandError) error = reply break else yield reply end end end end # Raise error when previous block broke out of the loop. raise error if error # Result is set to the value that the provided block used to break. result end |
#call_pipeline(pipeline) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/redis/client.rb', line 76 def call_pipeline(pipeline) without_reconnect_wrapper = lambda do |&blk| blk.call end without_reconnect_wrapper = lambda do |&blk| without_reconnect(&blk) end if pipeline.without_reconnect? shutdown_wrapper = lambda do |&blk| blk.call end shutdown_wrapper = lambda do |&blk| begin blk.call rescue ConnectionError # Assume the pipeline was sent in one piece, but execution of # SHUTDOWN caused none of the replies for commands that were executed # prior to it from coming back around. nil end end if pipeline.shutdown? without_reconnect_wrapper.call do shutdown_wrapper.call do pipeline.finish(call_pipelined(pipeline.commands)) end end end |
#call_pipelined(commands) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/redis/client.rb', line 101 def call_pipelined(commands) return [] if commands.empty? # The method #ensure_connected (called from #process) reconnects once on # I/O errors. To make an effort in making sure that commands are not # executed more than once, only allow reconnection before the first reply # has been read. When an error occurs after the first reply has been # read, retrying would re-execute the entire pipeline, thus re-issuing # already successfully executed commands. To circumvent this, don't retry # after the first reply has been read successfully. result = Array.new(commands.size) reconnect = @reconnect begin process(commands) do result[0] = read @reconnect = false (commands.size - 1).times do |i| result[i + 1] = read end end ensure @reconnect = reconnect end result end |
#call_without_timeout(command, &blk) ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/redis/client.rb', line 132 def call_without_timeout(command, &blk) without_socket_timeout do call(command, &blk) end rescue ConnectionError retry end |
#connect ⇒ Object
26 27 28 29 30 31 |
# File 'lib/redis/client.rb', line 26 def connect establish_connection call [:auth, @password] if @password call [:select, @db] if @db != 0 self end |
#connected? ⇒ Boolean
157 158 159 |
# File 'lib/redis/client.rb', line 157 def connected? connection.connected? end |
#disconnect ⇒ Object
161 162 163 |
# File 'lib/redis/client.rb', line 161 def disconnect connection.disconnect if connection.connected? end |
#id ⇒ Object
33 34 35 |
# File 'lib/redis/client.rb', line 33 def id "redis://#{location}/#{db}" end |
#io ⇒ Object
170 171 172 173 174 175 176 |
# File 'lib/redis/client.rb', line 170 def io yield rescue TimeoutError raise TimeoutError, "Connection timed out" rescue Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EBADF, Errno::EINVAL => e raise ConnectionError, "Connection lost (%s)" % [e.class.name.split("::").last] end |
#location ⇒ Object
37 38 39 |
# File 'lib/redis/client.rb', line 37 def location @path || "#{@host}:#{@port}" end |
#process(commands) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/redis/client.rb', line 140 def process(commands) logging(commands) do ensure_connected do commands.each do |command| if command_map[command.first] command = command.dup command[0] = command_map[command.first] end connection.write(command) end yield if block_given? end end end |
#read ⇒ Object
178 179 180 181 182 |
# File 'lib/redis/client.rb', line 178 def read io do connection.read end end |
#reconnect ⇒ Object
165 166 167 168 |
# File 'lib/redis/client.rb', line 165 def reconnect disconnect connect end |
#without_reconnect ⇒ Object
201 202 203 204 205 206 207 208 |
# File 'lib/redis/client.rb', line 201 def without_reconnect begin original, @reconnect = @reconnect, false yield ensure @reconnect = original end end |
#without_socket_timeout ⇒ Object
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/redis/client.rb', line 190 def without_socket_timeout connect unless connected? begin connection.timeout = 0 yield ensure connection.timeout = @timeout if connected? end end |
#write(command) ⇒ Object
184 185 186 187 188 |
# File 'lib/redis/client.rb', line 184 def write(command) io do connection.write(command) end end |