Browse Source

redis储存数据逻辑

Kute 2 months ago
parent
commit
e602900fa0

+ 24 - 2
ais-tcp/src/main/java/com/shcd/aistcp/service/impl/AisAnalysisServiceImpl.java

@@ -1,14 +1,17 @@
 package com.shcd.aistcp.service.impl;
 
-import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson2.JSON;
 import com.shcd.aistcp.config.RedisCache;
 import com.shcd.aistcp.entity.BoatInfoEntity;
+import com.shcd.aistcp.entity.GHAisInfo;
 import com.shcd.aistcp.service.AisAnalysisService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.util.ObjectUtils;
 
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -24,12 +27,31 @@ public class AisAnalysisServiceImpl implements AisAnalysisService {
     @Autowired
     private RedisCache redisCache;
 
+    @Autowired
+    public RedisTemplate redisTemplate;
+
     @Override
     public void saveAisAnalysis(BoatInfoEntity boatInfoEntity) {
         if (ObjectUtils.isEmpty(boatInfoEntity)){
             log.info("无数据");
             return;
         }
-        redisCache.setCacheObject(boatInfoEntity.getId()+"_zy", JSON.toJSONString(boatInfoEntity),10, TimeUnit.MINUTES);
+        String id = boatInfoEntity.getId();
+        String key = id + "_zy";
+        Object thisData= redisTemplate.opsForValue().get(key);
+        BoatInfoEntity redisBoatInfo = JSON.parseObject((String) thisData, BoatInfoEntity.class);
+        if (ObjectUtils.isEmpty(redisBoatInfo)){
+            redisTemplate.opsForValue().set(key, JSON.toJSONString(boatInfoEntity),10, TimeUnit.MINUTES);
+        }else {
+            List<GHAisInfo> aisData = boatInfoEntity.getAisData();
+            List<GHAisInfo> redisAisData = redisBoatInfo.getAisData();
+            redisAisData.addAll(aisData);
+            redisBoatInfo.setAisData(redisAisData);
+            redisBoatInfo.setPointNum(redisAisData.size());
+            String redisId = redisBoatInfo.getId();
+            key = redisId + "_zy";
+            redisTemplate.opsForValue().set(key, JSON.toJSONString(redisBoatInfo),10, TimeUnit.MINUTES);
+        }
+
     }
 }