首页 关于 归档

redis pool java

Table of Contents

redis pool java使用

使用

...
Jedis jedis = RedisHandler.getJedis();
try{
    result = jedis.incr(key);
}catch(Exception e){
    log4j.error(e.getMessage());
}finally{
    RedisHandler.close(jedis);
}
...

class

package com.kugou.tianmu.handlers;

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

public class RedisHandler {

   /**
    *
    */
    private static JedisPool pool = null;

    /**
     *
     */
    private static JedisPoolConfig conf = new JedisPoolConfig();

    /**
     *
     */
    static {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(6000);
        config.setMaxIdle(1000);
        config.setMaxWaitMillis(20000);
        pool = new JedisPool(conf, ip地址, 端口号, 20000);
    }


    public static Jedis getJedis() {
        return pool.getResource();
    }

    public static void close(Jedis jedis) {
        if (null != jedis) {
            pool.returnResource(jedis);
        }
    }
}

Author: josephzeng

Last Updated 2016-02-03. Created by Emacs 24.5.1 (Org mode 8.2.10)

Validate