From f203d231941f9477ac7e52378fb9614668fc87b9 Mon Sep 17 00:00:00 2001 From: Charles Perrot-Minot <112571330+CharlesPerrotMinotHCHB@users.noreply.github.com> Date: Sun, 12 May 2024 22:57:14 -0700 Subject: [PATCH] Switch from on_event to lifespan asynccontextmanager on_event is now deprecated, and to be replaced with lifespan: https://fastapi.tiangolo.com/advanced/events/ --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9950c69..388b17e 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,9 @@ or ### Quick Start ```python +from collections.abc import AsyncIterator +from contextlib import asynccontextmanager + from fastapi import FastAPI from starlette.requests import Request from starlette.responses import Response @@ -74,12 +77,11 @@ async def get_cache(): async def index(): return dict(hello="world") - -@app.on_event("startup") -async def startup(): +@asynccontextmanager +async def lifespan(_: FastAPI) -> AsyncIterator[None]: redis = aioredis.from_url("redis://localhost") FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache") - + yield ``` ### Initialization