jedis使用管道(pipeline)对redis进行读写(使用hmset、hgetall测试)
一般情况下,Redis Client端发出一个请求后,通常会阻塞并等待Redis服务端处理,Redis服务端处理完后请求命令后会将结果通过响应报文返回给Client。这有点类似于HBase的Scan,通常是Client端获取每一条记录都是一次RPC调用服务端。在Redis中,有没有类似HBase Scanner Caching的东西呢,一次请求,返回多条记录呢?有,这就是Pipline。官方介绍 http://redis.io/topics/pipelining。
通过pipeline方式当有大批量的操作时候,我们可以节省很多原来浪费在网络延迟的时间,需要注意到是用pipeline方式打包命令发送,redis必须在处理完所有命令前先缓存起所有命令的处理结果。打包的命令越多,缓存消耗内存也越多。所以并不是打包的命令越多越好。
使用Pipeline在对Redis批量读写的时候,性能上有非常大的提升。
使用Java测试了一下:
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response; public class Test {
public static void main(String[] args) throws Exception {
Jedis redis = new Jedis("127.0.0.1", 6379, 400000);
Map<String, String> data = new HashMap<String, String>();
redis.select(8);
redis.flushDB();
// hmset
long start = System.currentTimeMillis();
// 直接hmset
for (int i = 0; i < 10000; i++) {
data.clear();
data.put("k_" + i, "v_" + i);
redis.hmset("key_" + i, data);
}
long end = System.currentTimeMillis();
System.out.println("dbsize:[" + redis.dbSize() + "] .. ");
System.out.println("hmset without pipeline used [" + (end-start)/1000 + "] seconds ..");
redis.select(8);
redis.flushDB();
// 使用pipeline hmset
Pipeline p = redis.pipelined();
start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
data.clear();
data.put("k_" + i, "v_" + i);
p.hmset("key_" + i, data);
}
p.sync();
end = System.currentTimeMillis();
System.out.println("dbsize:[" + redis.dbSize() + "] .. ");
System.out.println("hmset with pipeline used [" + (end-start)/1000 + "] seconds ..");
// hmget
Set keys = redis.keys("*");
// 直接使用Jedis hgetall
start = System.currentTimeMillis();
Map<String, Map<String, String>> result = new HashMap<String, Map<String, String>>();
for (String key : keys) {
result.put(key, redis.hgetAll(key));
}
end = System.currentTimeMillis();
System.out.println("result size:[" + result.size() + "] ..");
System.out.println("hgetAll without pipeline used [" + (end-start)/1000 + "] seconds ..");
// 使用pipeline hgetall
Map<String, Response<Map<String, String>>> responses =
new HashMap<String, Response<Map<String, String>>>(
keys.size());
result.clear();
start = System.currentTimeMillis();
for (String key : keys) {
responses.put(key, p.hgetAll(key));
}
p.sync();
for (String k : responses.keySet()) {
result.put(k, responses.get(k).get());
}
end = System.currentTimeMillis();
System.out.println("result size:[" + result.size() + "] ..");
System.out.println("hgetAll with pipeline used [" + (end-start)/1000 + "] seconds ..");
redis.disconnect();
}
}
//测试结果:
//使用pipeline来批量读写10000条记录,就是小菜一碟,秒完。
dbsize:[10000] ..
hmset without pipeline used [243] seconds ..
dbsize:[10000] ..
hmset with pipeline used [0] seconds ..
result size:[10000] ..
hgetAll without pipeline used [243] seconds ..
result size:[10000] ..
hgetAll with pipeline used [0] seconds ..
//测试结果2 (外网)
dbsize:[10000] ..
hmset without pipeline used [653] seconds ..
dbsize:[10000] ..
hmset with pipeline used [1] seconds ..
result size:[10000] ..
hgetAll without pipeline used [680] seconds ..
result size:[10000] ..
hgetAll with pipeline used [1] seconds ..
jedis使用管道(pipeline)对redis进行读写(使用hmset、hgetall测试)的更多相关文章
- Java Redis的Pipeline管道,批量操作,节省大量网络往返时间 & Redis批量读写(hmset&hgetall) 使用Pipeline
一般情况下,大家使用redis去put/get都是先拿到一个jedis实例,然后操作,然后释放连接:这种模式是 请求-响应,请求-响应 这种模式,下一次请求必须得等第一次请求响应回来之后才可以,因为r ...
- Redis 管道pipeline
Redis是一个cs模式的tcp server,使用和http类似的请求响应协议. 一个client可以通过一个socket连接发起多个请求命令. 每个请求命令发出后client通常会阻塞并等待red ...
- redis管道pipeline
Jedis jedis = new Jedis("127.0.0.1",6379); Pipeline pipeline = jedis.pipelined(); for(int ...
- Jedis使用管道优化批量输出插入的效率
Jedis连接池: package com.daxin.jedis_datastructure; /** * * @author daxin * * @email leodaxin@163com * ...
- JedisCluster使用pipeline操作Redis Cluster最详细从0到1实现过程
公众号文章链接:https://mp.weixin.qq.com/s/6fMsG009RukLW954UUndbw 前言 2020年4月30日,Redis 6.0.0正式发布,标志着redis从此告别 ...
- [Linux] 流 ( Stream )、管道 ( Pipeline ) 、Filter - 笔记
流 ( Stream ) 1. 流,是指可使用的数据元素一个序列. 2. 流,可以想象为是传送带上等待加工处理的物品,也可以想象为工厂流水线上的物品. 3. 流,可以是无限的数据. 4. 有一种功能, ...
- Android OpenGL ES(二)OpenGL ES管道(Pipeline) .
大部分图形系统都可以比作工厂中的装配线(Assemble line)或者称为管道(Pipeline).前一道的输出作为下道工序的输入.主CPU发出一个绘图指令,然后可能由硬件部件完成坐标变换,裁剪,添 ...
- Redis 学习笔记3:Jedis 连接虚拟机下的Redis 服务
Jedis 是 Redis 官方首选的 Java 客户端开发包. 虚拟机的IP地址是192.168.8.88. Jedis代码是放在windows上的,启动虚拟机上的Redis服务之后,用Jedis连 ...
- redis使用问题一:Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool] with root cause
本文使用的是spring-data-redis 首先说下redis最简单得使用,除去配置. 需要在你要使用得缓存得地方,例如mybatis在mapper.xml中加入: <cache evict ...
随机推荐
- Centos7 下安装mysql数据库
centos7系统,安装mysql发现已经默认的是mariadb. 只能安装mariadb,mariadb是mysql一个分支,对mysql完全支持 1 安装 yum -y install maria ...
- DOM 综合练习(一)
// 练习一: 完成一个好友列表的展开闭合效果 <html> <head> <style type="text/css"> // 对表格中的 u ...
- django之多表查询与创建
https://www.cnblogs.com/liuqingzheng/articles/9499252.html # 一对多新增数据 添加一本北京出版社出版的书 第一种方式 ret=Book.ob ...
- unity3d相关资源
http://pan.baidu.com/s/1kTG9DVD GUI源码
- Mac 启动和关闭rabbitmq
1.安装 brew install rabbitmq 2.启动及关闭RabbitMQ服务 前台启动 sudo ./rabbitmq-server 或 sudo su/usr/local/Cell ...
- Redis二(Hash操作)
Hash操作 Hash操作,redis中Hash在内存中的存储格式如下图: hset(name, key, value) 1 2 3 4 5 6 7 8 9 # name对应的hash中设置一个键值对 ...
- python1变量,表达式和语句
1.变量和类型 变量是指向各种类型值的名字,以后再用到某个值时,直接引用这个名字即可,不用再写具体的值,在python中,变量的使用环境非常宽松,没有明显的变量声明,而且类型不是固定的.如果你不能确定 ...
- 多线程并发练习(Day35)
练习一 #_*_coding:utf-8_*_ #!/usr/bin/env python import multiprocessing import threading import socket ...
- JavaScript 函数,math对象,Date对象 序列化 总结
函数 函数定义 // 普通函数定义 function f1() { console.log("Hello world!"); } // 带参数的函数 function f2(a, ...
- Delphi 正则表达式语法(1): 关于大小写与中文
Delphi 正则表达式语法(1): 关于大小写与中文 //替换一般字符串 var reg: TPerlRegEx; begin reg := TPerlRegEx.Create(nil); ...