File tree Expand file tree Collapse file tree 1 file changed +11
-8
lines changed Expand file tree Collapse file tree 1 file changed +11
-8
lines changed 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