from typing import Optional
from fastapi import APIRouter, Depends
app11 = APIRouter()
"""Sub-dependencies 子依赖"""
def query(q: Optional[str] = None):
return q
def sub_query(q: str = Depends(query), last_query: Optional[str] = None):
if not q:
return last_query
return q
@app11.get("/sub_dependency")
async def sub_dependency(final_query: str = Depends(sub_query, use_cache=True)):
"""use_cache默认是True, 表示当多个依赖有一个共同的子依赖时,每次request请求只会调用子依赖一次,多次调用将从缓存中获取"""
return {"sub_dependency": final_query}
FastAPI从入门到实战(11)——子依赖
最新推荐文章于 2024-04-16 01:15:34 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)