Class: Redis::Client
- Inherits:
-
Object
- Object
- Redis::Client
- Defined in:
- lib/redis/client.rb
Direct Known Subclasses
Defined Under Namespace
Classes: ThreadSafe
Constant Summary collapse
- MINUS =
"-".freeze
- PLUS =
"+".freeze
- COLON =
":".freeze
- DOLLAR =
"$".freeze
- ASTERISK =
"*".freeze
Instance Attribute Summary collapse
-
#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.
-
#port ⇒ Object
Returns the value of attribute port.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #call(*args) ⇒ Object
- #call_loop(*args) ⇒ Object
- #call_pipelined(commands) ⇒ Object
- #call_without_timeout(*args) ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #id ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #process(*commands) ⇒ Object
- #read ⇒ Object
- #reconnect ⇒ Object
- #without_socket_timeout ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 |
# File 'lib/redis/client.rb', line 12 def initialize( = {}) @host = [:host] || "127.0.0.1" @port = ([:port] || 6379).to_i @db = ([:db] || 0).to_i @timeout = ([:timeout] || 5).to_i @password = [:password] @logger = [:logger] @sock = nil end |
Instance Attribute Details
#db ⇒ Object
Returns the value of attribute db.
9 10 11 |
# File 'lib/redis/client.rb', line 9 def db @db end |
#host ⇒ Object
Returns the value of attribute host.
9 10 11 |
# File 'lib/redis/client.rb', line 9 def host @host end |
#logger ⇒ Object
Returns the value of attribute logger.
9 10 11 |
# File 'lib/redis/client.rb', line 9 def logger @logger end |
#password ⇒ Object
Returns the value of attribute password.
9 10 11 |
# File 'lib/redis/client.rb', line 9 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
9 10 11 |
# File 'lib/redis/client.rb', line 9 def port @port end |
#timeout ⇒ Object
Returns the value of attribute timeout.
10 11 12 |
# File 'lib/redis/client.rb', line 10 def timeout @timeout end |
Instance Method Details
#call(*args) ⇒ Object
33 34 35 36 37 |
# File 'lib/redis/client.rb', line 33 def call(*args) process(args) do read end end |
#call_loop(*args) ⇒ Object
39 40 41 42 43 |
# File 'lib/redis/client.rb', line 39 def call_loop(*args) process(args) do loop { yield(read) } end end |
#call_pipelined(commands) ⇒ Object
45 46 47 48 49 |
# File 'lib/redis/client.rb', line 45 def call_pipelined(commands) process(*commands) do Array.new(commands.size) { read } end end |
#call_without_timeout(*args) ⇒ Object
51 52 53 54 55 |
# File 'lib/redis/client.rb', line 51 def call_without_timeout(*args) without_socket_timeout do call(*args) end end |
#connect ⇒ Object
22 23 24 25 26 27 |
# File 'lib/redis/client.rb', line 22 def connect connect_to(@host, @port) call(:auth, @password) if @password call(:select, @db) if @db != 0 @sock end |
#connected? ⇒ Boolean
66 67 68 |
# File 'lib/redis/client.rb', line 66 def connected? !! @sock end |
#disconnect ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/redis/client.rb', line 70 def disconnect return unless connected? begin @sock.close rescue ensure @sock = nil end end |
#id ⇒ Object
29 30 31 |
# File 'lib/redis/client.rb', line 29 def id "redis://#{host}:#{port}/#{db}" end |
#process(*commands) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/redis/client.rb', line 57 def process(*commands) logging(commands) do ensure_connected do @sock.write(join_commands(commands)) yield if block_given? end end end |
#read ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/redis/client.rb', line 86 def read # We read the first byte using read() mainly because gets() is # immune to raw socket timeouts. begin reply_type = @sock.read(1) 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" end raise Errno::ECONNRESET, "Connection lost" unless reply_type format_reply(reply_type, @sock.gets) end |
#reconnect ⇒ Object
81 82 83 84 |
# File 'lib/redis/client.rb', line 81 def reconnect disconnect connect end |
#without_socket_timeout ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/redis/client.rb', line 107 def without_socket_timeout ensure_connected do begin self.timeout = 0 yield ensure self.timeout = @timeout end end end |