Talk less and do more .

redis What exactly is the agreement ?
Redis The agreement is called RESP(redis Serialization protocol ) And redis The server communicates . It's for redis A set of serialization protocol designed .
This agreement is actually redis Of 1.2 We have the version , But here it is redis2.0 To become redis Standard of communication protocol .
RESP Characteristics of :

1.RESP You can serialize different data types , For example, integers , character string , array . There is also a specific type of error . The request is sent as an array of strings from the client to Redis The server , These strings represent the parameters of the command to execute .Redis Reply with command specific data types .

2.RESP It's binary secure , And there is no need to process batch data transferred from one process to another , Because it uses prefix length to transmit bulk data . be careful : The protocols outlined here are for clients only - Server communication .Redis
Cluster Use different binary protocols to exchange messages between nodes .

RESP Description of the agreement
RESP Supports serialization protocols for the following data types : Simple string , error , integer , Massive strings and arrays .
RESP stay Redis As a request - The way to respond to the protocol is as follows :
1. The client uses the command as a bulk string RESP Array sent to Redis The server .
2. The server implements the RESP Type one to reply .
stay RESP in , The type of some data depends on the first byte :
For simple strings , The first byte of the reply is “ +”
For errors , The first byte of the reply is “-”
For integers , The first byte of the reply is “:”
For batch strings , The first byte of the reply is “ $”
For arrays , The first byte of the reply is “ ”
stay RESP in , Different parts of the agreement are always *“ \ r \ n”(CRLF)** termination .

redis Communication protocol of ( supplement , Serialization protocol of data type ) A little boring can be ignored
We talked about it earlier resp There are five serialization protocols for data types . Now let's talk about these five meanings carefully .

RESP Simple string

Simple strings are encoded as follows : A plus sign , The following one cannot contain CR or LF Character string ( Line breaks are not allowed ), with CRLF termination ( Namely “ \ r \ n”).
Simple strings are used to transmit non binary secure strings with minimal overhead . for example , many Redis The command only answers when successful “

OK” To send binary secure strings , Please use RESP Batch string . When Redis When replying with a simple string , The client library should return a string to the caller , The string consists of ’+' The first character that follows until the end of the string , Not including the last CRLF byte .

RESP error

RESP Has a specific data type for the error . actually , Errors and RESP Simple strings are exactly the same , But the first character is a minus sign “-” Not a plus sign .RESP The real difference between a simple string in and an error in is , The client treats errors as exceptions , The string that makes up the error type is the error message itself .

The basic format is :
“-Error message\r\n”
Send an error reply only when an error occurs , for example , If you try to perform an operation on the wrong data type , Or the command doesn't exist, etc . When an error reply is received , Library client should throw an exception .
Here is an example of an error reply :
-ERR unknown command ‘foobar’
-WRONGTYPE Operation against a key holding the wrong kind of value
“-” First word after , Until the first space or newline character , Represents the type of error returned . It's just Redis Agreement on use , no RESP Part of the wrong format .

for example ,ERR It's a general error ,WRONGTYPE It's a more specific mistake , Indicates that the client attempted to perform an operation on the wrong data type . This is called the error prefix , Is a way to enable the client to understand the type of error returned by the server , It doesn't have to depend on the exact information given , The message may change over time .
The client implementation can return different kinds of exceptions for different errors , Or you can provide a common way to catch errors by providing the error name directly to the caller as a string .
however , This feature should not be considered critical , Because it's rarely useful , And limited client implementations may simply return common error conditions , for example false.

RESP integer

This type is just a CRLF Terminated string , It represents an integer , with “:” Bytes as prefix . for example ,“:0 \ r \ n” or “:1000 \ r
n” It's an integer reply .

many Redis Command return RESP integer , image INCR,LLEN and LASTSAVE.
The returned integer has no special meaning , It's just INCR Increment number of ,LASTSAVE Of UNIX Time and so on . however , Ensure that the returned integer is in the signed 64 Bit integer range .
In order to return true or false, It is also widely used Integer reply . for example , image EXISTS or SISMEMBER Such a command would be true return 1, by false return 0.
If the operation is actually performed , Other commands ( as SADD,SREM and SETNX) Will return 1, Otherwise return 0.
The following command will reply with an integer :SETNX,DEL,

RESP Bulk string

Use a large string to represent a single maximum length of 512 MB Binary security string for . Batch strings are encoded in the following way : “
$” byte , Followed by the number of bytes that make up the string ( Prefixed length ), from CRLF( Carriage return /r/n) termination .

Actual string data . final CRLF.

therefore , character string “ foobar” The code of the is as follows :
When an empty string is just :
“KaTeX parse error: Can't use function '\r' in math mode at position 2:
0\̲r̲\n\r\n" You can also use RE…-1\r\n”
This is called Null Bulk String.
When the server uses Null Bulk String When replying , Client Library API An empty string should not be returned , Instead, return nil object . for example ,Ruby Library should return “
nil”, and C Library should return NULL( Or in the reply Set special flag in object ), And so on .

RESP array

The array type can be used by the client to send commands to the server , Similarly, when some commands combine elements back to the client , The array type is also used as the reply type . It uses “*”
start , It is followed by the number of returned elements , Then there was CRLF, Then there is the type of each element in the array .

©2019-2020 Toolsou All rights reserved,
Python Garbage collection and memory leak hive Summary of processing methods for a large number of small files The difference between memory overflow and memory leak , Causes and Solutions Create data mysql Library process You don't know ——HarmonyOS stay Vue Use in Web WorkerSparkSQL Achieve partition overlay write msf Generate Trojan horse attack android mobile phone Linux Page replacement algorithm C Language implementation Django Personal blog building tutorial --- Time classified archiving