今天试了一下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多毫秒,速度有提高。

最新文章

  1. (转)DOM appendHTML实现及insertAdjacentHTML
  2. ThinkPhp 3.2 数据的连贯操作
  3. 程序设计入门—Java语言 第六周编程题 1 单词长度(4分)
  4. MVC ASP.net流程 源代码分析
  5. 17.C#类型判断和重载决策(九章9.4)
  6. unity3d中Find的用法
  7. bug_ _java.lang.IllegalArgumentException: View not attached to window manager 2
  8. Storm的容错性
  9. POJ2142——The Balance
  10. java中调用js脚本
  11. 10个简单步骤,完全理解SQL
  12. 笔记整理——linux程序设计
  13. H5本地储存Web Storage
  14. Cain工具ARP欺骗攻击
  15. 从零开始学习PYTHON3讲义(十一)计算器升级啦
  16. Oracle中和mysql中函数的区别
  17. asp.net mvc 简单实现一个账号只能在一个地方登录
  18. 配置本地无密码 SSH登录远程服务器
  19. window 系统显示svg、psd格式文件
  20. 给JSON中put的value=null时,这对key=value会被隐藏掉。

热门文章

  1. [bzoj2763][JLOI2011]飞行路线——分层图最短路
  2. linux coredump测试
  3. 【bzoj4282】慎二的随机数列
  4. 在oracle官网上,找到我们所需版本的jdk
  5. mpvue-小程序之蹲坑记
  6. 【SQL】单个表的查询
  7. selenium3+python自动化50-环境搭建(firefox)【转载】
  8. django 分页django-pure-pagination(zz)
  9. VS2010编写C++程序出现error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include &quot;StdAfx.h&quot;”?
  10. java 8中撤销永久代,引入元空间