mirror of
https://github.com/ourongxing/newsnow.git
synced 2025-01-19 11:19:14 +08:00
fix: cache logic
This commit is contained in:
parent
556b02bbc0
commit
1856f07a48
@ -24,17 +24,9 @@ export default defineEventHandler(async (event): Promise<SourceResponse> => {
|
|||||||
// await cacheStore.init()
|
// await cacheStore.init()
|
||||||
const cache = await cacheStore.get(id)
|
const cache = await cacheStore.get(id)
|
||||||
if (cache) {
|
if (cache) {
|
||||||
if (!latest && now - cache.updated < TTL) {
|
// interval 刷新间隔,对于缓存失效也要执行的。本质上表示本来内容更新就很慢,这个间隔内可能内容压根不会更新。
|
||||||
return {
|
// 默认 10 分钟,是低于 TTL 的,但部分 Source 的间隔会超过 TTL,甚至有的一天刷新一次。
|
||||||
status: "cache",
|
const interval = sources[id]?.interval ?? Interval
|
||||||
data: {
|
|
||||||
updatedTime: cache.updated,
|
|
||||||
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) {
|
if (now - cache.updated < interval) {
|
||||||
return {
|
return {
|
||||||
status: "success",
|
status: "success",
|
||||||
@ -44,6 +36,16 @@ export default defineEventHandler(async (event): Promise<SourceResponse> => {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 而 TTL 缓存失效时间,在时间范围内,就算内容更新了也要用这个缓存。
|
||||||
|
// 复用缓存是不会更新时间的。
|
||||||
|
if (!latest && now - cache.updated < TTL) {
|
||||||
|
return {
|
||||||
|
status: "cache",
|
||||||
|
data: {
|
||||||
|
updatedTime: cache.updated,
|
||||||
|
items: cache.data,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user