JedisPool和spring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">

	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="2048" />
		<property name="maxIdle" value="200" />
		<property name="numTestsPerEvictionRun" value="1024" />
		<property name="timeBetweenEvictionRunsMillis" value="30000" />
		<property name="minEvictableIdleTimeMillis" value="-1" />
		<property name="softMinEvictableIdleTimeMillis" value="10000" />
		<property name="maxWaitMillis" value="1500" />
		<property name="testOnBorrow" value="true" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnReturn" value="false" />
		<property name="jmxEnabled" value="true" />
		<property name="jmxNamePrefix" value="youyuan" />
		<property name="blockWhenExhausted" value="false" />
	</bean>
	
	<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
		<constructor-arg index="0" ref="jedisPoolConfig"/>  
        <constructor-arg index="1" value="${redis.slaver.host}"/>  
        <constructor-arg index="2" value="${redis.slaver.port}" type="int"/>  
        <!-- 
        <constructor-arg index="3" value="${redis.timeout}" type="int"/>  
        <constructor-arg index="4" value="${redis.password}"/>
         -->
	</bean>
	
</beans>

 

#最大分配的对象数  
redis.pool.maxActive=1024  
#最大能够保持idel状态的对象数  
redis.pool.maxIdle=200  
#当池内没有返回对象时,最大等待时间  
redis.pool.maxWait=1000  
#当调用borrow Object方法时,是否进行有效性检查  
redis.pool.testOnBorrow=true  
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true  
#IP
#redis.slaver.host=192.168.31.125
redis.slaver.host=127.0.0.1
#Port 
redis.slaver.port=6379  

 

package com.dongly.sssr.orm;

import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
public class RedisUtil {

	public static Logger logger = LoggerFactory.getLogger(RedisUtil.class);

	@Autowired
	private JedisPool jedisPool;

	/**
	 * 回收jedis
	 * 
	 * @param jedis
	 */
	private void returnJedis(Jedis jedis) {
		if (jedis != null) {
			try {
				jedis.close();
			} catch (Exception e) {
				logger.error("returnJedis error.", e);
			}
		}
	}

