File tree Expand file tree Collapse file tree 3 files changed +29
-22
lines changed Expand file tree Collapse file tree 3 files changed +29
-22
lines changed Original file line number Diff line number Diff line change @@ -64,7 +64,15 @@ from fastapi_cache.decorator import cache
6464
6565from redis import asyncio as aioredis
6666
67- app = FastAPI()
67+
68+ @asynccontextmanager
69+ async def lifespan (_ : FastAPI) -> AsyncIterator[None ]:
70+ redis = aioredis.from_url(" redis://localhost" )
71+ FastAPICache.init(RedisBackend(redis), prefix = " fastapi-cache" )
72+ yield
73+
74+
75+ app = FastAPI(lifespan = lifespan)
6876
6977
7078@cache ()
@@ -76,12 +84,6 @@ async def get_cache():
7684@cache (expire = 60 )
7785async def index ():
7886 return dict (hello = " world" )
79-
80- @asynccontextmanager
81- async def lifespan (_ : FastAPI) -> AsyncIterator[None ]:
82- redis = aioredis.from_url(" redis://localhost" )
83- FastAPICache.init(RedisBackend(redis), prefix = " fastapi-cache" )
84- yield
8587```
8688
8789### Initialization
Original file line number Diff line number Diff line change 11# pyright: reportGeneralTypeIssues=false
2- from typing import Dict , Optional
2+ from contextlib import asynccontextmanager
3+ from typing import AsyncIterator , Dict , Optional
34
45import pendulum
56import uvicorn
1112from starlette .requests import Request
1213from starlette .responses import JSONResponse , Response
1314
14- app = FastAPI ()
15+ @asynccontextmanager
16+ async def lifespan (_ : FastAPI ) -> AsyncIterator [None ]:
17+ FastAPICache .init (InMemoryBackend ())
18+ yield
19+
20+
21+ app = FastAPI (lifespan = lifespan )
1522
1623ret = 0
1724
@@ -119,10 +126,5 @@ def namespaced_injection(
119126 }
120127
121128
122- @app .on_event ("startup" )
123- async def startup ():
124- FastAPICache .init (InMemoryBackend ())
125-
126-
127129if __name__ == "__main__" :
128130 uvicorn .run ("main:app" , reload = True )
Original file line number Diff line number Diff line change 11# pyright: reportGeneralTypeIssues=false
2+ from contextlib import asynccontextmanager
23import time
4+ from typing import AsyncIterator
35
46import pendulum
57import uvicorn
1719import redis .asyncio as redis
1820from redis .asyncio .connection import ConnectionPool
1921
20- app = FastAPI ()
22+ @asynccontextmanager
23+ async def lifespan (_ : FastAPI ) -> AsyncIterator [None ]:
24+ pool = ConnectionPool .from_url (url = "redis://redis" )
25+ r = redis .Redis (connection_pool = pool )
26+ FastAPICache .init (RedisBackend (r ), prefix = "fastapi-cache" )
27+ yield
28+
29+
30+ app = FastAPI (lifespan = lifespan )
2131
2232app .mount (
2333 path = "/static" ,
@@ -80,12 +90,5 @@ async def cache_response_obj():
8090 return JSONResponse ({"a" : 1 })
8191
8292
83- @app .on_event ("startup" )
84- async def startup ():
85- pool = ConnectionPool .from_url (url = "redis://redis" )
86- r = redis .Redis (connection_pool = pool )
87- FastAPICache .init (RedisBackend (r ), prefix = "fastapi-cache" )
88-
89-
9093if __name__ == "__main__" :
9194 uvicorn .run ("main:app" , reload = True )
You can’t perform that action at this time.
0 commit comments