修复前端长时间挂机内存累积:城市缓存 LRU 逐出 + AI 预测缓存上限

Constraint: cityDetailsByName/citySummariesByName/cityDetailMetaByName 只增不减,52 城全量可达 ~5MB+
Constraint: aiCityForecastStateCache 无上限,随城市×日期组合膨胀
Tested: tsc --noEmit OK, ruff OK, pytest 184 passed
This commit is contained in:
2569718930@qq.com
2026-05-23 21:18:32 +08:00
parent 3bad968844
commit 864b1fa1f9
2 changed files with 52 additions and 0 deletions
@@ -20,6 +20,16 @@ const aiCityForecastStateCache = new Map<
string,
{ state: AiCityForecastState; updatedAt: number }
>();
const MAX_AI_FORECAST_CACHE_SIZE = 40;
function trimAiForecastCache() {
if (aiCityForecastStateCache.size <= MAX_AI_FORECAST_CACHE_SIZE) return;
const excess = aiCityForecastStateCache.size - MAX_AI_FORECAST_CACHE_SIZE;
const keys = Array.from(aiCityForecastStateCache.keys());
for (let i = 0; i < excess && i < keys.length; i++) {
aiCityForecastStateCache.delete(keys[i]);
}
}
function isHkoObservationCity(detail?: CityDetail | null) {
const source = String(
@@ -100,6 +110,7 @@ export function writeCachedAiForecastState(
state,
updatedAt: Date.now(),
});
trimAiForecastCache();
}
export function readReadyCachedAiForecastState(cacheKey: string, refreshToken: number) {