添加缓存预热

This commit is contained in:
2026-05-26 01:38:43 +08:00
parent 6dd12aa293
commit c1ad2bdc19
9 changed files with 513 additions and 177 deletions
+39 -14
View File
@@ -1,13 +1,44 @@
from config.main import config
from api.main import api_router
from fastapi import FastAPI
from multiprocessing import freeze_support
from api.core import Exceptions,Middleware,Gzip
import uvicorn
import asyncio
from contextlib import asynccontextmanager, suppress
import os
import time
from multiprocessing import freeze_support
application = FastAPI(docs_url='/doc', redoc_url=None)
import uvicorn
from fastapi import FastAPI
from api.core import Exceptions, Gzip, Middleware
from api.core.RedisPreheat import preheat_redis_cache
from api.main import api_router
from config.main import config
async def _run_redis_preheat_background():
print('Redis 缓存预热中(后台)...')
start = time.perf_counter()
try:
warmed_keys = await asyncio.to_thread(preheat_redis_cache)
duration = time.perf_counter() - start
print(f'Redis 缓存预热完成: {len(warmed_keys)} keys, {duration:.2f}s')
except Exception as exc:
print(f'Redis 缓存预热失败: {exc}')
@asynccontextmanager
async def lifespan(app: FastAPI):
print('dcalc is starting...')
print('Application starting...')
app.state.redis_preheat_task = asyncio.create_task(_run_redis_preheat_background())
print('Application startup complete.')
yield
preheat_task = getattr(app.state, 'redis_preheat_task', None)
if preheat_task and not preheat_task.done():
preheat_task.cancel()
with suppress(asyncio.CancelledError):
await preheat_task
application = FastAPI(docs_url='/doc', redoc_url=None, lifespan=lifespan)
application.add_exception_handler(Exception, Exceptions.all_exception_handler)
Middleware.register_cors(application)
Gzip.register_gzip_response(application)
@@ -17,13 +48,7 @@ application.include_router(api_router)
if __name__ == '__main__':
freeze_support()
def on_startup():
print("dcalc is starting...")
return print("Application starting...")
os.environ['TZ'] = 'Asia/Shanghai'
if os.name != 'nt': # Only call tzset on Unix-like systems, not on Windows
time.tzset()
uvicorn.run(app = "main:application", host='0.0.0.0', port=config.PORT, workers=config.WORKERS,reload=False)
uvicorn.run(app='main:application', host='0.0.0.0', port=config.PORT, workers=config.WORKERS, reload=False)