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
43
# 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?Boolean

Returns:

  • (Boolean)


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

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

#disconnect!Object



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

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

#quitObject



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

def quit
  if connected?
    assert_ok(@semaphore.synchronize { 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