package com.bshinfo.web.base.cache; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import javax.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; /*** * 类 <code>{RedisCacheTemplate}</code> * Redis缓存操作类 * * @author zhangChao * * @since JDK1.7 * * @version 1.0 * * @date 2016-12-9 12:19:44 * */ @Component(value="redisCacheTemplate") public class RedisCacheTemplate { /*** * 日志工具 */ public static Logger logger = Logger.getLogger(RedisCacheTemplate.class); /** * Redis存储对象的方法(实体对象,一定要实现 Serializable接口,并且显式声明 serialVersionUID 值) * * @param key 存储的Key值 * @param value 存储的Value值 */ public void put(final String key, final Object value) { if(null == value) { return; } if(value instanceof String) { if(StringUtils.isEmpty(value.toString())) { return; } } clusterRedisTemplate.execute( new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection)throws DataAccessException { byte[] keyb = key.getBytes(); byte[] valueb = toByteArray(value); // 判断当前值是否已经存在 if(connection.exists(keyb)) { // 删除原数据 connection.del(keyb); } connection.set(keyb, valueb); return 1L; } } ); } /** * Redis存储对象的方法(实体对象,一定要实现 Serializable接口,并且显式声明 serialVersionUID 值) * * @param key 存储的Key值 * @param value 存储的Value值 * @param liveTime 存储的生命周期(单位:秒) */ public void put(final String key, final Object value,final long liveTime) { if(null == value) { return; } if(value instanceof String) { if(StringUtils.isEmpty(value.toString())) { return; } } clusterRedisTemplate.execute( new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection)throws DataAccessException { byte[] keyb = key.getBytes(); byte[] valueb = toByteArray(value); // 判断当前值是否已经存在 if(connection.exists(keyb)) { // 删除原数据 connection.del(keyb); } connection.set(keyb, valueb); if (liveTime > 0) { connection.expire(keyb, liveTime); } return 1L; } } ); } /** * Redis获取对象的方法 * * @param key 获取对象对应的Key值 * * @return {Object} */ public Object get(String key) { final String keyf = (String) key; Object object; object = clusterRedisTemplate.execute( new RedisCallback<Object>() { public Object doInRedis(RedisConnection connection)throws DataAccessException { byte[] key = keyf.getBytes(); byte[] value = connection.get(key); if (value == null) { return null; } return toObject(value); } } ); return object; } /** * 将字节数组转化为Object对象 * * @param bytes * * @return {Object} */ private Object toObject(byte[] bytes) { Object obj = null; ByteArrayInputStream bis = null; ObjectInputStream ois =null; try { bis = new ByteArrayInputStream(bytes); ois = new ObjectInputStream(bis); obj = ois.readObject(); ois.close(); bis.close(); } catch (IOException ex) { logger.error("[将字节数组转化为Object对象IO异常,ErrorMsg:]",ex); } catch (ClassNotFoundException ex) { logger.error("[将字节数组转化为Object对象ClassNotFound异常,ErrorMsg:]",ex); } finally { if(ois!=null) { try { ois.close(); } catch (IOException e) { logger.error("[将字节数组转化为Object对象流关闭异常,ErrorMsg:]",e); } } if(bis!=null) { try { bis.close(); } catch (IOException e) { logger.error("[将字节数组转化为Object对象流关闭异常,ErrorMsg:]",e); } } } return obj; } /** * 将Object对象转化为字节数组 * * @param bytes * * @return {Object} */ private byte[] toByteArray(Object obj) { byte[] bytes = null; ByteArrayOutputStream bos = null; ObjectOutputStream oos = null; try { bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(obj); oos.flush(); bytes = bos.toByteArray(); oos.close(); bos.close(); } catch (IOException ex) { logger.error("[将Object对象转化为字节数组IO异常,ErrorMsg:]",ex); } finally { if(oos!=null) { try { oos.close(); } catch (IOException e) { logger.error("[将Object对象转化为字节数组流关闭异常,ErrorMsg:]",e); } } if(bos!=null) { try { bos.close(); } catch (IOException e) { logger.error("[将Object对象转化为字节数组流关闭异常,ErrorMsg:]",e); } } } return bytes; } /**Redis模板注入*/ @Resource private RedisTemplate<String, Object> clusterRedisTemplate; }