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.



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

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

  configure(url, timeout)
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

Instance Method Details

#configure(url, timeout) ⇒ Object



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

def configure(url, timeout)
  disconnect!

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

#connectObject



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

def connect
  establish_connection unless connected?

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

#connected?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/redic/client.rb', line 58

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

#disconnect!Object



62
63
64
65
66
67
# File 'lib/redic/client.rb', line 62

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

#quitObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/redic/client.rb', line 69

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

    true
  else
    false
  end
end

#readObject



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

def read
  #@connection.read
  @response
end

#write(command) ⇒ Object



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

def write(command)
  #@connection.write(command)
  cmd_name = command.shift.to_s.downcase
  block = command.find{ |cmd| cmd.is_a? Proc }
  #binding.pry if cmd_name == "subscribe"
  @response = if command.empty?
    @connection.send(cmd_name)
  else
    @connection.send(cmd_name, *command, &block)
  end
rescue => e
  return raise e if e.message =~ /ERR invalid DB index/
  return raise e if e.message =~  /ERR invalid password/
  @response = RuntimeError.new(e.message)
end