redis通信协议

时间:2020-03-24 15:36:49   收藏:0   阅读:45

标准文档:https://redis.io/topics/protocol

优点:简单易实现,高速解析,可读性强

请求-响应模型

客户端一组参数组成的指令,服务端接受后,处理并返回结果。
支持流水线式操作:客户端发送多个指令,统一处理完毕后返回结果
支持订阅/发布模式,当客户端订阅时,服务端会在客户端push后自动返回消息。(不是很懂。。。)

RESP协议描述

支持数据类型:Simple Strings, Errors, Integers, Bulk Strings and Arrays.
请求-响应处理过程:
客户端发送指令,指令用字符串形式的RESP数组(RESP Array of Bulk Strings)表示。
服务端根据指令执行结果,返回处理结果RESP类型。

In RESP, 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 Bulk Strings the first byte of the reply is "$"
For Arrays the first byte of the reply is "*"
In RESP different parts of the protocol are always terminated with "\r\n" (CRLF).

协议中使用CRLF来区分各个部分。协议形如:

*<参数数量> CR LF
$<参数 1 的字节数量> CR LF
<参数 1 的数据> CR LF
...
$<参数 N 的字节数量> CR LF
<参数 N 的数据> CR LF

RESP Simple Strings

表示返回成功
"+OK\r\n"

RESP Errors

表示返回错误
"-Error message\r\n"

RESP Integers

For example ":0\r\n", or ":1000\r\n" are integer replies.
返回int的指令: SETNX, DEL, EXISTS, INCR, INCRBY, DECR, DECRBY, DBSIZE, LASTSAVE, RENAMENX, MOVE, LLEN, SADD, SREM, SISMEMBER, SCARD.

RESP Bulk Strings

"$6\r\nfoobar\r\n"
最大为512M,结构如下:

其中"$-1\r\n"表示空。

RESP Arrays

*5\r\n
:1\r\n
:2\r\n
:3\r\n
:4\r\n
$6\r\n
foobar\r\n

结构如下:

Null elements in Arrays

用于表示空元素
$-1\r\n

原文:https://www.cnblogs.com/omg-two/p/12558282.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!