Class: Redis::Client
- Inherits:
-
Object
- Object
- Redis::Client
- Defined in:
- lib/redis/client.rb
Direct Known Subclasses
Defined Under Namespace
Classes: ThreadSafe
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.
-
#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.
7 8 9 10 11 12 13 14 15 |
# File 'lib/redis/client.rb', line 7 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] @connection = Connection.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 |
#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
28 29 30 31 32 |
# File 'lib/redis/client.rb', line 28 def call(*args) process(args) do read end end |
#call_loop(*args) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/redis/client.rb', line 34 def call_loop(*args) without_socket_timeout do process(args) do loop { yield(read) } end end end |
#call_pipelined(commands) ⇒ Object
42 43 44 45 46 |
# File 'lib/redis/client.rb', line 42 def call_pipelined(commands) process(*commands) do Array.new(commands.size) { read } end end |
#call_without_timeout(*args) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/redis/client.rb', line 48 def call_without_timeout(*args) without_socket_timeout do call(*args) end rescue Errno::ECONNRESET retry end |
#connect ⇒ Object
17 18 19 20 21 22 |
# File 'lib/redis/client.rb', line 17 def connect connect_to(@host, @port) call(:auth, @password) if @password call(:select, @db) if @db != 0 self end |
#connected? ⇒ Boolean
68 69 70 |
# File 'lib/redis/client.rb', line 68 def connected? connection.connected? end |
#disconnect ⇒ Object
72 73 74 |
# File 'lib/redis/client.rb', line 72 def disconnect connection.disconnect if connection.connected? end |
#id ⇒ Object
24 25 26 |
# File 'lib/redis/client.rb', line 24 def id "redis://#{host}:#{port}/#{db}" end |
#process(*commands) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/redis/client.rb', line 56 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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/redis/client.rb', line 81 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
76 77 78 79 |
# File 'lib/redis/client.rb', line 76 def reconnect disconnect connect end |
#without_socket_timeout ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/redis/client.rb', line 98 def without_socket_timeout connect unless connected? begin self.timeout = 0 yield ensure self.timeout = @timeout if connected? end end |