您的位置:首页 > 数据库 > Redis

Redis的一个简单连接池

2017-03-20 10:02 513 查看
做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);

            }

        }    

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: