Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the Communication Protocol RESP in Redis

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how the communication protocol RESP in Redis is realized, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

RESP

RESP (Redis Serialization Protocol) is the abbreviation of Redis serialization protocol, which is a plain text protocol with simple implementation process and good parsing performance.

Five unit types

The Redis protocol divides the transmitted data structure into five smallest unit types, and the unit ends uniformly with the carriage return newline symbol\ r\ n.

1. A single-line string begins with a + symbol

Example: + hello world\ r\ n

two。 The multiline string begins with the $symbol, followed by the string length

Example: $11\ r\ nhello world\ r\ n

Multiline strings can also be used to represent single-line strings

3. The integer value begins with the: symbol, followed by a string of integers

Example: 1024\ r\ n

4. The error message begins with a-symbol

Wrong parameter type

Example:-WRONGTYPE Operation against a key holding the wrong kind of value\ r\ n

5. The array begins with a * sign, followed by the length of the array

Example: * 3\ r\ nVOL\ r\ nVOL2\ r\ nR3\ r\ n

Two special types

1. NULL

NULL is represented by a multi-line string with a length of-1

Example: $- 1\ r\ n

two。 Empty string

The empty string is represented by a multi-line string with a length of 0.

Example: $0\ r\ n\ r\ n

The empty string has two\ r\ n, because the one between them is the empty string.

The client requests the server

The client sends instructions to the server in only one format, that is, an array of multiline strings.

For example, a simple set instruction set x x will be serialized into the following string

* 3\ r\ nroom3\ r\ nset\ r\ nroom1\ r\ nx\ r\ nroom1\ r\ nx\ r\ n

The console is shown as follows

* 3 $3set$1x$1x server response client

When the server responds to client information, it will use a variety of data structures, which is much more complex than when the client sends it to the server, but even if it is complex, it is a combination of the five basic types mentioned above.

Single-line string response

127. 0.0.1: 6379 > set x x OK

The above OK is an one-line string response (without double quotes), that is, + OK

Error response

127. 0. 0.1 ERR value is not an integer or out of range 6379 > incr x (error})

Increment a string and the server throws an error reminder

-ERR value is not an integer or out of range

Integer response

127.0.0.1 6379 > incr books (integer} 1

1 is the integer response: 1

Multiline string response

127.0.0.1 6379 > get x "x"

The x quoted in parentheses above is a multi-line string response, that is:

$1x

Array response

127.0.0.1 hset info name bibabo (integer) 1127.0.0.1 hset info age 6379 > hset info sex male (integer) 1127.0.0.1 hset info sex male 6379 > hgetall info1) "name" 2) "bibabo" 3) "age" 4) "18" 5) "sex" 6) "male"

The above hgetall command returns an array, the strings 0, 2, 4 are the key,1 of the hash table, and 3 and 6 are value, and the client is responsible for assembling the array into a dictionary to return.

* 6 $4 name $6 bibabo $3 age $2 18 $3 sex $4 male

Nesting

127.0.0.1 books 6379 > scan 0 1) "0" 2) 1) "info" 2) "books" 3) "author"

The scan command is used to scan all the key lists contained by the server, getting some of them at a time in the form of cursors, which returns a nested array

The first value of the array represents the value of the cursor. If the value is 0, the traversal has been completed. If it is not 0, use this value as the parameter for the next scan.

The second value of the array is another array, which is the list of key.

* 2 $10 billion 3 $4 info$5 books $6 author Thank you for reading this article carefully. I hope the article "how to implement the Communication Protocol RESP in Redis" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report