From 3d685203e77cd8deb2982856ee70f4d51d7ce8db Mon Sep 17 00:00:00 2001
From: lizijie <lzjiiie@163.com>
Date: Fri, 20 May 2022 11:33:42 +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