This commit is contained in:
long2ice
2024-07-24 22:24:30 +08:00
parent 701d83fec3
commit 5f985a8506
8 changed files with 2279 additions and 1520 deletions

View File

@@ -1,6 +1,6 @@
# pyright: reportGeneralTypeIssues=false
from contextlib import asynccontextmanager
import time
from contextlib import asynccontextmanager
from typing import AsyncIterator
import pendulum
@@ -19,6 +19,7 @@ from starlette.responses import JSONResponse, Response
import redis.asyncio as redis
from redis.asyncio.connection import ConnectionPool
@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
pool = ConnectionPool.from_url(url="redis://redis")
@@ -65,7 +66,7 @@ async def get_data(request: Request, response: Response):
# Note: This function MUST be sync to demonstrate fastapi-cache's correct handling,
# i.e. running cached sync functions in threadpool just like FastAPI itself!
@app.get("/blocking")
@cache(namespace="test", expire=10)
@cache(namespace="test", expire=10) # pyright: ignore[reportArgumentType]
def blocking():
time.sleep(2)
return {"ret": 42}