Skip to content

Commit 8b5601c

Browse files
authored
Merge branch 'main' into fix-no-cache
2 parents 2739b2d + 00d34e4 commit 8b5601c

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ or
5151
### Quick Start
5252

5353
```python
54+
from collections.abc import AsyncIterator
55+
from contextlib import asynccontextmanager
56+
5457
from fastapi import FastAPI
5558
from starlette.requests import Request
5659
from starlette.responses import Response
@@ -61,7 +64,15 @@ from fastapi_cache.decorator import cache
6164

6265
from 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)
7485
async 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

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

@@ -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-
136138
if __name__ == "__main__":
137139
uvicorn.run("main:app", reload=True)

examples/redis/main.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# pyright: reportGeneralTypeIssues=false
2+
from contextlib import asynccontextmanager
23
import time
4+
from typing import AsyncIterator
35

46
import pendulum
57
import uvicorn
@@ -17,7 +19,15 @@
1719
import redis.asyncio as redis
1820
from 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

2232
app.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-
9093
if __name__ == "__main__":
9194
uvicorn.run("main:app", reload=True)

0 commit comments

Comments
 (0)