Jedis(Java+Redis) Pool的使用
今天试了一下Jedis里连接池JedisPool的的使用。代码如下:
package com.myapp.jedis.pooldemo; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; /**
* Created by baidu on 16/10/18.
*/
public class TestPool {
private static JedisPool pool = null; public static JedisPool getPool() {
if (pool == null) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(500);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000*10);
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
config.setTestOnBorrow(true);
//new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
pool = new JedisPool(config, "[ip]", 8379, 10000, "[auth]"); }
return pool;
} public synchronized static Jedis getResource() {
if (pool == null) {
pool = getPool();
}
return pool.getResource();
} // 返还到连接池
// Deprecated
// 换成用完之后, redis.close()
/*
public static void returnResource(Jedis redis) {
if (redis != null) {
pool.returnResource(redis);
}
}
*/ public static void main(String[] args) {
Jedis redis = null;
int loop = 1;
while (loop < 20) {
try {
long start = System.currentTimeMillis();
redis = getResource();
redis.set("k1", "v1");
String ret = redis.get("k1");
long end = System.currentTimeMillis();
System.out.printf("Get ret from redis: %s with %d millis\n", ret, end-start);
} finally {
if (redis != null) {
redis.close();
}
}
loop++;
}
} }
其中,有个函数returnResource已经deprecated了,现在Jedis的close方法重写了,用Jedis.close来释放资源。
跑了20次,运行结果如下:
Get ret from redis: v1 with millis
Get ret from redis: v1 with millis
Get ret from redis: v1 with 225 millis
Get ret from redis: v1 with 214 millis
Get ret from redis: v1 with 210 millis
Get ret from redis: v1 with 232 millis
Get ret from redis: v1 with 209 millis
Get ret from redis: v1 with 211 millis
Get ret from redis: v1 with 239 millis
Get ret from redis: v1 with 207 millis
Get ret from redis: v1 with 215 millis
Get ret from redis: v1 with 223 millis
Get ret from redis: v1 with 291 millis
Get ret from redis: v1 with 220 millis
Get ret from redis: v1 with 214 millis
Get ret from redis: v1 with 219 millis
Get ret from redis: v1 with 257 millis
Get ret from redis: v1 with 214 millis
Get ret from redis: v1 with 211 millis Process finished with exit code 0
可以看出,第一次500多毫秒,之后都是200多毫秒,速度有提高。
Jedis(Java+Redis) Pool的使用的更多相关文章
- Java中使用Jedis操作Redis(转载)
整理 1.字符串 添加:set keyname value 查询:get keyname 拼接:append keyname value 删除:del keyname 添加多个: mset keyna ...
- (转)Java中使用Jedis操作Redis
转自http://www.cnblogs.com/liuling/p/2014-4-19-04.html 使用Java操作Redis需要jedis-2.1.0.jar,下载地址:http://file ...
- Java中使用Jedis操作Redis
使用Java操作Redis需要jedis-2.1.0.jar,下载地址:http://files.cnblogs.com/liuling/jedis-2.1.0.jar.zip 如果需要使用Redis ...
- Java通过jedis操作redis缓存
package com.wodexiangce.util; import java.util.Set; import redis.clients.jedis.Jedis; /** * redis工具类 ...
- java客户端Jedis操作Redis Sentinel 连接池
pom配置: <dependency> <groupId>org.springframework.data</groupId> <artifactId> ...
- spring 集成redis客户端jedis(java)
spring集成jedis简单实例 jedis是redis的java客户端,spring将redis连接池作为一个bean配置. “redis.clients.jedis.JedisPool”,这 ...
- Java使用Jedis操作Redis大全
Java操作Redis需要导入两个jar: commons-pool2-2.4.2.jar jedis-2.1.0.jar package com.chinasofti.test; import ja ...
- Redis】Java中使用Jedis操作Redis(Maven导入包)、创建Redis连接池
如果我们使用Java操作Redis, 需要确保已经安装了 redis 服务及 Java redis 驱动. Maven项目可以直接在pom.xml中加入jedis包驱动: <!-- https: ...
- Java中使用Jedis操作Redis之二
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.J ...
随机推荐
- PHP HERE DOCUMENT
转自: http://www.codeweblog.com/php%E4%B8%ADheredoc%E7%9A%84%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95/ Here ...
- Oracle exp
--导出表exp userid=hr/oracle123 tables=employees direct=y file=/u01/employees.dmp log=/u01/employees.lo ...
- 层级数据模板 案例(HierarchicalDataTemplateWindow)
1.xaml 文件 <Window x:Class="DataTemplate.HierarchicalDataTemplateWindow" xmlns=&q ...
- 串口通讯超时的设置与含义(COMMTIMEOUTS)
COMMTIMEOUTS:COMMTIMEOUTS主要用于串口超时参数设置.COMMTIMEOUTS结构如下: typedef struct _COMMTIMEOUTS { DWORD ReadInt ...
- libev 学习使用
libev 简单的I/O库. a high performance full featured event loop written in c libev 的大小也比 libevent 小得多并且自 ...
- 关于因各种原因,造成LINUX主机不能通过域名访问自己的解决办法
因路由黑洞或其它原因造成自己域名或外网IP的解决办法: vi /etc/hosts 添加一行: 10.28.86.250 www.edusoa.com 保存退出.
- (十四)基于GTID的主从复制
(1)GTID主从复制 1)环境介绍 /etc/redhat-release CentOS Linux release 7.3.1611 (Core) MySQL版本:5.7 mysql> se ...
- POJ 2236 Wireless Network [并查集+几何坐标 ]
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...
- 拓扑排序(Topological Order)UVa10305 Ordering Tasks
2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意 ...
- AMQ学习笔记 - 04. 消息选择器
概述 消息选择器使用类似于SQL语法,为Consumer指定基于Message属性的筛选条件. 消息选择器 发送的时候,给消息添加一些属性:在接收的时候,根据属性进行过滤. API javax.jms ...