1. Redis 缓存的基本模式
// 前端:还记得请求缓存吗?
const cache = new Map();
async function fetchUser(id) {
if (cache.has(id)) {
return cache.get(id); // 缓存命中
}
const data = await api.getUser(id); // 查数据库
cache.set(id, data); // 写入缓存
return data;
}
大约 4 分钟
