importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.ObjectInputStream;importjava.io.ObjectOutputStream;importjava.util.ArrayList;importjava.util.List;importjava.util.Set;importjavax.annotation.PostConstruct;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importorg.aspectj.weaver.patterns.ThisOrTargetPointcut;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importcom.aqh.util.MyProperties;importcom.sun.org.glassfish.external.statistics.Statistic;importredis.clients.jedis.Jedis;importredis.clients.jedis.JedisPool;importredis.clients.jedis.JedisPoolConfig;/*** jedis缓存工具*/@Service("jedisUtil")public classJedisUtil {privateJedisPool pool;
@AutowiredprivateMyProperties properties;private static Log log = LogFactory.getLog(JedisUtil.class);privateJedisUtil() {
}
@SuppressWarnings("unused")
@PostConstruct//指定spring实例化对象之后调用的方法
private voidinit() {
JedisPoolConfig config= newJedisPoolConfig();
config.setMaxActive(Integer.parseInt(properties.getJedisMaxActive()));
config.setMaxIdle(Integer.parseInt(properties.getJedisMaxIdle()));
config.setMaxWait(Long.parseLong(properties.getJedisMaxWait()));
config.setTestOnBorrow(false);
pool= new JedisPool(newJedisPoolConfig(), properties.getJedisHost(),
Integer.parseInt(properties.getJedisPort()),
Integer.parseInt(properties.getJedisTimeout()));
}public voidset(String key, String value) {
Jedis jedis= this.getResource();try{
jedis.set(key, value);
}finally{this.returnResource(jedis);
}
}publicString get(String key) {
Jedis jedis= this.getResource();try{returnjedis.get(key);
}finally{this.returnResource(jedis);
}
}public voidsetObject(String key, Object obj) {
Jedis jedis= this.getResource();try{
jedis.set(key.getBytes(), serialize(obj));
}finally{this.returnResource(jedis);
}
}publicObject getObject(String key) {
Jedis jedis= this.getResource();try{if(jedis.get(key.getBytes()) == null) {return null;
}else{returnunserialize(jedis.get(key.getBytes()));
}
}finally{this.returnResource(jedis);
}
}/*** 删除key
*@paramkey*/
public voiddelkey(String...keys) {
Jedis jedis= this.getResource();try{
jedis.del(keys);
}finally{this.returnResource(jedis);
}
}/*** 设置hash的值
*@paramkey hash中的key
*@paramfield hash中的域
*@paramobj 值*/
public voidsetHash(String key,String field,Object obj) {
Jedis jedis= this.getResource();try{
jedis.hset(key.getBytes(), field.getBytes(), serialize(obj));
}finally{this.returnResource(jedis);
}
}/*** 查找redis中hash的value值
*@paramkey hash中的key
*@paramfield hash中的域
*@return返回对象*/
publicObject getHash(String key,String field) {
Jedis jedis= this.getResource();try{if (jedis.hget(key, field) == null) {return null;
}returnunserialize(jedis.hget(key.getBytes(), field.getBytes()));
}finally{this.returnResource(jedis);
}
}/*** 删除hash中的指定域
*@paramkey
*@paramfields
*@return
*/
publicLong removeHash(String key,String fields) {
Jedis jedis= this.getResource();try{returnjedis.hdel(key.getBytes(),fields.getBytes());
}finally{this.returnResource(jedis);
}
}/*** 返回hash中的所有域
*@paramkey*/
public SethKeys(String key) {
Jedis jedis= this.getResource();try{
Set hkeys =jedis.hkeys(key);returnhkeys;
}finally{this.returnResource(jedis);
}
}/*** 序列化
*@paramobject
*@return
*/
private static byte[] serialize(Object object) {
ObjectOutputStream oos= null;
ByteArrayOutputStream baos= null;try{//序列化
baos = newByteArrayOutputStream();
oos= newObjectOutputStream(baos);
oos.writeObject(object);byte[] bytes =baos.toByteArray();returnbytes;
}catch(Exception e) {
e.printStackTrace();
log.error("jedis序列化异常.....");
}return null;
}/*** 反序列化
*
*@parambytes
*@return
*/
private static Object unserialize(byte[] bytes) {
ByteArrayInputStream bais= null;try{//反序列化
bais = newByteArrayInputStream(bytes);
ObjectInputStream ois= newObjectInputStream(bais);returnois.readObject();
}catch(Exception e) {
e.printStackTrace();
log.info("jedis反序列化异常.....");
}return null;
}/*** 获取jedis
*@return
*/
privateJedis getResource() {
Jedis jedis=pool.getResource();
jedis.auth(properties.getJedisPassword());returnjedis;
}/*** 设置生命周期(过期时间)
*@paramkey
*@paramsecond*/
public void setExpireByKey(String key, intseconds) {
Jedis jedis= null;try{
jedis= this.getResource();
jedis.expire(key, seconds);
}catch(Exception e) {
log.error(e);
}finally{this.returnResource(jedis);
}
}/*** 获取某个Key的余下存活时间(秒)。
*@paramkey
*@return存活时间(秒)*/
public longgetTimeToLive(String key) {
Jedis jedis= null;long sec = -2;try{
jedis= this.getResource();
sec=jedis.ttl(key);
}catch(Exception e) {
log.error(e);
}finally{this.returnResource(jedis);
}returnsec;
}/*** jedis放回连接池
*@paramjedis*/
private voidreturnResource(Jedis jedis) {
pool.returnResource(jedis);
}/*** 释放Redis资源池。*/
public voiddestroy() {if(pool != null) {
pool.destroy();
}
log.info("Redis池已销毁");
}
}