diff --git a/server.py b/server.py index c8305a24..e0bc1eee 100644 --- a/server.py +++ b/server.py @@ -41,7 +41,7 @@ def _chk_tcp(host, port, timeout=3): # ── 2026-08-15 温区数据按周期计算 + 缓存(现场温区归因约30秒/请求,缓存后毫秒级)── # 键 = period_tag + strategy_research 最新 created_at(数据更新即失效) -_regime_winrates_cache = {"key": None, "slots": {}} # slots: {period_tag: data} +_regime_winrates_cache = {"slots": {}} # slots: {period_tag: (data_version_key, data)} def _compute_regime_winrates_cached(pt, _approx_univ): @@ -63,11 +63,12 @@ def _compute_regime_winrates_cached(pt, _approx_univ): except Exception as _e: print(f"[REGIME_CACHE] key_err: {_e}", flush=True) _cache_key = None - if _cache_key and _regime_winrates_cache.get("key") == _cache_key \ - and (pt or "2y") in (_regime_winrates_cache.get("slots") or {}): - print(f"[REGIME_CACHE] HIT pt={pt} key={_cache_key} cost={round(_tt.time()-_t0,3)}s", flush=True) - return _regime_winrates_cache["slots"][pt or "2y"] - print(f"[REGIME_CACHE] MISS pt={pt} key={_cache_key} cached_key={_regime_winrates_cache.get('key')} slots={list((_regime_winrates_cache.get('slots') or {}).keys())}", flush=True) + _slots = _regime_winrates_cache.get("slots") or {} + _slot = _slots.get(pt or "2y") + if _cache_key and _slot and _slot[0] == _cache_key: + print(f"[REGIME_CACHE] HIT pt={pt} cost={round(_tt.time()-_t0,3)}s", flush=True) + return _slot[1] + print(f"[REGIME_CACHE] MISS pt={pt} key={_cache_key} slots={list(_slots.keys())}", flush=True) _pt_chain = {'1m': '1y', '6m': '1y', '1y': '1y', '2y': '2y', '5y': '5y', '10y': '10y'} _pt_use = _pt_chain.get(pt or '2y', '2y') @@ -173,8 +174,7 @@ def _compute_regime_winrates_cached(pt, _approx_univ): pass if _cache_key: try: - _regime_winrates_cache["key"] = _cache_key - (_regime_winrates_cache.setdefault("slots", {}))[pt or "2y"] = _regime_winrates + (_regime_winrates_cache.setdefault("slots", {}))[pt or "2y"] = (_cache_key, _regime_winrates) except Exception: pass print(f"[REGIME_CACHE] DONE pt={pt} cost={round(_tt.time()-_t0,1)}s strategies={len(_regime_winrates)}", flush=True)