Redis的一个简单连接池

本文介绍了一个用于Storm大数据开发的Redis连接池实现方案。该连接池基于Jedis 2.8.0版本的jar包构建,通过配置文件设置最大活跃、空闲连接数及等待时间等参数。

做Storm大数据开发,经常要用到redis来缓存一些东西,所以就写了个redis的连接池,jar包用的是2.8.0版本的,

经过一两个项目的检验,发现这个连接池在使用时还是挺不错的,代码如下:、



public enum RedisSingle {
    INSTANCE;
    private JedisPool jedisPool;

    private RedisSingle() {
        InputStream propInputStream = RedisSingle.class.getClassLoader().getResourceAsStream("config.properties");
        Properties properties = new Properties();
        try {
            properties.load(propInputStream);
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(Integer.parseInt(properties.get("JedisMaxActive").toString()));     //100
        jedisPoolConfig.setMaxIdle(Integer.parseInt(properties.get("JedisMaxIdle").toString()));          //10
        jedisPoolConfig.setMaxWaitMillis(Long.parseLong(properties.get("JedisMaxWait").toString()));        //5000
        jedisPoolConfig.setTestOnBorrow(Boolean.parseBoolean(properties.get("JedisTestOnBorrow").toString()));     //false
        jedisPool = new JedisPool(jedisPoolConfig, properties.get("JedisURL").toString(), Integer.parseInt(properties.get("JedisPort").toString()));   

                               //redis服务器IP,端口6379     
    }

    public Jedis getInstance() {
        return jedisPool.getResource();
    }

    public  void closeInstance(Jedis jedis) {            
        jedis.close();
    }
    
   public static void main(String[] args) {

        Jedis jedis = null;
        try {
            jedis = RedisSingle.INSTANCE.getInstance();
            System.out.println(jedis);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != jedis) {
                RedisSingle.INSTANCE.closeInstance(jedis);
            }
        }    
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值