在官方网站http://redis.io/topics/protocol我们必须redis通信协议做说明。

根据以下某些原因。我想解决redis client protocol:

1、足够了解通信协议。有助于做出更好的系统设计。

2、学习RESP的设计思想,不仅能扩展我的思维,或许将来能应用于我的代码中。

3、由于有些人想将redis client直接并入自己已有的系统中;包含我在内。这个将在我以后的文章再做说明。

以下我翻译一下http://redis.io/topics/protocol一些我觉得重要的内容:

Redis clients communicate with the Redis server using a protocol called RESP (REdis Serialization Protocol). While the protocol was designed specifically for Redis, it can be used for other client-server software projects.

RESP is a compromise between the following things:

  • Simple to implement.
  • Fast to parse.
  • Human readable.

RESP can serialize different data types like integers, strings, arrays. There is also a specific type for errors. Requests are sent from the client to the Redis server as arrays of strings representing the arguments of the command to execute. Redis replies
with a command-specific data type.

RESP is binary-safe and does not require processing of bulk data transferred from one process to another, because it uses prefixed-length to transfer bulk data.

Note: the protocol outlined here is only used for client-server communication. Redis Cluster uses a different binary protocol in order to exchange messages between nodes.

译:

redisclient和redis服务端用一种名叫RESP(REdis Serialization Protocol)的协议通信。尽管这样的协议专门为redis设计,可是它能被用于其他基于C/S模型的软件项目中。

RESP是基于以下一些事实的一种折衷方案:

  • 易于实现
  • 高速解释
  • 人类可读

RESP能序列化不同的数据类型。比如整型,字符串,数组,还有专门的错误类型。

client发送字符串数组请求到服务端。而字符串数组表示命令參数去运行。Redis会用专门的命令类型回复。

RESP是二进制安全的,同一时候过程转换中不须要大量的数据处理,由于它使用了前缀长度去转换批量数据。

注意:在这里概述的协议仅仅用于client-服务端通信。而Redis集群为了不同节点交换消息使用了一种不同的二进制协议。

RESP is actually a serialization protocol that supports the following data types: Simple Strings, Errors, Integers, Bulk Strings and Arrays.

The way RESP is used in Redis as a request-response protocol is the following:

  • Clients send commands to a Redis server as a RESP Array of Bulk Strings.
  • The server replies with one of the RESP types according to the command implementation.

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 "*"

Additionally RESP is able to represent a Null value using a special variation of Bulk Strings or Array as specified later.

In RESP different parts of the protocol are always terminated with "\r\n" (CRLF).

译:

RESP实际上是一种支持以下数据类型的序列化协议:短字符串。错误。整数,长字符串和数组。

RESP作为一种请求-回应协议。在Redis中的用法例如以下:

  • client发送一种宛如RESP中长字符串数组的命令到Redis服务端。

  • Redis服务端依据命令实现回复当中一种RESP类型。

在RESP中,一种数据类型基于第一个字节:

  • 对于短字符串,回复的第一个字节是"+"
  • 对于错误,回复的第一个字节是"-"
  • 对于整数,回复的第一个字节是":"
  • 对于长字符串,回复的第一个字节是"$"
  • 对于数组,回复的第一个字节是"*"

另外RESP能用指定的长字符串或数组的特殊变量来表示空值。

在RESP中,协议的不同部分总是以"\r\n"(CRLF)作为结束。

前面说到的协议,我有强调了是client,就是说server回复client请求时用到的协议;client请求server时,仅仅须要在命令后面加上"\r\n"。

以下是5种类型的返回实例:

如果在redis server中存在下面键值对:
name1 cat
age1 10 短字符串
"set name2 fish\r\n"
"+OK\r\n" 错误
"seet name3 dog\r\n"
"-ERR unknown command 'seet'\r\n" 整数
"incr age1\r\n"
":11\r\n" 长字符串

"get name1\r\n"
"$3\r\ncat\r\n"

"get name3\r\n"
"$-1\r\n" 数组

"mget name1 age1\r\n"
"*2\r\n$3\r\ncat\r\n$2\r\n11\r\n"

"mget name2 age2\r\n"
"*2\r\n$4\r\nfish\r\n$-1\r\n"

其他情况会返回"*-1\r\n"和"*0\r\n",详细参考redis官方文件;

版权声明:本文博主原创文章,博客,未经同意不得转载。

