From 15ce40adc82f9277dc60ed29dad710013e99eca9 Mon Sep 17 00:00:00 2001 From: lizijie <lzjiiie@163.com> Date: Tue, 24 Nov 2020 17:49:56 +0800 Subject: [PATCH] 时间戳截取工具类 --- src/main/java/com/moral/util/RedisUtils.java | 195 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 195 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/moral/util/RedisUtils.java b/src/main/java/com/moral/util/RedisUtils.java new file mode 100644 index 0000000..43122d4 --- /dev/null +++ b/src/main/java/com/moral/util/RedisUtils.java @@ -0,0 +1,195 @@ +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; + } +} -- Gitblit v1.8.0