feat: 交易日9:31清空全部缓存,保证开盘后数据全新

- services/cache.py 新增 clear_all() 清空整个 cache 表
- daily_collector.py 新增 cache_cleanup_loop() 后台循环:精确 sleep 到最近一个
  工作日 9:31 清空缓存,跳过周末,出错1分钟重试
- main.py lifespan 启动清理任务,与采集任务一同优雅退出

与既有"非交易时段 TTL 截止到下次开盘前"的惰性过期形成双保险

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Sakurasan
2026-08-11 16:56:56 +08:00
co-authored by Claude
parent 7fe8074e22
commit eb66ab02d0
3 changed files with 65 additions and 6 deletions
+9 -6
View File
@@ -8,7 +8,7 @@ from dotenv import load_dotenv
from database import init_db
from routes import stock, collections, shares, sectors, themes, core_stocks
from services.daily_collector import collector_loop
from services.daily_collector import collector_loop, cache_cleanup_loop
load_dotenv()
@@ -17,14 +17,17 @@ load_dotenv()
async def lifespan(app: FastAPI):
init_db()
collector_task = asyncio.create_task(collector_loop())
cache_cleanup_task = asyncio.create_task(cache_cleanup_loop())
try:
yield
finally:
collector_task.cancel()
try:
await collector_task
except asyncio.CancelledError:
pass
for t in (collector_task, cache_cleanup_task):
t.cancel()
for t in (collector_task, cache_cleanup_task):
try:
await t
except asyncio.CancelledError:
pass
app = FastAPI(title="AUV API", version="1.0.0", lifespan=lifespan)