• redis pool


    Redis Pool--Java 

    配置文件

    #redis conf
    
    ADDR=127.0.0.1
    PORT=6379
    AUTH=
    
    #session timeout
    TIMEOUT=1000  
    
    MAX_ACTIVE=500
    MAX_IDLE=200
    MIN_IDLE=100
    MAX_WAIT=1000

    RedisPool.java

      1 public final class RedisPool {
      2 
      3     private static JedisPool jedisPool = null;
      4 
      5     // 最大连接数:可同时建立的最大链接数
      6     private static int max_acti;
      7 
      8     // 最大空闲数:空闲链接数大于maxIdle时,将进行回收
      9     private static int max_idle;
     10 
     11     // 最小空闲数:低于minIdle时,将创建新的链接
     12     private static int min_idle;
     13 
     14     // 最大等待时间:单位ms
     15     private static int max_wait;
     16 
     17     private static String addr;
     18 
     19     private static int port;
     20 
     21     private static String auth;
     22 
     23     // session timeout by seconds
     24     private static int session_timeout;
     25 
     26     private RedisPool() {
     27     }
     28 
     29     /**
     30      * get properties and init RedisPool
     31      */
     32     static {
     33         Properties pps = new Properties();
     34         InputStream in;
     35         try {
     36             in = new BufferedInputStream(new FileInputStream("src"+File.separator+"conf"+File.separator+"redispool.properties"));
     37             pps.load(in);
     38 
     39             addr = pps.getProperty("ADDR");
     40             auth = pps.getProperty("AUTH");
     41             port = Integer.parseInt(pps.getProperty("PORT"));
     42             session_timeout = Integer.parseInt(pps.getProperty("TIMEOUT"));
     43             max_acti = Integer.parseInt(pps.getProperty("MAX_ACTIVE"));
     44             max_idle = Integer.parseInt(pps.getProperty("MAX_IDLE"));
     45             min_idle = Integer.parseInt(pps.getProperty("MIN_IDLE"));
     46             max_wait = Integer.parseInt(pps.getProperty("MAX_WAIT"));
     47 
     48             JedisPoolConfig config = new JedisPoolConfig();
     49             config.setMaxActive(max_acti);
     50             config.setMaxIdle(max_idle);
     51             config.setMaxWait(max_wait);
     52             config.setMinIdle(min_idle);
     53             jedisPool = new JedisPool(config, addr, port, 1000, auth);
     54 
     55         } catch (Exception e) {
     56             throw new RuntimeException("jedisPool init error" + e.getMessage());
     57         }
     58 
     59     }
     60 
     61     /**
     62      * get jedis resource
     63      */
     64     public synchronized static Jedis getJedis() {
     65         if (jedisPool != null) {
     66             return jedisPool.getResource();
     67         } else {
     68             throw new RuntimeException("jedisPool is null");
     69         }
     70     }
     71 
     72     /**
     73      * get value map by key
     74      * 
     75      * @param key
     76      * @return map
     77      */
     78     public static Map<String, String> getHashValue(String key) {
     79         Jedis jedis = getJedis();
     80         Map<String, String> map = new HashMap<String, String>();
     81         Iterator<String> iter = jedis.hkeys(key).iterator();
     82         while (iter.hasNext()) {
     83             String mkey = iter.next();
     84             map.put(mkey, jedis.hmget(key, mkey).get(0));
     85         }
     86         jedisPool.returnResource(jedis);
     87         return map;
     88     }
     89 
     90     /**
     91      * set value by key and map value
     92      * 
     93      * @param key
     94      * @param hash
     95      */
     96     public static void setHashValue(String key, Map<String, String> hash) {
     97         Jedis jedis = getJedis();
     98         jedis.hmset(key, hash);
     99         jedis.expire(key, session_timeout);
    100         jedisPool.returnResource(jedis);
    101     }
    102 
    103     /**
    104      * remove value by key
    105      * 
    106      * @param key
    107      */
    108     public static void remove(String key) {
    109         Jedis jedis = getJedis();
    110         jedis.del(key);
    111         jedisPool.returnResource(jedis);
    112     }
    113 
    114     /**
    115      * expire session time to session_timeout
    116      * 
    117      * @param key
    118      */
    119     public static void expire(String key) {
    120         Jedis jedis = getJedis();
    121         jedis.expire(key, session_timeout);
    122         jedisPool.returnResource(jedis);
    123     }
    124 
    125     /**
    126      * return jedis resource
    127      */
    128     public static void returnResource(final Jedis jedis) {
    129         if (jedis != null) {
    130             jedisPool.returnResource(jedis);
    131         }
    132     }
    133 
    134 }
  • 相关阅读:
    跳跃表原理
    ThreadLocal
    Innodb中的事务隔离级别和锁的关系
    线程池拒绝策略
    vue 的 nextTick 原理
    Git 的基本操作
    JavaScript 的运行机制
    实现一个react系列三:生命周期
    实现一个react系列二:渲染组件
    实现一个react系列一:JSX和虚拟DOM
  • 原文地址:https://www.cnblogs.com/luangeng/p/5767531.html
Copyright © 2020-2023  润新知