request_cache 是指对一次完整 Request 的结果做缓存,它和传统的 数据缓存(data cache) 有一个本质区别:
**数据缓存缓存的是数据;request_cache缓存的是请求 → 响应结果。特别适合微服务、API Gateway、LLM、搜索、推荐、复杂查询等场景。
1. Request Cache是什么
典型模型:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Request │ ▼ ┌─────────────────┐ │ Request Cache │ │ key → response │ └────────┬────────┘ │ ┌────┴─────┐ │ │ Hit Miss │ │ ▼ ▼ Response Backend │ ▼ DB / RPC / API │ ▼ 写入 Cache
|
它解决的不是数据库慢而是重复计算
传统 Cache:
1 2 3 4 5
| User ID ↓ Redis ↓ User Object
|
Request Cache:
1 2 3 4 5
| 完整 Request ↓ Redis ↓ 完整 Response
|
2.L1 + L2 + Backend
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| ┌─────────────┐ │ Client │ └──────┬──────┘ │ ▼ ┌─────────────────┐ │ API Gateway │ └────────┬────────┘ │ ▼ ┌───────────────────────┐ │ Application │ │ │ │ Request Cache │ │ │ │ │ ├── L1 Local │ │ │ │ │ └── L2 Redis │ └───────────┬───────────┘ │ miss ▼ ┌──────────────────┐ │ Business Service │ └────────┬─────────┘ │ ┌──────┴──────┐ ▼ ▼ DB RPC
|
3.相对稳定读缓存架构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| Request │ ▼ Build Cache Key │ ▼ ┌─────────────┐ │ L1 Cache │ └──────┬──────┘ │ miss ▼ ┌─────────────┐ │ L2 Redis │ └──────┬──────┘ │ miss ▼ ┌─────────────┐ │ SingleFlight│ └──────┬──────┘ │ ▼ Backend │ ┌─────────┼─────────┐ │ │ │ 200 404 Error │ │ ▼ ▼ Cache Negative │ Cache └────┬────┘ ▼ Response
|