class Redic::Client
Constants
- EMPTY
- SLASH
Attributes
timeout[RW]
Public Class Methods
new(url, timeout)
click to toggle source
# File lib/redic/client.rb, line 11 def initialize(url, timeout) @semaphore = Mutex.new @connection = false configure(url, timeout) end
Public Instance Methods
configure(url, timeout)
click to toggle source
# File lib/redic/client.rb, line 18 def configure(url, timeout) disconnect! @uri = URI.parse(url) @timeout = timeout end
connect() { || ... }
click to toggle source
# File lib/redic/client.rb, line 33 def connect @semaphore.synchronize do begin establish_connection unless connected? yield rescue Errno::ECONNRESET @connection = false retry end end end
connected?()
click to toggle source
# File lib/redic/client.rb, line 45 def connected? @connection && @connection.connected? end
disconnect!()
click to toggle source
# File lib/redic/client.rb, line 49 def disconnect! if connected? @connection.disconnect @connection = false end end
quit()
click to toggle source
# File lib/redic/client.rb, line 56 def quit if connected? assert_ok(@semaphore.synchronize { call("QUIT") }) disconnect! true else false end end
read()
click to toggle source
# File lib/redic/client.rb, line 25 def read @connection.read end
write(command)
click to toggle source
# File lib/redic/client.rb, line 29 def write(command) @connection.write(command) end
Private Instance Methods
assert(value, error)
click to toggle source
# File lib/redic/client.rb, line 91 def assert(value, error) raise error unless value end
assert_ok(reply)
click to toggle source
# File lib/redic/client.rb, line 95 def assert_ok(reply) assert(reply == "OK", reply) end
call(*args)
click to toggle source
# File lib/redic/client.rb, line 86 def call(*args) write(args) read end
establish_connection()
click to toggle source
# File lib/redic/client.rb, line 68 def establish_connection begin @connection = Redic::Connection.new(@uri, @timeout) rescue StandardError => err raise err, "Can't connect to: %s" % @uri end if @uri.scheme != "unix" if @uri.password assert_ok(call("AUTH", @uri.password)) end if @uri.path != EMPTY && @uri.path != SLASH assert_ok(call("SELECT", @uri.path[1..-1])) end end end