zoukankan      html  css  js  c++  java
  • JedisPool

     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 }

    双锁单例

  • 相关阅读:
    Java 中的按值传递
    字符串排序(非字典排序)
    字符串匹配的KMP算法(转)
    效率更高的整数转化为字符串函数
    Trie 树(转)
    C 语言字符串(译)
    linux 下 epoll 编程
    CSS攻击:记录用户密码
    Wireshark(抓包神器)使用方法
    搭建KVM环境——Linux上安装KVM带web管理界面
  • 原文地址:https://www.cnblogs.com/redhat0019/p/8427395.html
Copyright © 2011-2022 走看看