| New file | 
|  |  |  | 
|---|
|  |  |  | package com.moral.util; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.alibaba.fastjson.TypeReference; | 
|---|
|  |  |  | import com.moral.common.BooleanValueFilter; | 
|---|
|  |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
|---|
|  |  |  | import org.springframework.data.redis.core.ValueOperations; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import java.io.Serializable; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Set; | 
|---|
|  |  |  | import java.util.concurrent.TimeUnit; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Component | 
|---|
|  |  |  | public class RedisUtils { | 
|---|
|  |  |  | private static final String AlarmPrefix = "alarm_"; | 
|---|
|  |  |  | public static String getAlarmKey(int organizationId){ | 
|---|
|  |  |  | return AlarmPrefix+organizationId; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | private static final String Adjust = "adjust_"; | 
|---|
|  |  |  | public static String getAdjustKey(String mac){ | 
|---|
|  |  |  | return Adjust + mac; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private RedisTemplate redisTemplate; | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private BooleanValueFilter javaTypePatchValueFilter; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 批量删除对应的value | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param keys | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void remove(final String... keys) { | 
|---|
|  |  |  | for (String key : keys) { | 
|---|
|  |  |  | remove(key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 批量删除key | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param pattern | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void removePattern(final String pattern) { | 
|---|
|  |  |  | Set<Serializable> keys = redisTemplate.keys(pattern); | 
|---|
|  |  |  | if (keys.size() > 0) | 
|---|
|  |  |  | redisTemplate.delete(keys); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 删除对应的value | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void remove(final String key) { | 
|---|
|  |  |  | if (exists(key)) { | 
|---|
|  |  |  | redisTemplate.delete(key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 判断缓存中是否有对应的value | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean exists(final String key) { | 
|---|
|  |  |  | return redisTemplate.hasKey(key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 读取缓存 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Object get(final String key) { | 
|---|
|  |  |  | Object result = null; | 
|---|
|  |  |  | ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); | 
|---|
|  |  |  | result = operations.get(key); | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 读取缓存,返回指定类型 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public  <T>T get(String key,Class<T> clazz) { | 
|---|
|  |  |  | Object obj = get(key); | 
|---|
|  |  |  | String json = obj!=null?obj.toString():null; | 
|---|
|  |  |  | if(json!=null) { | 
|---|
|  |  |  | return (T) parseObject(json,clazz); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 读取缓存,返回指定类型 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public  <T>T get(String key,TypeReference typeReference) { | 
|---|
|  |  |  | Object obj = get(key); | 
|---|
|  |  |  | String json = obj!=null?obj.toString():null; | 
|---|
|  |  |  | if(json!=null) { | 
|---|
|  |  |  | return (T) parseObject(json,typeReference); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | private static <T>T parseObject(String key,Class<T> clazz) { | 
|---|
|  |  |  | return (T) JSON.parseObject(key,clazz); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | private static <T>T parseObject(String key,TypeReference typeReference) { | 
|---|
|  |  |  | return (T) JSON.parseObject(key,typeReference); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public <T> List<T> getList(String pattern,TypeReference typeReference){ | 
|---|
|  |  |  | List<String> jsonList = getList(pattern); | 
|---|
|  |  |  | List<T>  list = new ArrayList<T>(); | 
|---|
|  |  |  | for(String json:jsonList) { | 
|---|
|  |  |  | list.add((T)parseObject(json,typeReference)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return list; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public <T> List<T> getList(String pattern,Class<T> clazz){ | 
|---|
|  |  |  | List<String> jsonList = getList(pattern); | 
|---|
|  |  |  | List<T>  list = new ArrayList<>(); | 
|---|
|  |  |  | for(String json:jsonList) { | 
|---|
|  |  |  | list.add(parseObject(json,clazz)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return list; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public List<String> getList(String pattern){ | 
|---|
|  |  |  | Set<String> keys = keys(pattern); | 
|---|
|  |  |  | List<String> list = new ArrayList<String>(); | 
|---|
|  |  |  | for(String key:keys) { | 
|---|
|  |  |  | Object obj = get(key); | 
|---|
|  |  |  | String json = obj!=null?obj.toString():null; | 
|---|
|  |  |  | if(json!=null) { | 
|---|
|  |  |  | list.add(json); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return list; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public Set<String> keys(String pattern) { | 
|---|
|  |  |  | return redisTemplate.keys(pattern); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 写入缓存 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean set(final String key, Object value) { | 
|---|
|  |  |  | boolean result = false; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if(value!=null&&!(value instanceof String)) { | 
|---|
|  |  |  | value = JSON.toJSONString(value,new BooleanValueFilter()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); | 
|---|
|  |  |  | operations.set(key, value); | 
|---|
|  |  |  | result = true; | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 写入缓存 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean set(final String key, Object value, Long expireTime) { | 
|---|
|  |  |  | boolean result = false; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if(value!=null&&!(value instanceof String)) { | 
|---|
|  |  |  | value = JSON.toJSONString(value, javaTypePatchValueFilter); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); | 
|---|
|  |  |  | operations.set(key, value); | 
|---|
|  |  |  | redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); | 
|---|
|  |  |  | result = true; | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|