Dùng @asynccontextmanager với lifespan parameter thay vì @app.on_event (deprecated).
python
from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup
await database.connect()
redis_pool = await create_redis_pool()
app.state.redis = redis_pool
yield # App đang chạy
# Shutdown
await database.disconnect()
await redis_pool.aclose()
app = FastAPI(lifespan=lifespan)