feat: do not refetch feed in one minutes

This commit is contained in:
DIYgod
2024-02-27 18:13:54 +08:00
parent 9252753813
commit ea5f509c16
6 changed files with 76 additions and 41 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ import { IPFS_GATEWAY_FALLBACK, SITE_URL } from "~/lib/env"
import { parsePost } from "~/lib/json-feed"
import { NextServerResponse } from "~/lib/server-helper"
import { ExpandedNote } from "~/lib/types"
import { getFeed } from "~/models/home.model"
import { getFeed } from "~/queries/home.server"
export async function GET(request: Request) {
const searchParams = new URLSearchParams(request.url.split("?")[1])
+1 -1
View File
@@ -2,7 +2,7 @@ import { IPFS_GATEWAY_FALLBACK, SITE_URL } from "~/lib/env"
import { parsePost } from "~/lib/json-feed"
import { NextServerResponse } from "~/lib/server-helper"
import { ExpandedNote } from "~/lib/types"
import { getFeed } from "~/models/home.model"
import { getFeed } from "~/queries/home.server"
export async function GET(request: Request) {
const feed = await getFeed({
+1 -1
View File
@@ -1,5 +1,5 @@
import { getQuery, NextServerResponse } from "~/lib/server-helper"
import { getFeed } from "~/models/home.model"
import { getFeed } from "~/queries/home.server"
export async function GET(req: Request) {
const query = getQuery(req)
+36 -38
View File
@@ -34,10 +34,11 @@ export const getRedis = () => redisPromise
export async function cacheGet(options: {
key: string | (Record<string, any> | string | undefined | number)[]
getValueFun: () => Promise<any>
noUpdate?: boolean
noExpire?: boolean
noUpdate?: boolean // do not update when not expired
noExpire?: boolean // do not set expire time
expireTime?: number
allowEmpty?: boolean
durable?: number // update only after this period of time
}) {
const redis = await redisPromise
if (redis && redis.status === "ready") {
@@ -55,20 +56,39 @@ export async function cacheGet(options: {
} catch (error) {
console.error("Redis get error: ", error)
}
const set = (value: string | Record<string, any>) => {
if (value) {
if (options.noExpire) {
redis.set(redisKey, JSON.stringify(value))
} else {
redis.set(
redisKey,
JSON.stringify(value),
"EX",
options.expireTime || REDIS_EXPIRE,
)
}
}
}
if (cacheValue && cacheValue !== "undefined" && cacheValue !== "null") {
if (!options.noUpdate) {
setTimeout(() => {
options.getValueFun().then((value) => {
if (value) {
redis.set(
redisKey,
JSON.stringify(value),
"EX",
options.expireTime || REDIS_EXPIRE,
)
}
})
}, Math.random() * REDIS_REFRESH)
if (options.durable) {
const ttl = await redis.ttl(redisKey)
if (ttl < (options.expireTime || REDIS_EXPIRE) - options.durable) {
set(cacheValue)
options.getValueFun().then((value) => {
set(value)
})
}
} else {
setTimeout(() => {
options.getValueFun().then((value) => {
set(value)
})
}, Math.random() * REDIS_REFRESH)
}
}
try {
return JSON.parse(cacheValue)
@@ -79,34 +99,12 @@ export async function cacheGet(options: {
console.debug("cache miss", redisKey)
if (options.allowEmpty) {
options.getValueFun().then((value) => {
if (value) {
if (options.noExpire) {
redis.set(redisKey, JSON.stringify(value))
} else {
redis.set(
redisKey,
JSON.stringify(value),
"EX",
options.expireTime || REDIS_EXPIRE,
)
}
}
set(value)
})
return null
} else {
const value = await options.getValueFun()
if (value) {
if (options.noExpire) {
redis.set(redisKey, JSON.stringify(value))
} else {
redis.set(
redisKey,
JSON.stringify(value),
"EX",
options.expireTime || REDIS_EXPIRE,
)
}
}
set(value)
return value
}
}
+22
View File
@@ -52,6 +52,7 @@ export async function getFeed({
topic?: string
translateTo?: Language
}) {
console.log("getFeed", type)
if (type === "search" && !searchKeyword) {
type = "latest"
}
@@ -92,6 +93,9 @@ export async function getFeed({
count: 0,
}
const restrictedDate = new Date()
restrictedDate.setMonth(restrictedDate.getMonth() - 2)
switch (type) {
case "latest": {
const result = await client
@@ -106,6 +110,9 @@ export async function getFeed({
deleted: {
equals: false,
},
createdAt: {
gt: "${restrictedDate.toISOString()}",
},
metadata: {
content: {
path: "sources",
@@ -175,6 +182,9 @@ export async function getFeed({
deleted: {
equals: false,
},
createdAt: {
gt: "${restrictedDate.toISOString()}",
},
metadata: {
AND: [{
content: {
@@ -293,6 +303,9 @@ export async function getFeed({
deleted: {
equals: false,
},
createdAt: {
gt: "${restrictedDate.toISOString()}",
},
metadata: {
AND: [{
content: {
@@ -431,6 +444,9 @@ export async function getFeed({
deleted: {
equals: false,
},
createdAt: {
gt: "${restrictedDate.toISOString()}",
},
metadata: {
AND: [
{
@@ -517,6 +533,9 @@ export async function getFeed({
deleted: {
equals: false,
},
createdAt: {
gt: "${restrictedDate.toISOString()}",
},
metadata: {
AND: [
{
@@ -707,6 +726,9 @@ export async function getFeed({
deleted: {
equals: false,
},
createdAt: {
gt: "${restrictedDate.toISOString()}",
},
metadata: {
content: {
path: "sources",
+15
View File
@@ -13,6 +13,7 @@ export const prefetchGetFeed = async (
queryFn: async ({ pageParam }) => {
return cacheGet({
key,
durable: 60,
getValueFun: () =>
homeModel.getFeed({
...data,
@@ -24,9 +25,23 @@ export const prefetchGetFeed = async (
})
}
export const getFeed = async (
data: Parameters<typeof homeModel.getFeed>[0],
) => {
return cacheGet({
key: ["getFeed", data],
durable: 60,
getValueFun: () =>
homeModel.getFeed({
...data,
}),
}) as Promise<ReturnType<typeof homeModel.getFeed>>
}
export const getPreviewShort = async () => {
return cacheGet({
key: "getPreviewShort",
durable: 60,
getValueFun: () =>
homeModel.getFeed({
type: "shorts",