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
|
||||
|
||||
```python
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import Response
|
||||
@@ -74,12 +77,11 @@ async def get_cache():
|
||||
async def index():
|
||||
return dict(hello="world")
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
@asynccontextmanager
|
||||
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
||||
redis = aioredis.from_url("redis://localhost")
|
||||
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
||||
|
||||
yield
|
||||
```
|
||||
|
||||
### Initialization
|
||||
|
||||
Reference in New Issue
Block a user