1、spring相关配置如下:
2、缓存工具类CacheUtils
package com.lh.core.core.util; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import org.springframework.data.redis.core.BoundHashOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.util.CollectionUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.rd.ifaes.common.dict.ExpireTime; import com.lh.common.util.JsonMapper; import com.rd.ifaes.common.util.SpringContextHolder; import com.rd.ifaes.common.util.StringUtils; /** * 通用缓存工具类 * @author lh * @version 3.0 * @since 2016-6-22 * */ public class CacheUtils { private static StringRedisTemplate stringRedisTemplate = SpringContextHolder.getBean("stringRedisTemplate"); private static RedisTemplateredisTemplate = SpringContextHolder.getBean("redisTemplate"); /** * 删除缓存
* 根据key精确匹配删除 * @param key */ @SuppressWarnings("unchecked") public static void del(String... key){ if(key!=null && key.length > 0){ if(key.length == 1){ redisTemplate.delete(key[0]); }else{ redisTemplate.delete(CollectionUtils.arrayToList(key)); } } } /** * 批量删除
* (该操作会执行模糊查询,请尽量不要使用,以免影响性能或误删) * @param pattern */ public static void batchDel(String... pattern){ for (String kp : pattern) { redisTemplate.delete(redisTemplate.keys(kp + "*")); } } /** * 取得缓存(int型) * @param key * @return */ public static Integer getInt(String key){ String value = stringRedisTemplate.boundValueOps(key).get(); if(StringUtils.isNotBlank(value)){ return Integer.valueOf(value); } return null; } /** * 取得缓存(字符串类型) * @param key * @return */ public static String getStr(String key){ return stringRedisTemplate.boundValueOps(key).get(); } /** * 取得缓存(字符串类型) * @param key * @return */ public static String getStr(String key, boolean retain){ String value = stringRedisTemplate.boundValueOps(key).get(); if(!retain){ redisTemplate.delete(key); } return value; } /** * 获取缓存
* 注:基本数据类型(Character除外),请直接使用get(String key, Classclazz)取值 * @param key * @return */ public static Object getObj(String key){ return redisTemplate.boundValueOps(key).get(); } /** * 获取缓存
* 注:java 8种基本类型的数据请直接使用get(String key, Classclazz)取值 * @param key * @param retain 是否保留 * @return */ public static Object getObj(String key, boolean retain){ Object obj = redisTemplate.boundValueOps(key).get(); if(!retain){ redisTemplate.delete(key); } return obj; } /** * 获取缓存
* 注:该方法暂不支持Character数据类型 * @param key key * @param clazz 类型 * @return */ @SuppressWarnings("unchecked") public staticT get(String key, Class clazz) { return (T)redisTemplate.boundValueOps(key).get(); } /** * 获取缓存json对象
* @param key key * @param clazz 类型 * @return */ public staticT getJson(String key, Class clazz) { return JsonMapper.fromJsonString(stringRedisTemplate.boundValueOps(key).get(), clazz); } /** * 将value对象写入缓存 * @param key * @param value * @param time 失效时间(秒) */ public static void set(String key,Object value,ExpireTime time){ if(value.getClass().equals(String.class)){ stringRedisTemplate.opsForValue().set(key, value.toString()); }else if(value.getClass().equals(Integer.class)){ stringRedisTemplate.opsForValue().set(key, value.toString()); }else if(value.getClass().equals(Double.class)){ stringRedisTemplate.opsForValue().set(key, value.toString()); }else if(value.getClass().equals(Float.class)){ stringRedisTemplate.opsForValue().set(key, value.toString()); }else if(value.getClass().equals(Short.class)){ stringRedisTemplate.opsForValue().set(key, value.toString()); }else if(value.getClass().equals(Long.class)){ stringRedisTemplate.opsForValue().set(key, value.toString()); }else if(value.getClass().equals(Boolean.class)){ stringRedisTemplate.opsForValue().set(key, value.toString()); }else{ redisTemplate.opsForValue().set(key, value); } if(time.getTime() > 0){ redisTemplate.expire(key, time.getTime(), TimeUnit.SECONDS); } } /** * 将value对象以JSON格式写入缓存 * @param key * @param value * @param time 失效时间(秒) */ public static void setJson(String key,Object value,ExpireTime time){ stringRedisTemplate.opsForValue().set(key, JsonMapper.toJsonString(value)); if(time.getTime() > 0){ stringRedisTemplate.expire(key, time.getTime(), TimeUnit.SECONDS); } } /** * 更新key对象field的值 * @param key 缓存key * @param field 缓存对象field * @param value 缓存对象field值 */ public static void setJsonField(String key, String field, String value){ JSONObject obj = JSON.parseObject(stringRedisTemplate.boundValueOps(key).get()); obj.put(field, value); stringRedisTemplate.opsForValue().set(key, obj.toJSONString()); } /** * 递减操作 * @param key * @param by * @return */ public static double decr(String key, double by){ return redisTemplate.opsForValue().increment(key, -by); } /** * 递增操作 * @param key * @param by * @return */ public static double incr(String key, double by){ return redisTemplate.opsForValue().increment(key, by); } /** * 获取double类型值 * @param key * @return */ public static double getDouble(String key) { String value = stringRedisTemplate.boundValueOps(key).get(); if(StringUtils.isNotBlank(value)){ return Double.valueOf(value); } return 0d; } /** * 设置double类型值 * @param key * @param value * @param time 失效时间(秒) */ public static void setDouble(String key, double value, ExpireTime time) { stringRedisTemplate.opsForValue().set(key, String.valueOf(value)); if(time.getTime() > 0){ stringRedisTemplate.expire(key, time.getTime(), TimeUnit.SECONDS); } } /** * 设置double类型值 * @param key * @param value * @param time 失效时间(秒) */ public static void setInt(String key, int value, ExpireTime time) { stringRedisTemplate.opsForValue().set(key, String.valueOf(value)); if(time.getTime() > 0){ stringRedisTemplate.expire(key, time.getTime(), TimeUnit.SECONDS); } } /** * 将map写入缓存 * @param key * @param map * @param time 失效时间(秒) */ public static void setMap(String key, Map map, ExpireTime time){ redisTemplate.opsForHash().putAll(key, map); } /** * 将map写入缓存 * @param key * @param map * @param time 失效时间(秒) */ @SuppressWarnings("unchecked") public static void setMap(String key, T obj, ExpireTime time){ Map map = (Map )JsonMapper.parseObject(obj, Map.class); redisTemplate.opsForHash().putAll(key, map); } /** * 向key对应的map中添加缓存对象 * @param key * @param map */ public static void addMap(String key, Map map){ redisTemplate.opsForHash().putAll(key, map); } /** * 向key对应的map中添加缓存对象 * @param key cache对象key * @param field map对应的key * @param value 值 */ public static void addMap(String key, String field, String value){ redisTemplate.opsForHash().put(key, field, value); } /** * 向key对应的map中添加缓存对象 * @param key cache对象key * @param field map对应的key * @param obj 对象 */ public static void addMap(String key, String field, T obj){ redisTemplate.opsForHash().put(key, field, obj); } /** * 获取map缓存 * @param key * @param clazz * @return */ public static Map mget(String key, Class clazz){ BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(key); return boundHashOperations.entries(); } /** * 获取map缓存 * @param key * @param clazz * @return */ public static T getMap(String key, Class clazz){ BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(key); Map map = boundHashOperations.entries(); return JsonMapper.parseObject(map, clazz); } /** * 获取map缓存中的某个对象 * @param key * @param field * @param clazz * @return */ @SuppressWarnings("unchecked") public static T getMapField(String key, String field, Class clazz){ return (T)redisTemplate.boundHashOps(key).get(field); } /** * 删除map中的某个对象 * @author lh * @date 2016年8月10日 * @param key map对应的key * @param field map中该对象的key */ public void delMapField(String key, String... field){ BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(key); boundHashOperations.delete(field); } /** * 指定缓存的失效时间 * * @author FangJun * @date 2016年8月14日 * @param key 缓存KEY * @param time 失效时间(秒) */ public static void expire(String key, ExpireTime time) { if(time.getTime() > 0){ redisTemplate.expire(key, time.getTime(), TimeUnit.SECONDS); } } /** * 添加set * @param key * @param value */ public static void sadd(String key, String... value) { redisTemplate.boundSetOps(key).add(value); } /** * 删除set集合中的对象 * @param key * @param value */ public static void srem(String key, String... value) { redisTemplate.boundSetOps(key).remove(value); } /** * set重命名 * @param oldkey * @param newkey */ public static void srename(String oldkey, String newkey){ redisTemplate.boundSetOps(oldkey).rename(newkey); } /** * 短信缓存 * @author fxl * @date 2016年9月11日 * @param key * @param value * @param time */ public static void setIntForPhone(String key,Object value,int time){ stringRedisTemplate.opsForValue().set(key, JsonMapper.toJsonString(value)); if(time > 0){ stringRedisTemplate.expire(key, time, TimeUnit.SECONDS); } } /** * 模糊查询keys * @param pattern * @return */ public static Set keys(String pattern){ return redisTemplate.keys(pattern); } }
3、JsonMaper
package com.lh.common.util; import java.io.IOException; import java.util.TimeZone; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser.Feature; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.util.JSONPObject; import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule; /** * 简单封装Jackson,实现JSON String<->Java Object的Mapper. * */ public class JsonMapper extends ObjectMapper { private static final long serialVersionUID = 1L; private final static Logger LOGGER = LoggerFactory.getLogger(JsonMapper.class); private static JsonMapper mapper; public JsonMapper() { this(Include.NON_EMPTY); } public JsonMapper(Include include) { // 设置输出时包含属性的风格 if (include != null) { this.setSerializationInclusion(include); } // 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性 this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // 空值处理为空串 this.getSerializerProvider().setNullValueSerializer(new JsonSerializer