# 运行时错误：依赖缓存键错误

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

## 根因

FastAPI的依赖注入缓存存在冲突，通常是由于在依赖函数中使用了可变默认参数。

## 版本兼容性

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

## 解决方案

1. **Use immutable defaults or None** (90% 成功率)
   ```
   def dependency(items: list = None):
    if items is None:
        items = []
   ```
2. **Clear dependency cache manually** (50% 成功率)
   ```
   from fastapi import Depends
# Not recommended; better to fix dependency function
   ```

## 无效尝试

- **Ignoring mutable defaults in dependencies** — Mutable defaults are shared across requests, causing cache issues. (70% 失败率)
- **Using global variables in dependencies** — Global state can cause cache key collisions. (60% 失败率)
