Class: Redis::Client
- Inherits:
-
Object
- Object
- Redis::Client
- Defined in:
- lib/redis/client.rb
Instance Attribute Summary collapse
-
#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(*args) ⇒ Object
Starting with 2.2.1, assume that this method is called with a single array argument.
-
#call_loop(*args) ⇒ Object
Starting with 2.2.1, assume that this method is called with a single array argument.
- #call_pipelined(commands, options = {}) ⇒ Object
-
#call_without_reply(command) ⇒ Object
Assume that this method is called with a single array argument.
- #call_without_timeout(*args) ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #id ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #location ⇒ Object
- #process(commands) ⇒ Object
- #read ⇒ Object
- #reconnect ⇒ Object
- #without_reconnect ⇒ Object
- #without_socket_timeout ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/redis/client.rb', line 7 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 end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
5 6 7 |
# File 'lib/redis/client.rb', line 5 def connection @connection end |
#db ⇒ Object
Returns the value of attribute db.
3 4 5 |
# File 'lib/redis/client.rb', line 3 def db @db end |
#host ⇒ Object
Returns the value of attribute host.
3 4 5 |
# File 'lib/redis/client.rb', line 3 def host @host end |
#logger ⇒ Object
Returns the value of attribute logger.
3 4 5 |
# File 'lib/redis/client.rb', line 3 def logger @logger end |
#password ⇒ Object
Returns the value of attribute password.
3 4 5 |
# File 'lib/redis/client.rb', line 3 def password @password end |
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/redis/client.rb', line 3 def path @path end |
#port ⇒ Object
Returns the value of attribute port.
3 4 5 |
# File 'lib/redis/client.rb', line 3 def port @port end |
#timeout ⇒ Object
Returns the value of attribute timeout.
4 5 6 |
# File 'lib/redis/client.rb', line 4 def timeout @timeout end |
Instance Method Details
#call(*args) ⇒ Object
Starting with 2.2.1, assume that this method is called with a single array argument. Check its size for backwards compat.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/redis/client.rb', line 39 def call(*args) if args.first.is_a?(Array) && args.size == 1 command = args.first else command = args end reply = process([command]) { read } raise reply if reply.is_a?(RuntimeError) reply end |
#call_loop(*args) ⇒ Object
Starting with 2.2.1, assume that this method is called with a single array argument. Check its size for backwards compat.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/redis/client.rb', line 60 def call_loop(*args) if args.first.is_a?(Array) && args.size == 1 command = args.first else command = args end error = nil result = without_socket_timeout do process([command]) do loop do reply = read if reply.is_a?(RuntimeError) 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_pipelined(commands, options = {}) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 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 |
# File 'lib/redis/client.rb', line 90 def call_pipelined(commands, = {}) [:raise] = true unless .has_key?(:raise) # 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-issueing # already succesfully executed commands. To circumvent this, don't retry # after the first reply has been read succesfully. first = process(commands) { read } error = first if first.is_a?(RuntimeError) begin remaining = commands.size - 1 if remaining > 0 replies = Array.new(remaining) do reply = read error ||= reply if reply.is_a?(RuntimeError) reply end replies.unshift first replies else replies = [first] end rescue Exception disconnect raise end # Raise first error in pipeline when we should raise. raise error if error && [:raise] replies end |
#call_without_reply(command) ⇒ Object
Assume that this method is called with a single array argument. No backwards compat here, since it was introduced in 2.2.2.
53 54 55 56 |
# File 'lib/redis/client.rb', line 53 def call_without_reply(command) process([command]) nil end |
#call_without_timeout(*args) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/redis/client.rb', line 127 def call_without_timeout(*args) without_socket_timeout do call(*args) end rescue Errno::ECONNRESET retry end |
#connect ⇒ Object
22 23 24 25 26 27 |
# File 'lib/redis/client.rb', line 22 def connect establish_connection call [:auth, @password] if @password call [:select, @db] if @db != 0 self end |
#connected? ⇒ Boolean
147 148 149 |
# File 'lib/redis/client.rb', line 147 def connected? connection.connected? end |
#disconnect ⇒ Object
151 152 153 |
# File 'lib/redis/client.rb', line 151 def disconnect connection.disconnect if connection.connected? end |
#id ⇒ Object
29 30 31 |
# File 'lib/redis/client.rb', line 29 def id "redis://#{location}/#{db}" end |
#location ⇒ Object
33 34 35 |
# File 'lib/redis/client.rb', line 33 def location @path || "#{@host}:#{@port}" end |
#process(commands) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/redis/client.rb', line 135 def process(commands) logging(commands) do ensure_connected do commands.each do |command| connection.write(command) end yield if block_given? end end end |
#read ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/redis/client.rb', line 160 def read begin connection.read rescue Errno::EAGAIN # We want to make sure it reconnects on the next command after the # timeout. Otherwise the server may reply in the meantime leaving # the protocol in a desync status. disconnect raise Errno::EAGAIN, "Timeout reading from the socket" rescue Errno::ECONNRESET raise Errno::ECONNRESET, "Connection lost" end end |
#reconnect ⇒ Object
155 156 157 158 |
# File 'lib/redis/client.rb', line 155 def reconnect disconnect connect end |
#without_reconnect ⇒ Object
188 189 190 191 192 193 194 195 |
# File 'lib/redis/client.rb', line 188 def without_reconnect begin original, @reconnect = @reconnect, false yield ensure @reconnect = original end end |
#without_socket_timeout ⇒ Object
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/redis/client.rb', line 177 def without_socket_timeout connect unless connected? begin self.timeout = 0 yield ensure self.timeout = @timeout if connected? end end |