Class: Redic::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/redic/client.rb

Constant Summary collapse

EMPTY =
"".freeze
SLASH =
"/".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, timeout) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
# File 'lib/redic/client.rb', line 11

def initialize(url, timeout)
  @semaphore = Mutex.new
  @connection = false

  configure(url, timeout)
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



9
10
11
# File 'lib/redic/client.rb', line 9

def timeout
  @timeout
end

Instance Method Details

#configure(url, timeout) ⇒ Object



18
19
20
21
22
23
# File 'lib/redic/client.rb', line 18

def configure(url, timeout)
  disconnect!

  @uri = URI.parse(url)
  @timeout = timeout
end

#connectObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/redic/client.rb', line 33

def connect
  establish_connection unless connected?

  @semaphore.synchronize do
    yield
  end
rescue Errno::ECONNRESET
  @connection = false
  retry
end

#connected?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/redic/client.rb', line 44

def connected?
  @connection && @connection.connected?
end

#disconnect!Object



48
49
50
51
52
53
# File 'lib/redic/client.rb', line 48

def disconnect!
  if connected?
    @connection.disconnect
    @connection = false
  end
end

#quitObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/redic/client.rb', line 55

def quit
  if connected?
    assert_ok(call("QUIT"))
    disconnect!

    true
  else
    false
  end
end

#readObject



25
26
27
# File 'lib/redic/client.rb', line 25

def read
  @connection.read
end

#write(command) ⇒ Object



29
30
31
# File 'lib/redic/client.rb', line 29

def write(command)
  @connection.write(command)
end