Files
auv/backend/services/cache.py
T
SakurasanandClaude 548ebee47f perf: 优化热点穿透加载速度 + Canvas 渲染 + 触控板/手机交互
后端加载优化(盘中原本每次访问都重新聚合,耗时 10-30 秒):
- 图聚合结果与题材股票子层盘中加 60 秒缓存
- 缓存过期时返回旧数据并后台幂等重建(stale-while-revalidate),打开即秒开
- 缓存基础支持秒级 TTL(set_cache 新增 ttl_seconds)
- 响应体瘦身:移除 edges(前端从 stocks[].themeCodes 重建)、
  themes 精简字段、stocks 按 limit 裁剪,JSON 从数 MB 降至数百 KB

前端 Canvas 渲染重构:
- d3-force 布局保留,SVG 渲染层替换为 Canvas 双缓冲(静止态离屏层 drawImage)
- tick 由每帧 setState 改为 rAF 合帧,拖拽/缩放期间零 React 重渲染,800 节点流畅

交互优化:
- 手机:新增 +/−/适应 缩放按钮、命中半径放大至 22px、tap/drag 6px 阈值区分、
  双指捏合缩放、触屏禁用 hover 避免与选中冲突
- Mac 触控板:双指滚动=平移、捏合(ctrlKey)=缩放、滚轮缩放保留、点空白取消选中

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-07 13:21:08 +08:00

59 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""磁盘缓存:SQLite 持久化,重启不丢"""
import json
from datetime import datetime, timedelta
from typing import Optional
from database import get_connection
def get_cache(key: str) -> Optional[str]:
"""获取缓存,过期或不存在返回 None"""
now = datetime.now().isoformat()
conn = get_connection()
try:
row = conn.execute(
"SELECT value FROM cache WHERE key = ? AND expires_at > ?",
(key, now),
).fetchone()
if row:
return row["value"]
return None
finally:
conn.close()
def set_cache(key: str, value: str, ttl_hours: int = 6, ttl_seconds: int = 0):
"""写入缓存,过期时间 = now + ttl_hours + ttl_seconds(支持秒级短 TTL"""
expires_at = (datetime.now() + timedelta(hours=ttl_hours, seconds=ttl_seconds)).isoformat()
conn = get_connection()
try:
conn.execute(
"INSERT OR REPLACE INTO cache (key, value, expires_at) VALUES (?, ?, ?)",
(key, value, expires_at),
)
conn.commit()
finally:
conn.close()
def del_cache(key: str):
"""删除指定缓存"""
conn = get_connection()
try:
conn.execute("DELETE FROM cache WHERE key = ?", (key,))
conn.commit()
finally:
conn.close()
def clean_expired():
"""清理已过期的缓存(可在启动时调用一次)"""
now = datetime.now().isoformat()
conn = get_connection()
try:
conn.execute("DELETE FROM cache WHERE expires_at <= ?", (now,))
conn.commit()
finally:
conn.close()