Class: Redis::Connection
- Inherits:
-
Object
- Object
- Redis::Connection
- Defined in:
- lib/redis/connection.rb
Constant Summary collapse
- MINUS =
"-".freeze
- PLUS =
"+".freeze
- COLON =
":".freeze
- DOLLAR =
"$".freeze
- ASTERISK =
"*".freeze
- COMMAND_DELIMITER =
"\r\n"
Instance Method Summary collapse
- #build_command(name, *args) ⇒ Object
- #connect(host, port) ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #format_bulk_reply(line) ⇒ Object
- #format_error_reply(line) ⇒ Object
- #format_integer_reply(line) ⇒ Object
- #format_multi_bulk_reply(line) ⇒ Object
- #format_reply(reply_type, line) ⇒ Object
- #format_status_reply(line) ⇒ Object
-
#initialize ⇒ Connection
constructor
A new instance of Connection.
- #read ⇒ Object
- #timeout=(usecs) ⇒ Object
- #write(command) ⇒ Object
Constructor Details
#initialize ⇒ Connection
Returns a new instance of Connection.
9 10 11 |
# File 'lib/redis/connection.rb', line 9 def initialize @sock = nil end |
Instance Method Details
#build_command(name, *args) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/redis/connection.rb', line 49 def build_command(name, *args) command = [] command << "*#{args.size + 1}" command << "$#{string_size name}" command << name args.each do |arg| arg = arg.to_s command << "$#{string_size arg}" command << arg end command end |
#connect(host, port) ⇒ Object
17 18 19 20 |
# File 'lib/redis/connection.rb', line 17 def connect(host, port) @sock = TCPSocket.new(host, port) @sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1 end |
#connected? ⇒ Boolean
13 14 15 |
# File 'lib/redis/connection.rb', line 13 def connected? !! @sock end |
#disconnect ⇒ Object
22 23 24 25 26 27 |
# File 'lib/redis/connection.rb', line 22 def disconnect @sock.close rescue ensure @sock = nil end |
#format_bulk_reply(line) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/redis/connection.rb', line 97 def format_bulk_reply(line) bulklen = line.to_i return if bulklen == -1 reply = encode(@sock.read(bulklen)) @sock.read(2) # Discard CRLF. reply end |
#format_error_reply(line) ⇒ Object
85 86 87 |
# File 'lib/redis/connection.rb', line 85 def format_error_reply(line) raise "-" + line.strip end |
#format_integer_reply(line) ⇒ Object
93 94 95 |
# File 'lib/redis/connection.rb', line 93 def format_integer_reply(line) line.to_i end |
#format_multi_bulk_reply(line) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/redis/connection.rb', line 105 def format_multi_bulk_reply(line) n = line.to_i return if n == -1 Array.new(n) { read } end |
#format_reply(reply_type, line) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/redis/connection.rb', line 74 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 |
#format_status_reply(line) ⇒ Object
89 90 91 |
# File 'lib/redis/connection.rb', line 89 def format_status_reply(line) line.strip end |
#read ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/redis/connection.rb', line 64 def read # We read the first byte using read() mainly because gets() is # immune to raw socket timeouts. reply_type = @sock.read(1) raise Errno::ECONNRESET unless reply_type format_reply(reply_type, @sock.gets) end |
#timeout=(usecs) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/redis/connection.rb', line 29 def timeout=(usecs) secs = Integer(usecs / 1_000_000) usecs = Integer(usecs - (secs * 1_000_000)) # 0 - 999_999 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
44 45 46 47 |
# File 'lib/redis/connection.rb', line 44 def write(command) @sock.write(build_command(*command).join(COMMAND_DELIMITER)) @sock.write(COMMAND_DELIMITER) end |