Introduction
This article will introduce redis Command Parsing, but to have a more intuitive understanding of redis Command Parsing, you must first understand the redis Command Protocol format.
Source code
None (or in network. c)
Analyze requests
* <Number of arguments> CR LF
$ <Number of bytes of argument 1> CR LF
<Argument data> CR LF
...
$ <Number of bytes of argument n> CR LF
<Argument data> CR LF
Redis currently supports the following common binary secure request formats. Specific instances include:
* 3
$3
Set
$5
Mykey
$7
Myvalue
Convert to the actual form string of each byte as follows: "* 3 \ r \ N $3 \ r \ NSET \ r \ N $5 \ r \ nmykey \ r \ N $7 \ r \ nmyvalue \ r \ n ". The protocol in this format is also used by the redis server to reply to the client, called bulk reply.
This unified request protocol is also used in redis to send list items of some columns to clients, which is called multi bulk reply. It is composed of different bulk reply of N plus a * <argc> \ r \ n prefix, where argc is the number of bulk reply.
Replies
Redis will reply different types of replies to cient. Depending on the first byte, different types of reply replies can be determined:
- Status reply, the first byte is "+"
- Error reply, the first byte is "-"
- Integer reply, the first byte is ":"
- Bulk reply, the first byte is "$"
- Multi bulk reply, the first byte is "*"
- Status reply
+ <Status strings> \ r \ n
-<Error type> <error info> \ r \ n
: <Integer value strings> \ r \ n
$ <Number of bytes of The args> \ r \ n <ARGs strings> \ r \ n
* <Number of bulk replies> \ r \ n <bulk reply list> \ r \ n
Description
In multi-bulk replies, if the length of an element is-1, the element is lost or null.
Multiple commands and pipelining
A client can use the same connection to send multiple commands. Pipeline technology allows you to use a single write operation to send multiple client commands without waiting for the server to reply after sending a command. The responses to these requests can be read in a unified manner at the end.
Inline commands
Redis also supports the early protocol format, that is, the form in the command line is as follows:
C: PINGS: +PONG
C: EXISTS somekeyS: :0