首页 > 其他分享 >[FastAPI-32]依赖注入缓存

[FastAPI-32]依赖注入缓存

时间:2023-03-26 21:04:40浏览次数:48  
标签:缓存 num1 依赖 get FastAPI num 32

from fastapi import Depends, FastAPI

app = FastAPI()

'''
依赖注入缓存现象
- 依赖条件`get_num`被依赖了两次,但是你会发现其内部打印语句只打印了一次。也就是说,
第二次使用这个依赖条件时FastAPI并没有真正执行这个函数,而是直接使用了第一次执行的结果,这就是依赖注入的缓存现象。
- 禁用缓存 use_cache=False
'''

def get_num(num: int):
    print("get_num被执行了")
    return num


@app.get("/")
def get_results(num1: int = Depends(get_num), num2: int = Depends(get_num,use_cache=False)):
    return {"num1": num1, "num2": num2}

标签:缓存,num1,依赖,get,FastAPI,num,32
From: https://www.cnblogs.com/leoshi/p/17259505.html

相关文章

  • [FastAPI-31]嵌套注入
    fromtypingimportUnionfromfastapiimportDepends,FastAPIapp=FastAPI()'''嵌套注入-路径函数get_name需要的形参`username_or_nickname`有依赖条件,所以F......
  • [FastAPI=30]依赖注入
    fromfastapiimportFastAPI,Dependsapp=FastAPI(title="依赖注入")'''依赖注入-共享一块相同逻辑的代码块-共享数据库连接-权限认证,登录状态认证'''BOO......
  • [FastAPI-29]用户注册API-File字段需要在 Form之前
    importtypingfromfastapiimportFastAPI,Form,File,UploadFilefrompydanticimportBaseModelapp=FastAPI(title="注册接口")'''1.需要输入账号密码头......
  • [FastAPI-28]上传多个文件
    importtypingfromfastapiimportFastAPI,File,UploadFileapp=FastAPI(title="Form表单")'''上传多个文件'''@app.post("/files",summary="通过内存缓存上......
  • [FastAPI-27]上传文件为可选项
    importtypingfromfastapiimportFastAPI,File,UploadFileapp=FastAPI(title="Form表单")'''上传文件为可选项'''@app.post("/upload_large_file",summa......
  • Vmware UEFI装Windows8 Image type X64 is not support by this IA32 shell
      挂载ISO后,跟着启动虚拟机,会见到显示"unsuccessfulEFInetwork"  输入bootiaX86.EFI;回车 "ImagetypeIA32isnotsupportedbythisX64shell"或者:......
  • STM32移植FreeRTOS
    前言以前在学校做项目的时候,无论是智能车还是电赛,写代码有个习惯,就是把不同的功能的函数,都写成一个函数接口,最后全部在主函数里创建个while循环反复调用。intmain(void)......
  • LeetCode|1032. 字符流
    题目链接:1032.字符流设计一个算法:接收一个字符流,并检查这些字符的后缀是否是字符串数组words中的一个字符串。例如,words=["abc","xyz"]且字符流中逐个依次加入......
  • 产品原型1-20230320
          ......
  • 产品原型2-20230321
          ......