File tree Expand file tree Collapse file tree 3 files changed +32
-23
lines changed Expand file tree Collapse file tree 3 files changed +32
-23
lines changed Original file line number Diff line number Diff line change 5151### Quick Start
5252
5353``` python
54+ from collections.abc import AsyncIterator
55+ from contextlib import asynccontextmanager
56+
5457from fastapi import FastAPI
5558from starlette.requests import Request
5659from starlette.responses import Response
@@ -61,7 +64,15 @@ from fastapi_cache.decorator import cache
6164
6265from redis import asyncio as aioredis
6366
64- 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)
6576
6677
6778@cache ()
@@ -73,13 +84,6 @@ async def get_cache():
7384@cache (expire = 60 )
7485async def index ():
7586 return dict (hello = " world" )
76-
77-
78- @app.on_event (" startup" )
79- async def startup ():
80- redis = aioredis.from_url(" redis://localhost" )
81- FastAPICache.init(RedisBackend(redis), prefix = " fastapi-cache" )
82-
8387```
8488
8589### 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
@@ -128,10 +135,5 @@ def namespaced_injection(
128135 }
129136
130137
131- @app .on_event ("startup" )
132- async def startup ():
133- FastAPICache .init (InMemoryBackend ())
134-
135-
136138if __name__ == "__main__" :
137139 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