【发布时间】:2022-01-18 09:05:19
【问题描述】:
我正在使用下面的代码行将数据缓存到我的 NextJS 网站上的 Redis 中。但是,Redis 缓存会在 5 分钟后过期。
const getFromCache = async (key, getter) => {
const cached = JSON.parse(await redisGetKey(key))
if (cached) {
return cached
} else {
const data = await getter() // Getter function could be any database call to get data
redisSetEx(key, 5 * 60, JSON.stringify(data))
return data
}
}
Redis缓存不存在,一秒内收到海量请求,如何处理?目前,在这种情况下,所有这些请求都发现 Redis 缓存为空,并被发送到数据库,这会导致大量数据库调用和阻塞整个网络,从而导致大量网关超时错误。
【问题讨论】:
标签: node.js caching redis next.js