Class: Async::Redis::Protocol::RESP
- Inherits:
-
IO::Protocol::Line
- Object
- IO::Protocol::Line
- Async::Redis::Protocol::RESP
- Defined in:
- lib/async/redis/protocol/resp.rb
Constant Summary collapse
- CRLF =
"\r\n".freeze
Instance Method Summary collapse
-
#initialize(stream) ⇒ RESP
constructor
A new instance of RESP.
-
#read_object ⇒ Object
(also: #read_response)
def write_lines(*args) puts “write_lines(#argsargs.inspect)” super end.
- #write_object(object) ⇒ Object
-
#write_request(arguments) ⇒ Object
The redis server doesn’t want actual objects (e.g. integers) but only bulk strings.
Constructor Details
#initialize(stream) ⇒ RESP
Returns a new instance of RESP.
36 37 38 |
# File 'lib/async/redis/protocol/resp.rb', line 36 def initialize(stream) super(stream, CRLF) end |
Instance Method Details
#read_object ⇒ Object Also known as: read_response
def write_lines(*args) puts “write_lines(#Async::Redis::Protocol::RESP.argsargs.inspect)” super end
def read_line result = super puts “read_line #result”
return result end
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/async/redis/protocol/resp.rb', line 78 def read_object token = @stream.read(1) # puts "token: #{token}" case token when '$' length = read_line.to_i if length == -1 return nil else buffer = @stream.read(length) read_line # Eat trailing whitespace? return buffer end when '*' count = read_line.to_i array = Array.new(count) {read_object} return array when ':' return read_line.to_i when '-' raise ServerError.new(read_line) when '+' return read_line else raise NotImplementedError, "Implementation for token #{token} missing" end end |
#write_object(object) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/async/redis/protocol/resp.rb', line 53 def write_object(object) case object when String write_lines("$#{object.bytesize}", object) when Array write_array(object) when Integer write_lines(":#{object}") else write_object(object.to_redis) end end |
#write_request(arguments) ⇒ Object
The redis server doesn’t want actual objects (e.g. integers) but only bulk strings. So, we inline it for performance.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/async/redis/protocol/resp.rb', line 41 def write_request(arguments) write_lines("*#{arguments.count}") arguments.each do |argument| string = argument.to_s write_lines("$#{string.bytesize}", string) end @stream.flush end |