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

- **ID:** `python/fastapi-dependency-cache-not-cleared`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

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)
   ```

## 无效尝试

- **** — Global state persists across requests; cache not cleared between requests. (80% 失败率)
- **** — FastAPI automatically caches; need to use yield for cleanup. (60% 失败率)
