mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-24 20:47:54 +00:00
Fix redis example
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
# pyright: reportGeneralTypeIssues=false
|
# pyright: reportGeneralTypeIssues=false
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
import time
|
import time
|
||||||
|
from typing import AsyncIterator
|
||||||
|
|
||||||
import pendulum
|
import pendulum
|
||||||
import uvicorn
|
import uvicorn
|
||||||
@@ -17,7 +19,15 @@ from starlette.responses import JSONResponse, Response
|
|||||||
import redis.asyncio as redis
|
import redis.asyncio as redis
|
||||||
from redis.asyncio.connection import ConnectionPool
|
from redis.asyncio.connection import ConnectionPool
|
||||||
|
|
||||||
app = FastAPI()
|
@asynccontextmanager
|
||||||
|
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
||||||
|
pool = ConnectionPool.from_url(url="redis://redis")
|
||||||
|
r = redis.Redis(connection_pool=pool)
|
||||||
|
FastAPICache.init(RedisBackend(r), prefix="fastapi-cache")
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
|
||||||
app.mount(
|
app.mount(
|
||||||
path="/static",
|
path="/static",
|
||||||
@@ -80,12 +90,5 @@ async def cache_response_obj():
|
|||||||
return JSONResponse({"a": 1})
|
return JSONResponse({"a": 1})
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("startup")
|
|
||||||
async def startup():
|
|
||||||
pool = ConnectionPool.from_url(url="redis://redis")
|
|
||||||
r = redis.Redis(connection_pool=pool)
|
|
||||||
FastAPICache.init(RedisBackend(r), prefix="fastapi-cache")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
uvicorn.run("main:app", reload=True)
|
uvicorn.run("main:app", reload=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user