Class: Redis::Client
- Inherits:
-
Object
- Object
- Redis::Client
- Defined in:
- lib/redis/client.rb
Defined Under Namespace
Classes: Connector
Constant Summary collapse
- DEFAULTS =
{ :url => lambda { ENV["REDIS_URL"] }, :scheme => "redis", :host => "127.0.0.1", :port => 6379, :path => nil, :timeout => 5.0, :password => nil, :db => 0, :driver => nil, :id => nil, :tcp_keepalive => 0, :reconnect_attempts => 1, :inherit_socket => false }
Instance Attribute Summary collapse
-
#command_map ⇒ Object
readonly
Returns the value of attribute command_map.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #call(command, &block) ⇒ Object
- #call_loop(command) ⇒ Object
- #call_pipeline(pipeline) ⇒ Object
- #call_pipelined(commands) ⇒ Object
- #call_with_timeout(command, timeout, &blk) ⇒ Object
- #call_without_timeout(command, &blk) ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #db ⇒ Object
- #db=(db) ⇒ Object
- #disconnect ⇒ Object
- #driver ⇒ Object
- #host ⇒ Object
- #id ⇒ Object
- #inherit_socket? ⇒ Boolean
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #io ⇒ Object
- #location ⇒ Object
- #options ⇒ Object
- #password ⇒ Object
- #path ⇒ Object
- #port ⇒ Object
- #process(commands) ⇒ Object
- #read ⇒ Object
- #reconnect ⇒ Object
- #scheme ⇒ Object
- #timeout ⇒ Object
- #with_reconnect(val = true) ⇒ Object
- #with_socket_timeout(timeout) ⇒ Object
- #without_reconnect(&blk) ⇒ Object
- #without_socket_timeout(&blk) ⇒ Object
- #write(command) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/redis/client.rb', line 72 def initialize( = {}) @options = () @reconnect = true @logger = @options[:logger] @connection = nil @command_map = {} if .include?(:sentinels) @connector = Connector::Sentinel.new(@options) else @connector = Connector.new(@options) end end |
Instance Attribute Details
#command_map ⇒ Object (readonly)
Returns the value of attribute command_map.
70 71 72 |
# File 'lib/redis/client.rb', line 70 def command_map @command_map end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
69 70 71 |
# File 'lib/redis/client.rb', line 69 def connection @connection end |
#logger ⇒ Object
Returns the value of attribute logger.
68 69 70 |
# File 'lib/redis/client.rb', line 68 def logger @logger end |
Instance Method Details
#call(command, &block) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/redis/client.rb', line 108 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
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/redis/client.rb', line 119 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
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/redis/client.rb', line 143 def call_pipeline(pipeline) with_reconnect pipeline.with_reconnect? do begin pipeline.finish(call_pipelined(pipeline.commands)).tap do self.db = pipeline.db if pipeline.db end rescue ConnectionError => e return nil if pipeline.shutdown? # 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. raise e end end end |
#call_pipelined(commands) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/redis/client.rb', line 159 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_with_timeout(command, timeout, &blk) ⇒ Object
190 191 192 193 194 195 196 |
# File 'lib/redis/client.rb', line 190 def call_with_timeout(command, timeout, &blk) with_socket_timeout(timeout) do call(command, &blk) end rescue ConnectionError retry end |
#call_without_timeout(command, &blk) ⇒ Object
198 199 200 |
# File 'lib/redis/client.rb', line 198 def call_without_timeout(command, &blk) call_with_timeout(command, 0, &blk) end |
#connect ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/redis/client.rb', line 86 def connect @pid = Process.pid # Don't try to reconnect when the connection is fresh with_reconnect(false) do establish_connection call [:auth, password] if password call [:select, db] if db != 0 @connector.check(self) end self end |
#connected? ⇒ Boolean
219 220 221 |
# File 'lib/redis/client.rb', line 219 def connected? !! (connection && connection.connected?) end |
#db ⇒ Object
52 53 54 |
# File 'lib/redis/client.rb', line 52 def db @options[:db] end |
#db=(db) ⇒ Object
56 57 58 |
# File 'lib/redis/client.rb', line 56 def db=(db) @options[:db] = db.to_i end |
#disconnect ⇒ Object
223 224 225 |
# File 'lib/redis/client.rb', line 223 def disconnect connection.disconnect if connected? end |
#driver ⇒ Object
60 61 62 |
# File 'lib/redis/client.rb', line 60 def driver @options[:driver] end |
#host ⇒ Object
32 33 34 |
# File 'lib/redis/client.rb', line 32 def host @options[:host] end |
#id ⇒ Object
100 101 102 |
# File 'lib/redis/client.rb', line 100 def id @options[:id] || "redis://#{location}/#{db}" end |
#inherit_socket? ⇒ Boolean
64 65 66 |
# File 'lib/redis/client.rb', line 64 def inherit_socket? @options[:inherit_socket] end |
#io ⇒ Object
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/redis/client.rb', line 232 def io yield rescue TimeoutError => e1 # Add a message to the exception without destroying the original stack e2 = TimeoutError.new("Connection timed out") e2.set_backtrace(e1.backtrace) raise e2 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
104 105 106 |
# File 'lib/redis/client.rb', line 104 def location path || "#{host}:#{port}" end |
#options ⇒ Object
24 25 26 |
# File 'lib/redis/client.rb', line 24 def Marshal.load(Marshal.dump(@options)) end |
#password ⇒ Object
48 49 50 |
# File 'lib/redis/client.rb', line 48 def password @options[:password] end |
#path ⇒ Object
40 41 42 |
# File 'lib/redis/client.rb', line 40 def path @options[:path] end |
#port ⇒ Object
36 37 38 |
# File 'lib/redis/client.rb', line 36 def port @options[:port] end |
#process(commands) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/redis/client.rb', line 202 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 write(command) end yield if block_given? end end end |
#read ⇒ Object
243 244 245 246 247 |
# File 'lib/redis/client.rb', line 243 def read io do connection.read end end |
#reconnect ⇒ Object
227 228 229 230 |
# File 'lib/redis/client.rb', line 227 def reconnect disconnect connect end |
#scheme ⇒ Object
28 29 30 |
# File 'lib/redis/client.rb', line 28 def scheme @options[:scheme] end |
#timeout ⇒ Object
44 45 46 |
# File 'lib/redis/client.rb', line 44 def timeout @options[:timeout] end |
#with_reconnect(val = true) ⇒ Object
270 271 272 273 274 275 276 277 |
# File 'lib/redis/client.rb', line 270 def with_reconnect(val=true) begin original, @reconnect = @reconnect, val yield ensure @reconnect = original end end |
#with_socket_timeout(timeout) ⇒ Object
255 256 257 258 259 260 261 262 263 264 |
# File 'lib/redis/client.rb', line 255 def with_socket_timeout(timeout) connect unless connected? begin connection.timeout = timeout yield ensure connection.timeout = self.timeout if connected? end end |
#without_reconnect(&blk) ⇒ Object
279 280 281 |
# File 'lib/redis/client.rb', line 279 def without_reconnect(&blk) with_reconnect(false, &blk) end |
#without_socket_timeout(&blk) ⇒ Object
266 267 268 |
# File 'lib/redis/client.rb', line 266 def without_socket_timeout(&blk) with_socket_timeout(0, &blk) end |
#write(command) ⇒ Object
249 250 251 252 253 |
# File 'lib/redis/client.rb', line 249 def write(command) io do connection.write(command) end end |