接口添加缓存策略
This commit is contained in:
@@ -65,7 +65,8 @@ async def cache_set(key: str, value: Any, ttl: int) -> None:
|
||||
if c is None:
|
||||
return
|
||||
try:
|
||||
await c.set(key, json.dumps(value, ensure_ascii=False), ex=max(1, ttl))
|
||||
# default=str:value 里混入 datetime/date 也不炸(炸了会触发 _bail 毒死整个缓存层)
|
||||
await c.set(key, json.dumps(value, ensure_ascii=False, default=str), ex=max(1, ttl))
|
||||
except Exception: # noqa: BLE001
|
||||
_bail()
|
||||
|
||||
|
||||
@@ -52,8 +52,9 @@ def _f(v) -> float | None:
|
||||
return None if math.isnan(f) else f
|
||||
|
||||
|
||||
def _d(v) -> date | None:
|
||||
return datetime.strptime(str(v), "%Y%m%d").date() if v else None
|
||||
def _d(v) -> str | 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():
|
||||
@@ -152,7 +153,7 @@ async def fetch_overview() -> dict:
|
||||
errors.append(f"两市统计: {str(e)[:60]}")
|
||||
|
||||
payload = {
|
||||
"updated_at": datetime.now(),
|
||||
"updated_at": datetime.now().isoformat(), # 字符串:cache_set 走 json.dumps
|
||||
"indexes": indexes,
|
||||
"stats": stats,
|
||||
"errors": errors,
|
||||
|
||||
@@ -4,3 +4,8 @@ INFO: Started reloader process [40700] using WatchFiles
|
||||
INFO: Started server process [42044]
|
||||
INFO: Waiting for application startup.
|
||||
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...
|
||||
|
||||
Reference in New Issue
Block a user