ソースを参照

缓存数据更新

skyline 2 年 前
コミット
480d5b6e2f
1 ファイル変更51 行追加14 行削除
  1. 51 14
      service/src/main/java/com/kym/service/utils/KymCache.java

+ 51 - 14
service/src/main/java/com/kym/service/utils/KymCache.java

@@ -10,6 +10,7 @@ import lombok.Data;
 import org.springframework.stereotype.Component;
 
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Collectors;
 
 /**
@@ -21,37 +22,61 @@ import java.util.stream.Collectors;
 @Data
 public class KymCache {
 
-    private static Map<String, String> SHORT_ID_MAPPING;
-    private static Map<String, String> CONNECTOR_STATION_MAPPING;
-    private static Map<String, String> STATION_MAPPING;
-    private static Map<Long, String> ADMIN_USER_STATION;
+    private static ConcurrentHashMap<String, String> SHORT_ID_MAPPING = new ConcurrentHashMap<>();
+    private static ConcurrentHashMap<String, String> CONNECTOR_STATION_MAPPING = new ConcurrentHashMap<>();
+    private static ConcurrentHashMap<String, String> STATION_MAPPING = new ConcurrentHashMap<>();
+    private static ConcurrentHashMap<Long, String> ADMIN_USER_STATION = new ConcurrentHashMap<>();
+    private final EquipmentRelationService relationService;
+    private final StationService stationService;
+    private final AdminUserStationService adminUserStationService;
 
     public KymCache(EquipmentRelationService relationService, StationService stationService, AdminUserStationService adminUserStationService) {
-        SHORT_ID_MAPPING = relationService.list().stream().collect(Collectors.toMap(EquipmentRelation::getEquipmentId, EquipmentRelation::getShortId));
-        CONNECTOR_STATION_MAPPING = relationService.list().stream().collect(Collectors.toMap(EquipmentRelation::getConnectorId, EquipmentRelation::getStationId));
-        STATION_MAPPING = stationService.list().stream().collect(Collectors.toMap(Station::getStationId, Station::getStationName));
-        ADMIN_USER_STATION = adminUserStationService.list().stream().collect(Collectors.toMap(AdminUserStation::getAdminUserId, AdminUserStation::getStationId));
+        this.relationService = relationService;
+        this.stationService = stationService;
+        this.adminUserStationService = adminUserStationService;
+
+        SHORT_ID_MAPPING.putAll(relationService.list().stream().collect(Collectors.toMap(EquipmentRelation::getEquipmentId, EquipmentRelation::getShortId)));
+        CONNECTOR_STATION_MAPPING.putAll(relationService.list().stream().collect(Collectors.toMap(EquipmentRelation::getConnectorId, EquipmentRelation::getStationId)));
+        STATION_MAPPING.putAll(stationService.list().stream().collect(Collectors.toMap(Station::getStationId, Station::getStationName)));
+        ADMIN_USER_STATION.putAll(adminUserStationService.list().stream().collect(Collectors.toMap(AdminUserStation::getAdminUserId, AdminUserStation::getStationId)));
     }
 
     public String getShortId(String equipmentId) {
         if (equipmentId.length() == 17) {
             equipmentId = equipmentId.substring(0, 16);
         }
-        return SHORT_ID_MAPPING.get(equipmentId);
+        var res = SHORT_ID_MAPPING.get(equipmentId);
+        if (res == null) {
+            var relation = relationService.lambdaQuery().eq(EquipmentRelation::getEquipmentId, equipmentId).one();
+            if (relation != null) {
+                res = relation.getShortId();
+                SHORT_ID_MAPPING.put(equipmentId, res);
+            }
+        }
+        return res;
     }
 
     public String getConnectorId(String equipmentId) {
         return switch (equipmentId.length()) {
             case 17 -> equipmentId;
             case 16 -> equipmentId.concat("1");
-            case 6 ->  SHORT_ID_MAPPING.entrySet().stream().filter(entry -> equipmentId.equals(entry.getValue())).map(Map.Entry::getKey).findFirst().get();
+            case 6 ->
+                    SHORT_ID_MAPPING.entrySet().stream().filter(entry -> equipmentId.equals(entry.getValue())).map(Map.Entry::getKey).findFirst().get();
             default -> null;
         };
     }
 
 
     public String getStationId(String connectorId) {
-        return CONNECTOR_STATION_MAPPING.get(connectorId);
+        var res = CONNECTOR_STATION_MAPPING.get(connectorId);
+        if (res == null) {
+            var relation = relationService.lambdaQuery().eq(EquipmentRelation::getConnectorId, connectorId).one();
+            if (relation != null) {
+                res = relation.getStationId();
+                CONNECTOR_STATION_MAPPING.put(connectorId, res);
+            }
+        }
+        return res;
     }
 
     public String getStationNameByConnectorId(String connectorId) {
@@ -60,11 +85,23 @@ public class KymCache {
     }
 
     public String getStationName(String stationId) {
-        return STATION_MAPPING.get(stationId);
+        var res = STATION_MAPPING.get(stationId);
+        if (res == null) {
+            var station = stationService.lambdaQuery().eq(Station::getStationId, stationId).one();
+            if (station != null) {
+                res = station.getStationId();
+                STATION_MAPPING.put(stationId, res);
+            }
+        }
+        return res;
     }
 
     public String getAdminUserStationId(Long adminUserId) {
-        return ADMIN_USER_STATION.get(adminUserId);
+        var res = ADMIN_USER_STATION.get(adminUserId);
+        if (res == null) {
+            var adminUserStation = adminUserStationService.lambdaQuery().eq(AdminUserStation::getAdminUserId, adminUserId).one();
+            res = adminUserStation.getStationId();
+        }
+        return res;
     }
-
 }