Skip to content

Commit 00d34e4

Browse files
authored
Merge pull request #433 from nikstuckenbrock/feature/update-project-docs
Update project docs and examples
2 parents 865dba1 + 552d54c commit 00d34e4

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@ from fastapi_cache.decorator import cache
6464

6565
from 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)
7785
async 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

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)

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)