瀏覽代碼

修改清除redis缓存逻辑

lixing 3 年之前
父節點
當前提交
1a2d5c7f20

+ 51 - 9
src/main/java/com/persagy/apm/diagnose/indicatorrecord/service/impl/MonitorIndicatorRecordServiceImpl.java

@@ -30,6 +30,9 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.Cursor;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.ScanOptions;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.scheduling.annotation.AsyncResult;
 import org.springframework.stereotype.Service;
@@ -85,6 +88,8 @@ public class MonitorIndicatorRecordServiceImpl implements IMonitorIndicatorRecor
     @Autowired
     private RabbitConfig rabbitConfig;
 
+    @Resource(name = "alarmRedisTemplate")
+    private RedisTemplate<Object, Object> redisTemplate;
 
     /**
      * 查询监测指标历史记录
@@ -357,16 +362,53 @@ public class MonitorIndicatorRecordServiceImpl implements IMonitorIndicatorRecor
 
     @Override
     public void cleanAlarmConfigSendTime(CleanAlarmConfigDTO cleanAlarmConfigDTO) {
-        if (StringUtils.isBlank(cleanAlarmConfigDTO.getProjectId())) {
-            //清除所有
-            redisUtil.del(DiAgnoseConst.RedisConstants.PROJECT_ITEM_SEND_TIME);
-        } else if (StringUtils.isBlank(cleanAlarmConfigDTO.getAlarmConfigItemId())) {
-            //清除项目
-            redisUtil.del(DiAgnoseConst.RedisConstants.PROJECT_ALARM_ITEM + ":" + cleanAlarmConfigDTO.getProjectId());
-        } else {
-            //清除指定
-            redisUtil.hdel(DiAgnoseConst.RedisConstants.PROJECT_ALARM_ITEM + ":" + cleanAlarmConfigDTO.getProjectId(), cleanAlarmConfigDTO.getAlarmConfigItemId());
+//        if (StringUtils.isBlank(cleanAlarmConfigDTO.getProjectId())) {
+//            //清除所有
+//            redisUtil.del(DiAgnoseConst.RedisConstants.PROJECT_ITEM_SEND_TIME);
+//        } else if (StringUtils.isBlank(cleanAlarmConfigDTO.getAlarmConfigItemId())) {
+//            //清除项目
+//            redisUtil.del(DiAgnoseConst.RedisConstants.PROJECT_ALARM_ITEM + ":" + cleanAlarmConfigDTO.getProjectId());
+//        } else {
+//            //清除指定
+//            redisUtil.hdel(DiAgnoseConst.RedisConstants.PROJECT_ALARM_ITEM + ":" + cleanAlarmConfigDTO.getProjectId(), cleanAlarmConfigDTO.getAlarmConfigItemId());
+//        }
+        // 清除所有iot数据发送时间
+        clearAllIotDataSendTime();
+        // 删除报警条件状态缓存
+        cleanEqdxAlarmConditionState();
+    }
+
+    /**
+     * 清除所有iot数据发送时间
+     *
+     * @author lixing
+     * @version V1.0 2021/12/18 12:36 下午
+     */
+    private void clearAllIotDataSendTime() {
+        Set<Object> keys = redisTemplate.keys("ALL-PROJECT_ITEM_SEND_TIME*");
+        redisTemplate.delete(keys);
+    }
+
+    /**
+     * 删除报警条件状态缓存
+     *
+     * @author lixing
+     * @version V1.0 2021/12/18 12:31 下午
+     */
+    private void cleanEqdxAlarmConditionState(){
+        Cursor<Map.Entry<Object, Object>> entryCursor = redisTemplate.opsForHash().scan(
+                "ALARM_CONDITION_STATE", ScanOptions.scanOptions()       //绑定模糊查询的hash的key
+                .match("Eqdx*")                                 //模糊查询规则
+                .count(10000).build());
+
+        while(entryCursor.hasNext()){
+            Map.Entry next = entryCursor.next();
+
+            String key = next.getKey().toString();
+
+            redisTemplate.opsForHash().delete("ALARM_CONDITION_STATE", key);
         }
+
     }
 
     private void sendIndicatorDataToAlarmServer(ProjectDTO projectDTO) throws Exception {