在高并发的后端服务中,缓存一致性问题一直是核心挑战。本文分享一个基于时间戳与版本号的双层缓存失效策略,已在多个生产环境稳定运行。
核心思路 采用双层机制:时间戳层用于粗粒度失效,版本号层实现细粒度控制。当数据更新时,同时修改时间戳和版本号,确保缓存失效的及时性。
实现方案
public class CacheKey {
private String key;
private Long timestamp;
private Integer version;
public CacheKey(String key, Long timestamp, Integer version) {
this.key = key;
this.timestamp = timestamp;
this.version = version;
}
}
// 更新数据时的逻辑
public void updateData(String id, Data data) {
// 1. 更新数据库
dataMapper.update(data);
// 2. 生成新的时间戳和版本号
Long newTimestamp = System.currentTimeMillis();
Integer newVersion = getAndIncrementVersion(id);
// 3. 更新缓存键
CacheKey cacheKey = new CacheKey(id, newTimestamp, newVersion);
redisTemplate.opsForValue().set(
"cache_key:" + id,
cacheKey,
300, TimeUnit.SECONDS
);
// 4. 删除旧缓存数据
redisTemplate.delete("data:" + id);
}
验证步骤
- 模拟并发请求,同时读取并更新同一数据
- 观察缓存失效时间戳与版本号的同步性
- 验证旧数据是否被正确清除
该策略有效避免了传统双写机制中的数据不一致问题,推荐在高并发场景下使用。

讨论