三种缓存策略(续)
// ✅ 推荐:定时重新验证
const res = await fetch("https://api.example.com/posts", {
next: { revalidate: 3600 }, // 每小时更新一次
});
效果:类似 ISR(增量静态再生成)
sequenceDiagram
participant U1 as 用户1<br/>(0秒)
participant U2 as 用户2<br/>(1800秒)
participant U3 as 用户3<br/>(3601秒)
participant Cache as 缓存
participant API as API
U1->>Cache: 请求数据
Cache->>API: 缓存不存在,请求 API
API-->>Cache: 返回数据
Cache-->>U1: 返回数据
U2->>Cache: 请求数据
Cache-->>U2: 返回缓存(未过期)
U3->>Cache: 请求数据
Note over Cache: 缓存已过期(>3600秒)
Cache->>API: 重新请求 API
API-->>Cache: 返回新数据
Cache-->>U3: 返回新数据
适用场景:
- ✅ 新闻列表(每小时更新)
- ✅ 产品列表(每天更新)
- ✅ 评论列表(每分钟更新)