慕课网-一站式学习Redis从入门到高可用分布式实践-第4章-瑞士军刀Redis其他功能-pipeline
pipeline
1.1次网络命令通信模型

2.批量网络命令通信模型

3.什么是流水线

4.流水线的作用

流水线两点注意
1)Redis 的命令时间是微秒级别
2)pipeline 每次条数要控制(网络)
实例,从北京到上海

5.pipeline-Jedis 实现
引入 maven 依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
代码演示
#没有 pipeline,1w hset 需要 50s
Jedis jedis = new Jedis("127.0.0.1", 6379);
for (int i = 0; i < 10000; i++) {
jedis.hset("hashkey:" + i, "field" + i, "value" + i);
}
#使用 pipeline,1w hset 需要 0.7s
Jedis jedis = new Jedis("127.0.0.1", 6379);
for (int i = 0; i < 100; i++) {
Pipeline pipeline = jedis.pipelined();
for (int j = i * 100; j < (i + 1) * 100; j++) {
jedis.hset("hashkey:" + j, "field" + j, "value" + j);
}
pipeline.syncAndReturnAll();
}
6.与原生 M 的操作?


7.使用建议
1)注意每次pipeline携带数据量
2)pipeline每次只能作用在一个Redis节点上
3)M操作与pipeline区别
posted on 2019-12-05 11:30 herisson_pan 阅读(12) 评论(0) 收藏 举报
浙公网安备 33010602011771号