	/**
	 * 设置一个key的过期时间(单位:秒)
	 * 
	 * @param key
	 *            key值
	 * @param seconds
	 *            多少秒后过期
	 * @return true:设置了过期时间 false:没有设置过期时间/不能设置过期时间
	 */
	public Boolean expire(String key, int seconds) {
		if (key == null || key.equals("")) {
			return false;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			Long expire = jedis.expire(key, seconds);
			return expire == 1 ? true : false;
		} catch (Exception ex) {
			logger.error("expire error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return false;
	}

	/**
	 * 设置一个key在某个时间点过期
	 * 
	 * @param key
	 *            key值
	 * @param unixTimestamp
	 *            unix时间戳,从1970-01-01 00:00:00开始到现在的秒数
	 * @return true:设置了过期时间 false:没有设置过期时间/不能设置过期时间
	 */
	public Boolean expireAt(String key, long unixTimestamp) {
		if (StringUtils.isEmpty(key)) {
			return false;
		}

		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			Long expireAt = jedis.expireAt(key, unixTimestamp);
			return expireAt == 1 ? true : false;
		} catch (Exception ex) {
			logger.error("expireAt error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return false;
	}

	/**
	 * 截断一个List
	 * 
	 * @param key
	 *            列表key
	 * @param start
	 *            开始位置 从0开始
	 * @param end
	 *            结束位置
	 * @return true:表示成功
	 */
	public Boolean trimList(String key, long start, long end) {
		if (StringUtils.isEmpty(key)) {
			return false;
		}

		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.ltrim(key, start, end);
			return true;
		} catch (Exception ex) {
			logger.error("trimList error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return false;
	}

	/**
	 * 检查Set 元素个数
	 * 
	 * @param key
	 * @return -1:表示key 为空
	 */
	public long setSize(String key) {
		if (StringUtils.isEmpty(key)) {
			return -1;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.scard(key);
		} catch (Exception ex) {
			logger.error("setSize error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return -1;
	}

	/**
	 * 添加到Set中(同时设置过期时间)
	 * 
	 * @param key
	 *            key值
	 * @param seconds
	 *            过期时间 单位s
	 * @param value
	 * @return 成功true
	 */
	public boolean addSet(String key, int seconds, String... value) {
		if (StringUtils.isEmpty(key)) {
			return false;
		}

		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			Boolean addSet = addSet(key, value);
			if (addSet) {
				expire(key, seconds);
				return true;
			}
		} catch (Exception ex) {
			logger.error("addSet error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return false;
	}

	/**
	 * 添加到Set中
	 * 
	 * @param key
	 * @param value
	 * @return -1 表示添加失败 1:成功
	 */
	public Boolean addSet(String key, String... value) {
		if (StringUtils.isEmpty(key)) {
			return false;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.sadd(key, value);
			return true;
		} catch (Exception ex) {
			logger.error("addSet error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return false;
	}

	/**
	 * @param key
	 * @param value
	 * @return 判断value值是否包含在set中
	 */
	public boolean containsInSet(String key, String value) {
		if (StringUtils.isEmpty(key)) {
			return false;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.sismember(key, value);
		} catch (Exception ex) {
			logger.error("containsInSet error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return false;
	}

	/**
	 * 获取Set
	 * 
	 * @param key
	 * @return 返回null或者【】 表示 没有这个➹
	 */
	public Set<String> getSet(String key) {

		if (StringUtils.isEmpty(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.smembers(key);
		} catch (Exception ex) {
			logger.error("getSet error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return null;
	}

	/**
	 * 从set中删除value
	 * 
	 * @param key
	 * @return
	 */
	public boolean removeSetValue(String key, String... value) {
		if (StringUtils.isEmpty(key)) {
			return false;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			Long srem = jedis.srem(key, value);
			return true;
		} catch (Exception ex) {
			logger.error("removeSetValue error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return false;
	}

	/**
	 * 从list中删除value 默认count 1
	 * 
	 * @param key
	 * @param values
	 *            值list
	 * @return
	 */
	public int removeListValue(String key, List<String> values) {
		return removeListValue(key, 1, values);
	}

	/**
	 * 从list中删除value
	 * 
	 * @param key
	 * @param count
	 * @param values
	 *            值list
	 * @return
	 */
	public int removeListValue(String key, long count, List<String> values) {
		int result = 0;
		if (values != null && values.size() > 0) {
			for (String value : values) {
				if (removeListValue(key, count, value)) {
					result++;
				}
			}
		}
		return result;
	}

	/**
	 * 从list中删除value
	 * 
	 * @param key
	 * @param count
	 *            要删除个数
	 * @param value
	 * @return
	 */
	public boolean removeListValue(String key, long count, String value) {
		if (StringUtils.isEmpty(key)) {
			return false;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.lrem(key, count, value);
			return true;
		} catch (Exception ex) {
			logger.error("containsInSet error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return false;
	}

	/**
	 * 截取List
	 * 
	 * @param key
	 * @param start
	 *            起始位置
	 * @param end
	 *            结束位置
	 * @return
	 */
	public List<String> rangeList(String key, long start, long end) {

		if (StringUtils.isEmpty(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.lrange(key, start, end);
		} catch (Exception ex) {
			logger.error("rangeList 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage(), ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return null;
	}

	/**
	 * List长度
	 * 
	 * @param key
	 * @return
	 */
	public long countList(String key) {
		if (StringUtils.isEmpty(key)) {
			return -1;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.llen(key);
		} catch (Exception ex) {
			logger.error("countList error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return -1;
	}

	/**
	 * 添加到List中(同时设置过期时间)
	 * 
	 * @param key
	 *            key值
	 * @param seconds
	 *            过期时间 单位s
	 * @param value
	 * @return
	 */
	public boolean addList(String key, int seconds, String... value) {
		boolean result = addList(key, value);
		if (result) {
			return expire(key, seconds);
		}
		return false;
	}

	/**
	 * 添加到List
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean addList(String key, String... value) {
		if (StringUtils.isEmpty(key)) {
			return false;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.rpush(key, value);
			return true;
		} catch (Exception ex) {
			logger.error("addList error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return false;
	}

	/**
	 * 添加到List(只新增)
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean addList(String key, List<String> list) {
		if (key == null || list == null || list.size() == 0) {
			return false;
		}
		for (String value : list) {
			addList(key, value);
		}
		return true;
	}

	/**
	 * 获取List
	 * 
	 * @param key
	 * @return
	 */
	public List<String> getList(String key) {
		if (StringUtils.isEmpty(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.lrange(key, 0, -1);
		} catch (Exception ex) {
			logger.error("getList error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return null;
	}

	/**
	 * 设置HashSet对象
	 *
	 * @param domain 域名
	 * @param key 键值
	 * @param value Json String or String value
	 * @return -1:添加失败  0:field原来在map里面已经存在 1:field是一个新的字段
	 */
	public Long setHSet(String key, String field, String value) {
		if (StringUtils.isEmpty(key)) {
			return -1l;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.hset(key, field, value);
		} catch (Exception ex) {
			logger.error("setHSet error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return -1l;
	}

	/**
	 * 获得HashSet对象
	 * @param field 域名
	 * @param key 键值
	 * @return Json String or String value
	 */
	public String getHSet(String key, String field) {
		if (StringUtils.isEmpty(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.hget(key, field);
		} catch (Exception ex) {
			logger.error("setHSet error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return null;
	}

	/**
	 * 删除HashSet对象
	 *
	 * @param domain
	 *            域名
	 * @param key
	 *            键值
	 * @return 删除的记录数 -1 表示异常  0 表示删除失败
	 */
	public long delHSet(String key, String field) {
		if (StringUtils.isEmpty(key)) {
			return -1;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.hdel(key, field);
		} catch (Exception ex) {
			logger.error("delHSet error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return -1;
	}


	/**
	 * 判断Hash field是否存在
	 *
	 * @param domain
	 *            域名
	 * @param key
	 *            键值
	 * @return
	 */
	public boolean existsHSet(String key, String field) {
		if (StringUtils.isEmpty(key)) {
			return false;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.hexists(key, field);
		} catch (Exception ex) {
			logger.error("delHSet error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return false;
	}

	/**
	 * 返回 Hash 集中所有字段的value值
	 * @param key
	 * @return
	 */

	public List<String> hvals(String key) {
		if (StringUtils.isEmpty(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.hvals(key);
		} catch (Exception ex) {
			logger.error("hvals error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return null;
	}

	/**
	 * 返回 Hash 集中所有字段的key值
	 * @param domain
	 * @return
	 */

	public Set<String> hkeys(String key) {
		if (StringUtils.isEmpty(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.hkeys(key);
		} catch (Exception ex) {
			logger.error("hkeys error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return null;
	}

	/**
	 * 返回 Hash 中字段值总数
	 * @param key
	 * @return
	 */
	public long lenHset(String key) {
		if (StringUtils.isEmpty(key)) {
			return -1;
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.hlen(key);
		} catch (Exception ex) {
			logger.error("lenHset error.", ex);
			returnJedis(jedis);
		} finally {
			returnJedis(jedis);
		}
		return -1;
	}

}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值