今天试了一下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的使用的更多相关文章

  1. Java中使用Jedis操作Redis(转载)

    整理 1.字符串 添加:set keyname value 查询:get keyname 拼接:append keyname value 删除:del keyname 添加多个: mset keyna ...

  2. (转)Java中使用Jedis操作Redis

    转自http://www.cnblogs.com/liuling/p/2014-4-19-04.html 使用Java操作Redis需要jedis-2.1.0.jar,下载地址:http://file ...

  3. Java中使用Jedis操作Redis

    使用Java操作Redis需要jedis-2.1.0.jar,下载地址:http://files.cnblogs.com/liuling/jedis-2.1.0.jar.zip 如果需要使用Redis ...

  4. Java通过jedis操作redis缓存

    package com.wodexiangce.util; import java.util.Set; import redis.clients.jedis.Jedis; /** * redis工具类 ...

  5. java客户端Jedis操作Redis Sentinel 连接池

    pom配置: <dependency> <groupId>org.springframework.data</groupId> <artifactId> ...

  6. spring 集成redis客户端jedis(java)

    spring集成jedis简单实例   jedis是redis的java客户端,spring将redis连接池作为一个bean配置. “redis.clients.jedis.JedisPool”,这 ...

  7. Java使用Jedis操作Redis大全

    Java操作Redis需要导入两个jar: commons-pool2-2.4.2.jar jedis-2.1.0.jar package com.chinasofti.test; import ja ...

  8. Redis】Java中使用Jedis操作Redis(Maven导入包)、创建Redis连接池

    如果我们使用Java操作Redis, 需要确保已经安装了 redis 服务及 Java redis 驱动. Maven项目可以直接在pom.xml中加入jedis包驱动: <!-- https: ...

  9. Java中使用Jedis操作Redis之二

    import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.J ...

随机推荐

  1. python3 面向对象、类、继承、组合、派生、接口、子类重用父类方法

    对象是特征(变量)与技能(函数)的结合体而类是一系列对象共同的特征与技能的集合体 class teacher: lesson = "python" def __init__(sel ...

  2. 转: Photoshop cs6 快捷键命令大全

    转自: http://www.cnblogs.com/zhen656/p/4249759.html 工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取) 矩形.椭圆选框工具.单行单列 ...

  3. SpringMvc基础知识(一)

    目录: springmvc框架原理(掌握) 前端控制器.处理器映射器.处理器适配器.视图解析器 springmvc入门程序 目的:对前端控制器.处理器映射器.处理器适配器.视图解析器学习 非注解的处理 ...

  4. kthread_create与kernel_thread的区别【栈】

    转自:http://blog.chinaunix.net/uid-25513153-id-2888903.html kthread_create与kernel_thread的区别 kernel thr ...

  5. 程序异常退出 却没有产生core文件

    程序异常退出  却没有产生core文件 http://www.cnblogs.com/my_life/articles/4107333.html

  6. (一)安装openvpn服务器端

    环境 centos版本 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) 关闭cento ...

  7. 使用threadpool并发测试,报错HTTPConnectionPool Max retires exceeded

    解决方法:和以下答案一致 https://blog.csdn.net/qq_21405949/article/details/79363084 场景: 在做爬虫项目或者是在发送网络请求的时候,一般都会 ...

  8. linux:/lib/libc.so.6: version `glibc_2.7′ not found【没有解决】采用新方法达到目的

    1 下载glibc wget http://ftp.gnu.org/pub/gnu/glibc/glibc-2.7.tar.gz 2. tar zxf glibc-2.7.tar.gz 3. cd g ...

  9. cogs 双服务点设置

    4. 双服务点设置 ☆   输入文件:djsb.in   输出文件:djsb.out   简单对比时间限制:1 s   内存限制:128 MB 问题描述为了进一步普及九年义务教育,政府要在某乡镇建立两 ...

  10. POJ2342 Anniversary party(动态规划)(树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6635   Accepted: 3827 ...