JAVA redis连接池




/**
 * 缓存连接池
 * @author rcj
 */
@Component
public class CacheService {
	private static final Logger logger = Logger.getLogger(CacheService.class);

	private static JedisPool jedisPool;
	
		
    static{
			String host=ConfigContext.getStringValue("redis.host");
			int port=Integer.valueOf(ConfigContext.getStringValue("redis.port"));
			String password=ConfigContext.getStringValue("redis.pass");
			int timeout=Integer.valueOf(ConfigContext.getStringValue("redis.timeout"));
			int maxIdle=Integer.valueOf(ConfigContext.getStringValue("redis.maxIdle"));
			int maxTotal=Integer.valueOf(ConfigContext.getStringValue("redis.maxActive"));
			int maxWait=Integer.valueOf(ConfigContext.getStringValue("redis.maxWait"));
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxWaitMillis(maxWait); //  最大等待时间
			config.setMaxTotal(maxTotal);         //  最大连接数
			config.setMinIdle(maxIdle);           //  允许最小的空闲连接数
			config.setTestOnBorrow(false);  //  申请到连接时是否效验连接是否有效,对性能有影响,建议关闭
			config.setTestOnReturn(false);  //  使用完连接放回连接池时是否效验连接是否有效,对性能有影响,建议关闭
			config.setTestWhileIdle(true);  //  申请到连接时,如果空闲时间大于TimeBetweenEvictionRunsMillis时间,效验连接是否有效,建议开启,对性能有效不大
			config.setTimeBetweenEvictionRunsMillis(30000); //TestWhileIdle的判断依据
			jedisPool=new JedisPool(config, host, port, timeout,password);
    }
    
    
    /**
     * 获取Jedis对象
     * @return
     */
    public static Jedis getJedis(){
    	return jedisPool.getResource();
    }
    
    
    
    /**
     * 设置缓存对象
     * @param key
     * @param value
     */
    public void set(String key,String value){
        Jedis jedis = null;
        boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            jedis.set(key, value);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
    }
    
    /**
     * 设置缓存对象
     * @param key
     * @param value
     */
    public void lset(String key,int index,String value){
        Jedis jedis = null;
        boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            jedis.lset(key, index, value);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
    }
    
    
    /**
     * 删除缓存对象
     * @param key
     * @param value
     */
    public Long lrem(String key,long count,String value){
        Jedis jedis = null;
        boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.lrem(key, count, value);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
    }
    
    
    /**
     * 删除缓存对象
     * @param key
     * @param value
     */
    public Long lrem(byte[] key,long count,byte[] value){
        Jedis jedis = null;
        boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.lrem(key, count, value);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
    }
    
    
    /**
     * 设置缓存对象
     * @param key
     * @param value
     */
    public void set(byte[] key,byte[] value){
        Jedis jedis = null;
        boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            jedis.set(key, value);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
    }
    
    
    
    /**
     * 获取缓存对象
     * @param key
     * @return
     */
    public String get(String key){
        Jedis jedis = null;
        boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.get(key);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
    }
    
    /**
     * 获取缓存对象
     * @param key
     * @return
     */
    public byte[] get(byte[] key){
        Jedis jedis = null;
        boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.get(key);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
    }

    /**
     * 删除缓存对象
     * @param key
     */
    public void del(String key){
        Jedis jedis = null;
        boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            jedis.del(key);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
    }
    
    /**
     * 判断key是否存在
     * @param key
     * @return
     */
    public boolean exists(String key){
    	 Jedis jedis = null;
    	 boolean broken = false;
         try{
            jedis = jedisPool.getResource();
            return jedis.exists(key);
         }catch (JedisException e) {
             broken = handleJedisException(e);
             throw e;
         } finally {
             closeResource(jedis, broken);
         }
    }
    
    public Long llen(String key) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.llen(key);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    public Long llen(byte[] key) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.llen(key);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    
    
    public Long lpush(String key, String[] strings) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.lpush(key,strings);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    
    public Boolean hexists(byte[] key, byte[] field) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.hexists(key,field);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    
    public byte[] hget(byte[] key, byte[] field) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.hget(key,field);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    
    public Long hlen(byte[] key) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.hlen(key);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    
    public Long hlen(String key) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.hlen(key);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    
    public String hget(String key, String field) {
    	 Jedis jedis = null;
    	 boolean broken = false;
         try{
             jedis = jedisPool.getResource();
             return jedis.hget(key,field);
         }catch (JedisException e) {
             broken = handleJedisException(e);
             throw e;
         } finally {
             closeResource(jedis, broken);
         }
	}
    
    
    public Map<String,String> hgetAll(String key) {
   	 Jedis jedis = null;
   	 boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.hgetAll(key);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    
    public List<String> lrange(String key, long start, long end) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.lrange(key,start,end);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    public List<byte[]> lrange(byte[] key, long start, long end) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.lrange(key,start,end);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    public Long rpush(String key, String jsonStr) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.rpush(key,jsonStr);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    public Long lpush(String key, String strings) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.lpush(key,strings);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    public String rpop(String key) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.rpop(key);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    
    public Long hset(String key, String field, String value) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.hset(key,field,value);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    
    public Boolean hexists(String key, String field) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.hexists(key,field);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    public Long hset(byte[] key, byte[] field, byte[] value) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.hset(key,field,value);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    
    public Long hdel(byte[] key, byte[] byteInfo) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.hdel(key,byteInfo);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    public Long hdel(String key, String byteInfo) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.hdel(key,byteInfo);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    public String hmset(String key, Map<String, String> hash) {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            return jedis.hmset(key,hash);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    /*
     * 
     */
    public void expire(String key, int seconds){
        Jedis jedis = null;
        boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            jedis.expire(key,seconds);
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
    }
    
    
    
    
    
    public void flushdb() {
    	Jedis jedis = null;
    	boolean broken = false;
        try{
            jedis = jedisPool.getResource();
            jedis.flushDB();
        }catch (JedisException e) {
            broken = handleJedisException(e);
            throw e;
        } finally {
            closeResource(jedis, broken);
        }
	}
    
    
    
    /**
     * Handle jedisException, write log and return whether the connection is broken.
     */
    protected boolean handleJedisException(JedisException jedisException) {
        if (jedisException instanceof JedisConnectionException) {
            logger.error("Redis connection " + jedisPool + " lost.", jedisException);
        } else if (jedisException instanceof JedisDataException) {
            if ((jedisException.getMessage() != null) && (jedisException.getMessage().indexOf("READONLY") != -1)) {
                logger.error("Redis connection " + jedisPool + " are read-only slave.", jedisException);
            } else {
                // dataException, isBroken=false
                return false;
            }
        } else {
            logger.error("Jedis exception happen.", jedisException);
        }
        return true;
    }
    /**
     * Return jedis connection to the pool, call different return methods depends on the conectionBroken status.
     */
    @SuppressWarnings("deprecation")
	public static void closeResource(Jedis jedis, boolean conectionBroken) {
        try {
            if (conectionBroken) {
                jedisPool.returnBrokenResource(jedis);
            } else {
                jedisPool.returnResource(jedis);
            }
        } catch (Exception e) {
            logger.error("return back jedis failed, will fore close the jedis.", e);
            if(jedis!=null){
            	jedis.close();
            }
        }
    }
    
    
    
    
    
    
    
    
    

}



使用:

@Autowired
 private CacheService jedis;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值