From 1856f07a4882ce591f668268c17bf2f0d6666579 Mon Sep 17 00:00:00 2001 From: Ou Date: Tue, 8 Oct 2024 22:41:38 +0800 Subject: [PATCH] fix: cache logic --- server/routes/[id].ts | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/server/routes/[id].ts b/server/routes/[id].ts index 7e383fb..1818b38 100644 --- a/server/routes/[id].ts +++ b/server/routes/[id].ts @@ -24,6 +24,20 @@ export default defineEventHandler(async (event): Promise => { // await cacheStore.init() const cache = await cacheStore.get(id) if (cache) { + // interval 刷新间隔,对于缓存失效也要执行的。本质上表示本来内容更新就很慢,这个间隔内可能内容压根不会更新。 + // 默认 10 分钟,是低于 TTL 的,但部分 Source 的间隔会超过 TTL,甚至有的一天刷新一次。 + const interval = sources[id]?.interval ?? Interval + if (now - cache.updated < interval) { + return { + status: "success", + data: { + updatedTime: now, + items: cache.data, + }, + } + } + // 而 TTL 缓存失效时间,在时间范围内,就算内容更新了也要用这个缓存。 + // 复用缓存是不会更新时间的。 if (!latest && now - cache.updated < TTL) { return { status: "cache", @@ -32,18 +46,6 @@ export default defineEventHandler(async (event): Promise => { items: cache.data, }, } - } else if (latest) { - let interval = Interval - if ("interval" in sources[id]) interval = sources[id].interval as number - if (now - cache.updated < interval) { - return { - status: "success", - data: { - updatedTime: now, - items: cache.data, - }, - } - } } } }