Cache Plugin 是一个高性能的 AI API 请求缓存解决方案,通过存储和重用相同请求的响应来帮助减少延迟和成本。它支持内存缓存和 Redis,适用于分布式部署。
{
"model": "gpt-4",
"type": 1,
"plugin": {
"cache": {
"enable": true,
"ttl": 300,
"item_max_size": 1048576,
"add_cache_hit_header": true,
"cache_hit_header": "X-Cache-Status"
}
}
}
| 字段 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
enable |
bool | 是 | false | 是否启用 Cache 插件 |
ttl |
int | 否 | 300 | 缓存项的生存时间(秒) |
item_max_size |
int | 否 | 1048576 (1MB) | 单个缓存项的最大大小(字节) |
add_cache_hit_header |
bool | 否 | false | 是否添加指示缓存命中的头部 |
cache_hit_header |
string | 否 | "X-Aiproxy-Cache" | 缓存命中头部的名称 |
插件基于以下内容生成缓存键:
这确保了相同的请求会命中缓存,而不同的请求不会相互干扰。
插件使用两层缓存策略:
请求阶段:
响应阶段:
{
"plugin": {
"cache": {
"enable": true,
"ttl": 60,
"item_max_size": 524288,
"add_cache_hit_header": true
}
}
}
当 add_cache_hit_header 启用时:
缓存命中:
X-Aiproxy-Cache: hit
缓存未命中:
X-Aiproxy-Cache: miss