命令行使用管道(命令以换行符分隔):

(printf "PING\r\nPING\r\nPING\r\n"; sleep 1) | nc localhost 6379

redis server 接收客户端的输入,调用栈如下:

ae.c/aeProcessEvents
networking.c/processInputBuffer

redis 中客户端的结构体:

typedef struct client {
// 输入缓冲区保存客户端发送的命令
sds querybuf;
// 字符串数组,要执行的命令,例如 PING
robj **argv;
// 记录 argv 长度
int argc;
} client;

分析管道命令的执行过程:按换行符 split 命令,分三次执行 PING 命令。

void processInputBuffer(client *c) {
server.current_client = c;
/* Keep processing while there is something in the input buffer */
// querybuf 的初始值是 "PING\r\nPING\r\nPING\r\n"
// 每经过一次 processInlineBuffer(c),减少一个 PING
// "PING\r\nPING\r\nPING\r\n" -> "PING\r\nPING\r\n" -> "PING\r\n" -> ""
while(sdslen(c->querybuf)) {
/* Return if clients are paused. */
if (!(c->flags & CLIENT_SLAVE) && clientsArePaused()) break; /* Immediately abort if the client is in the middle of something. */
if (c->flags & CLIENT_BLOCKED) break; /* CLIENT_CLOSE_AFTER_REPLY closes the connection once the reply is
* written to the client. Make sure to not let the reply grow after
* this flag has been set (i.e. don't process more commands).
*
* The same applies for clients we want to terminate ASAP. */
if (c->flags & (CLIENT_CLOSE_AFTER_REPLY|CLIENT_CLOSE_ASAP)) break; /* Determine request type when unknown. */
// 普通命令以 * 开头,请求类型为 PROTO_REQ_MULTIBULK,管道命令类型为 PROTO_REQ_INLINE
if (!c->reqtype) {
if (c->querybuf[] == '*') {
c->reqtype = PROTO_REQ_MULTIBULK;
} else {
c->reqtype = PROTO_REQ_INLINE;
}
} if (c->reqtype == PROTO_REQ_INLINE) {
if (processInlineBuffer(c) != C_OK) break;
} else if (c->reqtype == PROTO_REQ_MULTIBULK) {
if (processMultibulkBuffer(c) != C_OK) break;
} else {
serverPanic("Unknown request type");
} /* Multibulk processing could see a <= 0 length. */
if (c->argc == ) {
resetClient(c);
} else {
/* Only reset the client when the command was executed. */
if (processCommand(c) == C_OK)
resetClient(c);
/* freeMemoryIfNeeded may flush slave output buffers. This may result
* into a slave, that may be the active client, to be freed. */
if (server.current_client == NULL) break;
}
}
server.current_client = NULL;
}

执行具体命令:

sever.c/processCommand
sever.c/call

redis 管道原理的更多相关文章

  1. Redis管道理解

    Redis管道理解 简介 管道并不是Redis本身提供的功能,通常是客户端提供的功能: 管道就是打包多条无关命令批量执行,以减少多个命令分别执行消耗的网络交互时间(TCP网络交互),可以显著提升Red ...

  2. 使用Redis管道提升性能

    首发于 樊浩柏科学院 Redis 的 管道 (pipelining)是用来打包多条无关命令批量执行,以减少多个命令分别执行带来的网络交互时间.在一些批量操作数据的场景,使用管道可以显著提升 Redis ...

  3. redis管道操作(事务),无回滚

    管道:将数据操作放在内存中,只有成功后,才会一次性全部放入redis #管道(事务),要是都成功则成功,失败一个全部失败 #原理:将数据操作放在内存中,只有成功后,才会一次性全部放入redis pip ...

  4. Redis 管道技术

    Redis是一种基于客户端-服务端模型以及请求/响应协议的TCP服务.这意味着通常情况下一个请求会遵循以下步骤: 客户端向服务端发送一个查询请求,并监听Socket返回,通常是以阻塞模式,等待服务端响 ...

  5. Redis事务原理分析

    Redis事务原理分析 基本应用 在Redis的事务里面,采用的是乐观锁,主要是为了提高性能,减少客户端的等待.由几个命令构成:WATCH, UNWATCH, MULTI, EXEC, DISCARD ...

  6. 全面剖析Redis Cluster原理和应用

    全面剖析Redis Cluster原理和应用 1.Redis Cluster总览 1.1 设计原则和初衷 在官方文档Cluster Spec中,作者详细介绍了Redis集群为什么要设计成现在的样子.最 ...

  7. 一、Redis事务原理分析

    一.Redis事务原理分析 在Redis的事务里面,采用的是乐观锁,主要是为了提高性能,减少客户端的等待.由几个命令构成:WATCH, UNWATCH, MULTI, EXEC, DISCARD.通过 ...

  8. Redis管道功能

    Redis管道,Redis存储用户浏览数据 当频繁的存储获取Redis数据库中的数据时,可以使用Redis的pipeline(管道)功能,将多个相互没有依赖关系的读写操作,如:下一步执行的Redis操 ...

  9. Redis核心原理

    Redis系统介绍: Redis的基础介绍与安装使用步骤:https://www.jianshu.com/p/2a23257af57b Redis的基础数据结构与使用:https://www.jian ...

随机推荐

  1. 数据库无法打开到SQL Server连接

    今天打开数据库,发现连接不上,弹出错误提示: 打开SQLServer Configuration Manager,发现SQL Server状态已经停止,双击启动弹出错误提示: 打开SQL Server ...

  2. 【二十九】php之简易微信二维码支付

    参考二维码支付接口文档:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_5 index.php <!DOCTYPE htm ...

  3. Ubuntu 下 Python自由切换

    sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo update-alternati ...

  4. hdu-5707-Combine String

    题意:给你三个字符串,让你计算1 2 串和3 串是否匹配,就是3串可以分解为 1  2 串,字母顺序必须是按照1 2 串的字母前后顺序. DP代码太深奥 看不太透,这个代码比较好理解一点: #incl ...

  5. Qt5鼠标事件及实例

    mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLa ...

  6. 修改记录-优化后(springboot+shiro+session+redis+ngnix共享)

    1.普通用户实现redis共享session 1.配置 #cache指定缓存类型 spring.cache.type=REDIS #data-redis spring.redis.database=1 ...

  7. Within K stops 最短路径 Cheapest Flights Within K Stops

    2018-09-19 22:34:28 问题描述: 问题求解: 本题是典型的最短路径的扩展题,可以使用Bellman Ford算法进行求解,需要注意的是在Bellman Ford算法的时候需要额外申请 ...

  8. MyBatis-session-SqlSession

    The SqlSession instance is the most powerful in MyBatis. It is where you'll find all of the methods ...

  9. pointer-events

    在做移动端的页面时,经常会遇到点击(touch)一个弹出的层,在上面触发点击(touch)事件,当弹出层关闭之后点击(touch)事件会穿透到下面的层,这时候如果下一层的某个元素也绑定了点击(touc ...

  10. English trip V1 - B 20. Likes and Dislikes 喜欢和不喜欢 Teacher:Sole Key:

    In this lesson you will learn to talk about likes and dislikes. 课上内容(Lesson) # talk about hobby Do y ...