当前位置:网站首页>Redis introduction complete tutorial: client communication protocol
Redis introduction complete tutorial: client communication protocol
2022-07-04 22:53:00 【Gu Ge academic】
Almost all mainstream programming languages have Redis The client of (http://redis.io/clients),
Don't consider Redis The reason why it is very popular , From the perspective of technology, there are two reasons : The first
One , The communication protocol between client and server is TCP Built on top of the agreement . second ,
Redis Formulated the RESP(REdis Serialization Protocol,Redis Serialization protocol ) Realize customer
The normal interaction between the client and the server , This protocol is simple and efficient , It can be parsed by the machine , It's easy
Recognized by humans . For example, the client sends a message set hello world Give the command to the server , according to RESP
Standards for , The client needs to encapsulate it in the following format ( For each line \r\n Separate ):
*3
$3
SET
$5
hello
$5
world
such Redis The server can follow RESP It is interpreted as set hello world command , After execution
The format of the reply is as follows :
+OK
You can see that in addition to the command (set hello world) And return results (OK) It also contains
Some special characters and numbers , These formats will be described below .
1. Send command format
RESP The format of a command is as follows ,CRLF representative "\r\n".
*< The number of arguments > CRLF
$< Parameters 1 Number of bytes > CRLF
< Parameters 1> CRLF
...
$< Parameters N Number of bytes > CRLF
< Parameters N> CRLF
Still in set hell world This command describes .
The number of parameters is 3 individual , So the first act :
*3
The number of parameter bytes is 355, So the following behaviors :
$3
SET
$5
hello
$5
world
One thing to note is that , The above is just the result of formatting the display , The actual transmission format is as follows
Code , The whole process is as shown in the figure 4-1 Shown :
*3\r\n$3\r\nSET\r\n$5\r\nhello\r\n$5\r\nworld\r\n
2. Return the result format
Redis There are five types of returned results , Pictured 4-2 Shown :
· State the reply : stay RESP The first byte in the is "+".
· Error response : stay RESP The first byte in the is "-".
· Integer reply : stay RESP The first byte in the is ":".
· String reply : stay RESP The first byte in the is "$".
· Multiple string replies : stay RESP The first byte in the is "*".


We know redis-cli Only the final execution result can be seen , That's because redis-cli Itself is
according to RESP For result analysis , So there is no intermediate result ,redis-cli.c The source code ends with the command
The analytic structure of the fruit is as follows :
static sds cliFormatReplyTTY(redisReply *r, char *prefix) {
sds out = sdsempty();
switch (r->type) {
case REDIS_REPLY_ERROR:
// Handle error replies
case REDIS_REPLY_STATUS:
// Process status reply
case REDIS_REPLY_INTEGER:
// Handle integer replies
case REDIS_REPLY_STRING:
// Handle string replies
case REDIS_REPLY_NIL:
// Processing empty
case REDIS_REPLY_ARRAY:
// Handle multiple string replies
return out;
}
For example, to perform set hello world, The return is OK, You can't see the plus sign :
127.0.0.1:6379> set hello world
OK
In order to see Redis Returned by the server “ real ” result , have access to nc command 、telnet life
Make 、 Even write a socket Program simulation . Let's say nc Command to demonstrate , use first
nc127.0.0.16379 Connect to Redis:
nc 127.0.0.1 6379
State the reply :set hello world The return result of is +OK:
set hello world
+OK
Error response : because sethx This command does not exist , So the return result is "-" Number plus error
Error message :
sethx
-ERR unknown command 'sethx'
Integer reply : When the execution result of the command is an integer , The result is an integer reply , for example
incr、exists、del、dbsize The returned results are all integers , For example, to perform incr counter Return results
Namely “:” Add an integer :
incr counter
:1
String reply : When the execution result of the command is a string , The return result is string return
complex . for example get、hget The returned results are all strings , for example get hello Result
by “$5\r\nworld\r\n”:
get hello
$5
world
Multiple string replies : When the execution result of the command is multiple strings , The return result is more
String reply . for example mget、hgetall、lrange The command will return multiple results , For example, the following
operation :
use first mset Set multiple key value pairs :
mset java jedis python redis-py
+OK
And then execute mget The command returns multiple results , first *2 Represents the number of returned results , Back
The format of is consistent with the string reply :
mget java python
*2
$5
jedis
$8
redis-py
There is one caveat , Whether it's a string reply or multiple string replies , If there is nil
value , So it will return $-1.
for example , Execute on a nonexistent key get operation , The return result is :
get not_exist_key
$-1
If a batch operation contains one item, it is nil The result of the value , So the return result is as follows :
mget hello not_exist_key java
*3
$5
world
$-1
$5
jedis
With RESP The protocol format of sending command and returning result provided , Various programming languages can
To use it to achieve the corresponding Redis client , The following two sections will introduce Java and Python Two programming
Linguistic Redis client .
边栏推荐
- 攻防世界 MISC 进阶区 Ditf
- Analysis of environmental encryption technology
- Introducing QA into the software development lifecycle is the best practice that engineers should follow
- Redis入门完整教程:键管理
- Attack and defense world misc advanced area Hong
- Redis入门完整教程:初识Redis
- Sword finger offer 68 - ii The nearest common ancestor of binary tree
- 蓝队攻防演练中的三段作战
- 安装人大金仓数据库
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
猜你喜欢

Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)

Redis démarrer le tutoriel complet: Pipeline

Redis入门完整教程:Bitmaps

Redis入门完整教程:GEO

【室友用一局王者荣耀的时间学会了用BI报表数据处理】

Attack and defense world misc advanced area ditf

Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf

Logo Camp d'entraînement section 3 techniques créatives initiales

堆排序代码详解

Talk about Middleware
随机推荐
关于栈区、堆区、全局区、文字常量区、程序代码区
Is Huatai Securities a nationally recognized securities firm? Is it safe to open an account?
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
繁华落尽、物是人非:个人站长该何去何从
Redis入门完整教程:有序集合详解
leetcode 72. Edit distance edit distance (medium)
It is said that software testing is very simple, but why are there so many dissuasions?
Redis introduction complete tutorial: slow query analysis
Talk about Middleware
SPSS安装激活教程(包含网盘链接)
The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
Google collab trample pit
Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
[roommate learned to use Bi report data processing in the time of King glory in one game]
Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
Redis入门完整教程:GEO
sobel过滤器
[cooking record] - stir fried 1000 pieces of green pepper
攻防世界 misc 进阶区 2017_Dating_in_Singapore
Advanced area a of attack and defense world misc Masters_ good_ idea