Class: Redis::Connection::Ruby
- Inherits:
-
Object
- Object
- Redis::Connection::Ruby
show all
- Includes:
- CommandHelper
- Defined in:
- lib/redis/connection/ruby.rb
Constant Summary
collapse
- MINUS =
"-".freeze
- PLUS =
"+".freeze
- COLON =
":".freeze
- DOLLAR =
"$".freeze
- ASTERISK =
"*".freeze
CommandHelper::COMMAND_DELIMITER
Instance Method Summary
collapse
#build_command
Constructor Details
#initialize ⇒ Ruby
Returns a new instance of Ruby.
16
17
18
|
# File 'lib/redis/connection/ruby.rb', line 16
def initialize
@sock = nil
end
|
Instance Method Details
#connect(host, port, timeout) ⇒ Object
24
25
26
27
28
29
|
# File 'lib/redis/connection/ruby.rb', line 24
def connect(host, port, timeout)
with_timeout(timeout.to_f / 1_000_000) do
@sock = TCPSocket.new(host, port)
@sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
end
end
|
#connect_unix(path, timeout) ⇒ Object
31
32
33
34
35
|
# File 'lib/redis/connection/ruby.rb', line 31
def connect_unix(path, timeout)
with_timeout(timeout.to_f / 1_000_000) do
@sock = UNIXSocket.new(path)
end
end
|
#connected? ⇒ Boolean
20
21
22
|
# File 'lib/redis/connection/ruby.rb', line 20
def connected?
!! @sock
end
|
#disconnect ⇒ Object
37
38
39
40
41
42
|
# File 'lib/redis/connection/ruby.rb', line 37
def disconnect
@sock.close
rescue
ensure
@sock = nil
end
|
94
95
96
97
98
99
100
|
# File 'lib/redis/connection/ruby.rb', line 94
def format_bulk_reply(line)
bulklen = line.to_i
return if bulklen == -1
reply = encode(@sock.read(bulklen))
@sock.read(2) reply
end
|
82
83
84
|
# File 'lib/redis/connection/ruby.rb', line 82
def format_error_reply(line)
RuntimeError.new(line.strip)
end
|
90
91
92
|
# File 'lib/redis/connection/ruby.rb', line 90
def format_integer_reply(line)
line.to_i
end
|
102
103
104
105
106
107
|
# File 'lib/redis/connection/ruby.rb', line 102
def format_multi_bulk_reply(line)
n = line.to_i
return if n == -1
Array.new(n) { read }
end
|
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/redis/connection/ruby.rb', line 71
def format_reply(reply_type, line)
case reply_type
when MINUS then format_error_reply(line)
when PLUS then format_status_reply(line)
when COLON then format_integer_reply(line)
when DOLLAR then format_bulk_reply(line)
when ASTERISK then format_multi_bulk_reply(line)
else raise ProtocolError.new(reply_type)
end
end
|
86
87
88
|
# File 'lib/redis/connection/ruby.rb', line 86
def format_status_reply(line)
line.strip
end
|
#read ⇒ Object
61
62
63
64
65
66
67
68
69
|
# File 'lib/redis/connection/ruby.rb', line 61
def read
reply_type = @sock.read(1)
raise Errno::ECONNRESET unless reply_type
format_reply(reply_type, @sock.gets)
end
|
#timeout=(usecs) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/redis/connection/ruby.rb', line 44
def timeout=(usecs)
secs = Integer(usecs / 1_000_000)
usecs = Integer(usecs - (secs * 1_000_000))
optval = [secs, usecs].pack("l_2")
begin
@sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
@sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
rescue Errno::ENOPROTOOPT
end
end
|
#write(command) ⇒ Object
57
58
59
|
# File 'lib/redis/connection/ruby.rb', line 57
def write(command)
@sock.write(build_command(command))
end
|