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

@@ -12,6 +12,7 @@ from pydantic import BaseModel
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
FastAPICache.init(InMemoryBackend())
@@ -64,7 +65,7 @@ async def get_kwargs(name: str):
@app.get("/sync-me")
@cache(namespace="test")
@cache(namespace="test") # pyright: ignore[reportArgumentType]
def sync_me():
# as per the fastapi docs, this sync function is wrapped in a thread,
# thereby converted to async. fastapi-cache does the same.
@@ -125,7 +126,7 @@ async def cached_put():
@app.get("/namespaced_injection")
@cache(namespace="test", expire=5, injected_dependency_namespace="monty_python")
@cache(namespace="test", expire=5, injected_dependency_namespace="monty_python") # pyright: ignore[reportArgumentType]
def namespaced_injection(
__fastapi_cache_request: int = 42, __fastapi_cache_response: int = 17
) -> Dict[str, int]:

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}