python runtime_error ai_generated true

断言错误:依赖缓存未清除。

AssertionError: Dependency cache is not cleared

ID: python/fastapi-dependency-cache-not-cleared

其他格式: JSON · Markdown 中文 · English
80%修复率
80%置信度
0证据数
2024-10-05首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

FastAPI 缓存每个请求的依赖结果;错误使用全局状态或 yield 依赖可能导致数据过期。

English

FastAPI caches dependency results per request; improper use of global state or yield dependencies can cause stale data.

generic

解决方案

  1. 90% 成功率
    Use yield dependencies properly: def get_db():
        db = Session()
        try:
            yield db
        finally:
            db.close()
  2. 85% 成功率
    Set use_cache=False for dependencies that should not cache: Depends(get_db, use_cache=False)

无效尝试

常见但无效的做法:

  1. 80% 失败

    Global state persists across requests; cache not cleared between requests.

  2. 60% 失败

    FastAPI automatically caches; need to use yield for cleanup.