redis client protocol 分解的更多相关文章

  1. 深入浅出 Redis client/server交互流程

    综述 最近笔者阅读并研究redis源码,在redis客户端与服务器端交互这个内容点上,需要参考网上一些文章,但是遗憾的是发现大部分文章都断断续续的非系统性的,不能给读者此交互流程的整体把握.所以这里我 ...

  2. 【轮子狂魔】手把手教你自造Redis Client

    为什么做Redis Client? Redis Client顾名思义,redis的客户端,主要是封装了一些对于Redis的操作. 而目前用的比较广泛的 ServiceStack.Redis 不学好,居 ...

  3. 教你写个简单到的 Redis Client 框架 - .NET Core

    目录 1,关于 Redis RESP 定义数据类型 2,定义异步消息状态机 3,定义命令发送模板 4,定义 Redis Client 5,实现简单的 RESP 解析 6,实现命令发送客户端 7,如何使 ...

  4. Redis 详解 (一) StackExchange.Redis Client

    这期我们来看StackExchange.Redis,这是redis 的.net客户端之一.Redis是一个开源的内存数据存储,可以用来做数据库,缓存或者消息代理服务.目前有不少人在使用ServiceS ...

  5. StackExchange.Redis Client

    StackExchange.Redis Client 这期我们来看StackExchange.Redis,这是redis 的.net客户端之一.Redis是一个开源的内存数据存储,可以用来做数据库,缓 ...

  6. Redis client Python usage

    http://www.yiibai.com/redis/redis_sorted_sets.html mport redis r_server = redis.Redis('localhost') # ...

  7. 咏南中间件开始支持redis client接口调用

    咏南中间件开始支持redis client接口调用 咏南中间件封装了redis client接口,可以支持REDIS了. 如下图,将数据集写入REDIS缓存,和从REDIS缓存获取数据: proced ...

  8. redis集群错误解决:/usr/lib/ruby/gems/1.8/gems/redis-3.0.0/lib/redis/client.rb:79:in `call': ERR Slot 15495 is already busy (Redis::CommandError)

    错误信息: /usr/lib/ruby/gems/1.8/gems/redis-3.0.0/lib/redis/client.rb:79:in `call': ERR Slot 15495 is al ...

  9. redis client 2.0.0 pipeline 的list的rpop bug

    描写叙述: redis client 2.0.0 pipeline 的list的rpop 存在严重bug,rpop list的时候,假设list已经为空的时候,rpop出来的Response依旧不为n ...

随机推荐

  1. Installing SSL on CentOS | My Virtual Time Capsule

    Installing SSL on CentOS | My Virtual Time Capsule Installing SSL on CentOS Extracted from the Sourc ...

  2. hdu3790最短路径问题 (用优先队列实现的)

    Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的.   Inp ...

  3. hdu5176(并查集)

    传送门:The Experience of Love 题意:一个叫Gorwin的女孩和一个叫Vivin的男孩是一对情侣.他们来到一个叫爱情的国家,这个国家由N个城市组成而且只有N−1条小道(像一棵树) ...

  4. Android-->发送短信页面实现(短信发送以及群发和从电话本中选择联系人)-----------》2

    分析下怎么写 首先,我们需要一个输入框,可以手动的输入手机号码, 其次,很少有人愿意手动输入,那么我们需要提供一个按钮来给我们的用户选择自己电话本中的联系人(一次可以选择多个即群发) 然后,我们需要一 ...

  5. Phaser是一款专门用于桌面及移动HTML5 2D游戏开发的开源免费框架

    Phaser是一款专门用于桌面及移动HTML5 2D游戏开发的开源免费框架,提供JavaScript和TypeScript双重支持,内置游戏对象的物理属性,采用Pixi.js引擎以加快Canvas和W ...

  6. DirectX Sample-ConfigSystem中采用配置文件进行游戏设置

    这个例子是一个撞球系统,碰撞部分也值得学习,不过最重要的还是其配置部分,配置文件为config.txt,其中包括: Requirements:所需配置 propertyset:预定义的设置,通过名字引 ...

  7. Vs2012于Linux应用程序开发(4):公共财产的定义

    在嵌入式开发流程.有些参数基本上不改变,比如编译主机IP,username,password等参数.我们用VS提供的属性管理器来保存这些參数. 打开属性管理器: watermark/2/text/aH ...

  8. 基于HttpClient 4.3的可訪问自签名HTTPS网站的新版工具类

    本文出处:http://blog.csdn.net/chaijunkun/article/details/40145685,转载请注明.因为本人不定期会整理相关博文,会对相应内容作出完好.因此强烈建议 ...

  9. Nagios+pnp4nagios+rrdtool 安装配置为nagios添加自定义插件(三)

    nagios博大精深,可以以shell.perl等语句为nagios写插件,来满足自己监控的需要.本文写mysql中tps.qps的插件,并把收集到的结果以图形形式展现出来,这样输出的结果就有一定的要 ...

  10. 一个使用Java jdk8中Nashorn(Java javascript引擎)设计的Web开发框架

    地址:https://github.com/iboxdb/hijk 採用给框架开发应用,简单直接.开发效率高 下载后 set PATH to /JAVA 8_HOME/bin jjs build.js ...