Correct example in README.md

This commit is contained in:
Nik Stuckenbrock
2024-07-19 15:07:23 +02:00
parent 865dba19a5
commit 5ba03ca6f5

View File

@@ -64,7 +64,15 @@ from fastapi_cache.decorator import cache
from redis import asyncio as aioredis from redis import asyncio as aioredis
app = FastAPI()
@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
redis = aioredis.from_url("redis://localhost")
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
yield
app = FastAPI(lifespan=lifespan)
@cache() @cache()
@@ -76,12 +84,6 @@ async def get_cache():
@cache(expire=60) @cache(expire=60)
async def index(): async def index():
return dict(hello="world") return dict(hello="world")
@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
redis = aioredis.from_url("redis://localhost")
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
yield
``` ```
### Initialization ### Initialization