From 088d6d943e2669426a1b410e438e9da3a29f1203 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期二, 14 四月 2026 08:57:35 +0800
Subject: [PATCH] #算法耗时优化loadLoopMergeGuardContext增加缓存

---
 src/main/java/com/zy/common/utils/NavigateUtils.java |   40 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 40 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/zy/common/utils/NavigateUtils.java b/src/main/java/com/zy/common/utils/NavigateUtils.java
index 98da343..8d5ef42 100644
--- a/src/main/java/com/zy/common/utils/NavigateUtils.java
+++ b/src/main/java/com/zy/common/utils/NavigateUtils.java
@@ -80,6 +80,9 @@
     private final Object runtimeSnapshotLock = new Object();
     private volatile CachedStationPathRuntimeSnapshot cachedRuntimeSnapshot;
 
+    private final Object loopMergeGuardLock = new Object();
+    private volatile CachedLoopMergeGuardContext cachedLoopMergeGuardContext;
+
     public List<NavigateNode> calcOptimalPathByStationId(Integer startStationId,
                                                          Integer endStationId,
                                                          Integer currentTaskNo,
@@ -1766,6 +1769,23 @@
     }
 
     private LoopMergeGuardContext loadLoopMergeGuardContext() {
+        long now = System.currentTimeMillis();
+        CachedLoopMergeGuardContext cached = cachedLoopMergeGuardContext;
+        if (cached != null && now - cached.cacheAtMs <= STATION_PATH_RUNTIME_SNAPSHOT_TTL_MS) {
+            return new LoopMergeGuardContext(cached.context);
+        }
+        synchronized (loopMergeGuardLock) {
+            cached = cachedLoopMergeGuardContext;
+            if (cached != null && now - cached.cacheAtMs <= STATION_PATH_RUNTIME_SNAPSHOT_TTL_MS) {
+                return new LoopMergeGuardContext(cached.context);
+            }
+            LoopMergeGuardContext context = buildLoopMergeGuardContext();
+            cachedLoopMergeGuardContext = new CachedLoopMergeGuardContext(System.currentTimeMillis(), context);
+            return new LoopMergeGuardContext(context);
+        }
+    }
+
+    private LoopMergeGuardContext buildLoopMergeGuardContext() {
         LoopMergeGuardContext context = new LoopMergeGuardContext();
         try {
             if (stationCycleCapacityService == null) {
@@ -3083,6 +3103,16 @@
         }
     }
 
+    private static class CachedLoopMergeGuardContext {
+        private final long cacheAtMs;
+        private final LoopMergeGuardContext context;
+
+        private CachedLoopMergeGuardContext(long cacheAtMs, LoopMergeGuardContext context) {
+            this.cacheAtMs = cacheAtMs;
+            this.context = context;
+        }
+    }
+
     private static class PathCandidateMetrics {
         private List<NavigateNode> path;
         private int pathLen;
@@ -3136,6 +3166,16 @@
     private static class LoopMergeGuardContext {
         private final Set<Integer> loopStationIdSet = new HashSet<>();
         private final Map<Integer, Set<Integer>> loopNeighborMap = new HashMap<>();
+
+        private LoopMergeGuardContext() {
+        }
+
+        private LoopMergeGuardContext(LoopMergeGuardContext source) {
+            this.loopStationIdSet.addAll(source.loopStationIdSet);
+            for (Map.Entry<Integer, Set<Integer>> entry : source.loopNeighborMap.entrySet()) {
+                this.loopNeighborMap.put(entry.getKey(), new LinkedHashSet<>(entry.getValue()));
+            }
+        }
     }
 
     private static class LoopMergeEntry {

--
Gitblit v1.9.1