mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-24 20:47:54 +00:00
Switch from on_event to lifespan asynccontextmanager
on_event is now deprecated, and to be replaced with lifespan: https://fastapi.tiangolo.com/advanced/events/
This commit is contained in:
committed by
GitHub
parent
91ba6d7552
commit
f203d23194
10
README.md
10
README.md
@@ -51,6 +51,9 @@ or
|
|||||||
### Quick Start
|
### Quick Start
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
from collections.abc import AsyncIterator
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
from starlette.responses import Response
|
from starlette.responses import Response
|
||||||
@@ -74,12 +77,11 @@ async def get_cache():
|
|||||||
async def index():
|
async def index():
|
||||||
return dict(hello="world")
|
return dict(hello="world")
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
@app.on_event("startup")
|
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
||||||
async def startup():
|
|
||||||
redis = aioredis.from_url("redis://localhost")
|
redis = aioredis.from_url("redis://localhost")
|
||||||
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
||||||
|
yield
|
||||||
```
|
```
|
||||||
|
|
||||||
### Initialization
|
### Initialization
|
||||||
|
|||||||
Reference in New Issue
Block a user