接口添加缓存策略

This commit is contained in:
2026-08-16 21:41:46 +08:00
parent f192a08384
commit 436a126e3b
3 changed files with 11 additions and 4 deletions

View File

@@ -65,7 +65,8 @@ async def cache_set(key: str, value: Any, ttl: int) -> None:
if c is None: if c is None:
return return
try: try:
await c.set(key, json.dumps(value, ensure_ascii=False), ex=max(1, ttl)) # default=strvalue 里混入 datetime/date 也不炸(炸了会触发 _bail 毒死整个缓存层)
await c.set(key, json.dumps(value, ensure_ascii=False, default=str), ex=max(1, ttl))
except Exception: # noqa: BLE001 except Exception: # noqa: BLE001
_bail() _bail()

View File

@@ -52,8 +52,9 @@ def _f(v) -> float | None:
return None if math.isnan(f) else f return None if math.isnan(f) else f
def _d(v) -> date | None: def _d(v) -> str | None:
return datetime.strptime(str(v), "%Y%m%d").date() if v else None """YYYYMMDD -> 'YYYY-MM-DD'(字符串便于 JSON 缓存pydantic 响应模型自动 coerce"""
return datetime.strptime(str(v), "%Y%m%d").date().isoformat() if v else None
def _get_pro(): def _get_pro():
@@ -152,7 +153,7 @@ async def fetch_overview() -> dict:
errors.append(f"两市统计: {str(e)[:60]}") errors.append(f"两市统计: {str(e)[:60]}")
payload = { payload = {
"updated_at": datetime.now(), "updated_at": datetime.now().isoformat(), # 字符串cache_set 走 json.dumps
"indexes": indexes, "indexes": indexes,
"stats": stats, "stats": stats,
"errors": errors, "errors": errors,

View File

@@ -4,3 +4,8 @@ INFO: Started reloader process [40700] using WatchFiles
INFO: Started server process [42044] INFO: Started server process [42044]
INFO: Waiting for application startup. INFO: Waiting for application startup.
INFO: Application startup complete. INFO: Application startup complete.
INFO: 127.0.0.1:50860 - "GET /api/health HTTP/1.1" 200 OK
INFO: 127.0.0.1:50992 - "GET /api/market/overview HTTP/1.1" 401 Unauthorized
INFO: 127.0.0.1:50994 - "GET /api/market/overview HTTP/1.1" 401 Unauthorized
WARNING: WatchFiles detected changes in 'app\data\market_overview.py'. Reloading...