package com.gxx.manage.shiro; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.exceptions.JedisConnectionException; /** *
*
Title:
*
* Jedis工具类 *
*
Description:
*
*

none *

*
* * @author Administrator * @version 1.0, 2015年9月22日 * @since manage * */ public class JedisManager { private JedisPool jedisPool; public Jedis getJedis() { Jedis jedis = null; try { jedis = getJedisPool().getResource(); } catch (Exception e) { throw new JedisConnectionException(e); } return jedis; } public void returnResource(Jedis jedis, boolean isBroken) { if (jedis == null) return; if (isBroken) getJedisPool().returnBrokenResource(jedis); else getJedisPool().returnResource(jedis); } public byte[] getValueByKey(int dbIndex, byte[] key) throws Exception { Jedis jedis = null; byte[] result = null; boolean isBroken = false; try { jedis = getJedis(); jedis.select(dbIndex); result = jedis.get(key); } catch (Exception e) { isBroken = true; throw e; } finally { returnResource(jedis, isBroken); } return result; } public void deleteByKey(int dbIndex, byte[] key) throws Exception { Jedis jedis = null; boolean isBroken = false; try { jedis = getJedis(); jedis.select(dbIndex); jedis.del(key); } catch (Exception e) { isBroken = true; throw e; } finally { returnResource(jedis, isBroken); } } public void saveValueByKey(int dbIndex, byte[] key, byte[] value, int expireTime) throws Exception { Jedis jedis = null; boolean isBroken = false; try { jedis = getJedis(); jedis.select(dbIndex); jedis.set(key, value); if (expireTime > 0) jedis.expire(key, expireTime); } catch (Exception e) { isBroken = true; throw e; } finally { returnResource(jedis, isBroken); } } public JedisPool getJedisPool() { return jedisPool; } public void setJedisPool(JedisPool jedisPool) { /** * 配置文件注入有问题,这里手动创建 * TODO */ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(1000); config.setMaxIdle(600); config.setMinIdle(300); config.setTestOnBorrow(true); JedisPool pool = new JedisPool(config, "121.43.104.34", 6379, 20000); this.jedisPool = pool; } }