Home > Database > Redis > body text

Detailed explanation of Redis RESP protocol implementation examples

WBOY
Release: 2022-09-06 17:37:31
forward
1964 people have browsed it

Recommended learning: Redis video tutorial

Review RESP protocol

RESP is a Redis communication protocol based on TCP. The protocol is divided by /r/n (line), and the protocol supports 5 types, the specific information is as follows:

##TypePrefixRemarks Simple string Simple string starts with Error data-Error data starts with -Start with Integer:Integer starts with:Complex string$Complex string starts with $Array*Array starts with *

即,我们向redis发送命令:set name pdudo,其实发送的具体信息是

*3
$3
set
$4
name
$5
pdudo
Copy after login

而服务器返回的信息也是类似的,只不过还需要了解+-,这2个前缀分别代表正确消息和错误的消息。

我们准备2个例子,我们来敲一下

例子1

set name pdudo
Copy after login

例子2

lpush pdudo data1
lpush pdudo data2
lrange pdudo 0 -1
Copy after login

快来动动你的小手指,看能不能根据RESP协议规则,将上述例子命令敲出来。现在你体会到了Redis官网介绍RESP协议时所述的 简单易读 可么?

对于RESP来说,一定要搞清楚协议后,最好能够手写协议去执行,再考虑写程序去实现协议!!!

如何拆解RESP协议

终于到了喜闻乐见的环节了,我们要拆解和组装协议了。 那我们至少来解决如下3个问题:

  • 该协议是基于TCP流的,我们如何判断整个命令什么时候结束?
  • 如何拆解命令?

协议什么时候结束

一般而言,我们自己在使用TCP传输数据,都会在数据开头定义2个或者4个字节,用于存储该数据有多少个字节,这样方便检验接收,类似于这种情况。

RESP有意思了,它是以/r/n来分割的。最前面会以前缀来判断其类型,例如我们发送命令,其会用到的前缀有*以及$,那么我们如何来判断,我们要读取多少个/r/n呢?

因为上述*代表数组,即有多少组数据需要处理,图中为n

$表示复杂字符串,即需要获取m个字符数据,不包含/r/n

如何拆解RESP协议

若要拆解命令,则我们得获取命令,如上图所示,报文$m,其实记录的有m长度的数据(不包含\r\n),所以我们可以这样来写伪代码。

根据如上,我们很容易写出伪代码

func toArgs(rd *bufio.Reader) {
	data , _ , _ := rd.ReadLine()
	switch data[0] {
	case '*':
		n := data[1:] // 循环n次
		for i:=0;i<n;i++ {
			toArgs(rd)
		}
	case &#39;$&#39;:
		m := data[1:] // 获取m个数据
		// 获取m长度的数据即可
	}
}
Copy after login

如上我们先获取前缀为*的,继而获取其值n,我们则循环n次,即可获取该报文的数据。而前缀为$的,我们可以直接获取该m长度的数据即可,这里主要要处理一下\r\n

将命令构建RESP报文规范,根据拆解反操作就可以了,这里暂不介绍了。

上述,我们核心功能已经探讨完毕了。

功能实现

代码已经编写完毕,放置在了gitee上: gitee

如上我们已经学会了如何拆解和组装RESP协议了,我们接着来看,我们如何用go来编写拆解和组装协议的代码呢? 我们可以看。

我们先创建一个字符,然后将其封装为bufio.Reader,我们来看下:

因为我们要使用readLine()函数,所以我们需要将其转换为bufio.Reader类型,若是直接从net.Conn中获取,不用转换,直接可以使用 bufio.Reader的。

我们将上述伪代码编写一下,实现拆解的功能。

其具体执行过程是我们先获取一行数据,放置到data中,而后判断其前缀是什么,若是*则取其后面的数据,将其转为int类型n,而后再递归该函数n次,而后中遇到$,我们则取后面的数据,也是将其转为int类型m,而后再取m长度的实际数据,这就是我们的命令了,最后我们再踢掉命令的\r\n即可。

其中,有一个函数是byteToInt是我们自己写的通过切片转为数字的函数,我们看下

该函数主要的功能是将其[]byte数字转换为int数据。

如上,我们整个RESP协议功能写完了,我们运行下看下实际效果:

Obviously, we successfully disassembled the data.

In this article, we introduce how to use go to simply disassemble the contents of the RESP protocol. Why don’t we introduce how to write redisWhat about master-slave middleware?

At first I planned to write it like this, but with too much knowledge, the introduction will be very complicated and it is difficult to explain one point clearly, so we have chosen a core point to introduce separately. I would like to call it It is called core-oriented programming (my friend told me a long time ago). The so-called simplicity of core-oriented programming means that when we are involved in a function, we must learn to dismantle the function and use the core function first. demo Make it, and then slowly enrich the surroundings to complete the entire requirement package.

Finally, let’s talk about the RESP protocol. The official website summarizes it as simple implementation, quick analysis,direct Can read . If you study these two articles carefully, you will definitely have a deep understanding of this.

Recommended learning: Redis video tutorial

The above is the detailed content of Detailed explanation of Redis RESP protocol implementation examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!