码迷,mamicode.com
首页 > 其他好文 > 详细

JedisPool

时间:2018-02-07 19:32:50      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:stat   turn   object   client   row   log   isp   red   ack   

 1 package redis;
 2 
 3 import redis.clients.jedis.Jedis;
 4 import redis.clients.jedis.JedisPool;
 5 import redis.clients.jedis.JedisPoolConfig;
 6 
 7 public class JedisPoolUtil {
 8     private static volatile JedisPool jedisPool = null;
 9     private JedisPoolUtil() {}
10     public static JedisPool getJedisPoolIntence() {
11         if(null == jedisPool) {
12             synchronized (JedisPoolUtil.class) {
13                 if(null == jedisPool) {
14                     JedisPoolConfig poolConfig = new JedisPoolConfig();
15                     poolConfig.setMaxActive(1000);
16                     poolConfig.setMaxIdle(32);
17                     poolConfig.setMaxWait(100*1000);
18                     poolConfig.setTestOnBorrow(true);
19                     
20                     jedisPool = new JedisPool(poolConfig, "192.168.88.128", 6379);
21                     return jedisPool;
22                 }
23             }
24         }
25         return jedisPool;
26     }
27     
28     public static void relace(JedisPool jedisPool, Jedis jedis) {
29         if(null != jedis) {
30             jedisPool.returnResourceObject(jedis);
31         }
32     }
33 }    
 1 package redis;
 2 
 3 import redis.clients.jedis.Jedis;
 4 import redis.clients.jedis.JedisPool;
 5 
 6 public class TestPool {
 7     public static void main(String[] args) {
 8         JedisPool jedisPool = JedisPoolUtil.getJedisPoolIntence();
 9         Jedis jedis = null;
10         try {
11             jedis = jedisPool.getResource();
12             jedis.set("aa", "bb");
13             System.out.println("success");
14         } catch (Exception e) {
15             e.printStackTrace();
16         }finally {
17             JedisPoolUtil.relace(jedisPool, jedis);
18         }
19     }
20 }

双锁单例

JedisPool

标签:stat   turn   object   client   row   log   isp   red   ack   

原文地址:https://www.cnblogs.com/redhat0019/p/8427395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!