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 ...
随机推荐
- Extjs3 combobox使用
Combobox 在程序中应用十分普遍,每个combobox的选项 一般对应两个值:一个用于前台显示的值,一个与显示值对应的value值.在后台获取value的值需使用combobox的 Hidden ...
- UIButton+Block
UIButton的一个Category,使用block处理UIControlEvent事件,如常用的TouchUpInside等.代码非原创,也是从网上看到的,用到了实际项目中,目前还没发现什么问题. ...
- Gson 解析多层嵌套JSON数据
http://stackoverflow.com/questions/14139437/java-type-generic-as-argument-for-gson
- mongoose连接数据库的两种形式
不废话,直接 ---------------------- .如果你的应用程序只使用一个数据库, 应该使用 mongoose.connect. 如果您需要创建额外的连接,使用 mongoose.cre ...
- 我的Android进阶之旅------>关于使用Android Studio替换App的launcher图标之后仍然显示默认的ic_launcher图标的解决方法
前言 最近做了一个App,之前开发该App的时候一直以来都是默认的launcher图标启动的, 今天美工换了一个App的launcher 图标,因此在Android Studio中将默认的lanche ...
- hdu 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- PHP memcache的使用教程
(结尾附:完整版资源下载) 首先,为什么要用memcached?如果你看过InnoDB的一些书籍,你应该知道在存储引擎那一层是由一个内存池的.而在内存池中 又有一个缓冲池.而缓冲池就会缓冲查找的数据, ...
- mysql与sql server参照对比学习mysql
mysql与sql server参照对比学习mysql 关键词:mysql语法.mysql基础 转自桦仔系列:http://www.cnblogs.com/lyhabc/p/3691555.html ...
- (4.8)SET ANSI_NULLS ON、SET QUOTED_IDENTIFIER ON
T-SQL支持在与空值进行比较时,允许比较运算符返回 TRUE 或 FALSE. 通过设置 ANSI_NULLS OFF 可将此选项激活.当 ANSI_NULLS 为 OFF 时,如果 ColumnA ...
- An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.
与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provider: TCP ...