Skip to content

Commit 63f9bdd

Browse files
Fix in memory example
1 parent 5ba03ca commit 63f9bdd

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

examples/in_memory/main.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# pyright: reportGeneralTypeIssues=false
2-
from typing import Dict, Optional
2+
from contextlib import asynccontextmanager
3+
from typing import AsyncIterator, Dict, Optional
34

45
import pendulum
56
import uvicorn
@@ -11,7 +12,13 @@
1112
from starlette.requests import Request
1213
from 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

1623
ret = 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-
127129
if __name__ == "__main__":
128130
uvicorn.run("main:app", reload=True)

0 commit comments

Comments
 (0)