From 5ba03ca6f585c5c96e15678ed950ef38dfe3bfd4 Mon Sep 17 00:00:00 2001 From: Nik Stuckenbrock Date: Fri, 19 Jul 2024 15:07:23 +0200 Subject: [PATCH 1/3] Correct example in README.md --- README.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 388b17e..77f2f7e 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,15 @@ from fastapi_cache.decorator import cache 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() @@ -76,12 +84,6 @@ async def get_cache(): @cache(expire=60) async def index(): 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 From 63f9bdd068ff2d475e6005386aa3d699b9288099 Mon Sep 17 00:00:00 2001 From: Nik Stuckenbrock Date: Fri, 19 Jul 2024 15:08:43 +0200 Subject: [PATCH 2/3] Fix in memory example --- examples/in_memory/main.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/in_memory/main.py b/examples/in_memory/main.py index ac2c5dc..c43dce3 100644 --- a/examples/in_memory/main.py +++ b/examples/in_memory/main.py @@ -1,5 +1,6 @@ # pyright: reportGeneralTypeIssues=false -from typing import Dict, Optional +from contextlib import asynccontextmanager +from typing import AsyncIterator, Dict, Optional import pendulum import uvicorn @@ -11,7 +12,13 @@ from pydantic import BaseModel from starlette.requests import Request from starlette.responses import JSONResponse, Response -app = FastAPI() +@asynccontextmanager +async def lifespan(_: FastAPI) -> AsyncIterator[None]: + FastAPICache.init(InMemoryBackend()) + yield + + +app = FastAPI(lifespan=lifespan) ret = 0 @@ -119,10 +126,5 @@ def namespaced_injection( } -@app.on_event("startup") -async def startup(): - FastAPICache.init(InMemoryBackend()) - - if __name__ == "__main__": uvicorn.run("main:app", reload=True) From 552d54c1ca98c26e91a395562b02c27793a69fde Mon Sep 17 00:00:00 2001 From: Nik Stuckenbrock Date: Fri, 19 Jul 2024 15:09:39 +0200 Subject: [PATCH 3/3] Fix redis example --- examples/redis/main.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/examples/redis/main.py b/examples/redis/main.py index c9a4f7c..bfae058 100644 --- a/examples/redis/main.py +++ b/examples/redis/main.py @@ -1,5 +1,7 @@ # pyright: reportGeneralTypeIssues=false +from contextlib import asynccontextmanager import time +from typing import AsyncIterator import pendulum import uvicorn @@ -17,7 +19,15 @@ from starlette.responses import JSONResponse, Response import redis.asyncio as redis from redis.asyncio.connection import ConnectionPool -app = FastAPI() +@asynccontextmanager +async def lifespan(_: FastAPI) -> AsyncIterator[None]: + pool = ConnectionPool.from_url(url="redis://redis") + r = redis.Redis(connection_pool=pool) + FastAPICache.init(RedisBackend(r), prefix="fastapi-cache") + yield + + +app = FastAPI(lifespan=lifespan) app.mount( path="/static", @@ -80,12 +90,5 @@ async def cache_response_obj(): return JSONResponse({"a": 1}) -@app.on_event("startup") -async def startup(): - pool = ConnectionPool.from_url(url="redis://redis") - r = redis.Redis(connection_pool=pool) - FastAPICache.init(RedisBackend(r), prefix="fastapi-cache") - - if __name__ == "__main__": uvicorn.run("main:app", reload=True)