diff --git a/src/app/api/image/route.ts b/src/app/api/image/route.ts index 31308c2a..791e8453 100644 --- a/src/app/api/image/route.ts +++ b/src/app/api/image/route.ts @@ -7,6 +7,7 @@ const getImage = async (src: string) => { return cacheGet({ key: ["getImage", src], noUpdate: true, + noExpire: true, getValueFun: async () => { const buffer = await fetch(src).then(async (res) => Buffer.from(await res.arrayBuffer()), diff --git a/src/app/api/score/route.ts b/src/app/api/score/route.ts index 6746cae6..6270d996 100644 --- a/src/app/api/score/route.ts +++ b/src/app/api/score/route.ts @@ -69,6 +69,7 @@ async function getScore(cid: string) { key: ["summary_score", cid], allowEmpty: true, noUpdate: true, + noExpire: true, getValueFun: async () => { let resut await lock.acquire(cid, async () => { diff --git a/src/components/home/HomeFeed.tsx b/src/components/home/HomeFeed.tsx index 5cde321b..c2129726 100644 --- a/src/components/home/HomeFeed.tsx +++ b/src/components/home/HomeFeed.tsx @@ -24,7 +24,7 @@ import { getSiteLink } from "~/lib/helpers" import { useTranslation } from "~/lib/i18n/client" import { getStorage, setStorage } from "~/lib/storage" import { ExpandedNote } from "~/lib/types" -import { cn, getStringLength } from "~/lib/utils" +import { cn, getStringLength, isServerSide } from "~/lib/utils" import type { FeedType, SearchType } from "~/models/home.model" import { useGetFeed } from "~/queries/home" @@ -412,7 +412,7 @@ export const HomeFeed = ({ type }: { type?: FeedType }) => { }} > - {feed.isFetching && feed.hasNextPage && } + {feed.isFetching && feed.hasNextPage && !isServerSide && } )} diff --git a/src/lib/redis.server.ts b/src/lib/redis.server.ts index c6c84c05..022892e1 100644 --- a/src/lib/redis.server.ts +++ b/src/lib/redis.server.ts @@ -35,6 +35,8 @@ export async function cacheGet(options: { key: string | (Record | string | undefined | number)[] getValueFun: () => Promise noUpdate?: boolean + noExpire?: boolean + expireTime?: number allowEmpty?: boolean }) { const redis = await redisPromise @@ -52,7 +54,12 @@ export async function cacheGet(options: { if (!options.noUpdate) { options.getValueFun().then((value) => { if (value) { - redis.set(redisKey, JSON.stringify(value), "EX", REDIS_EXPIRE) + redis.set( + redisKey, + JSON.stringify(value), + "EX", + options.expireTime || REDIS_EXPIRE, + ) } }) } @@ -61,10 +68,15 @@ export async function cacheGet(options: { if (options.allowEmpty) { options.getValueFun().then((value) => { if (value) { - if (options.noUpdate) { + if (options.noExpire) { redis.set(redisKey, JSON.stringify(value)) } else { - redis.set(redisKey, JSON.stringify(value), "EX", REDIS_EXPIRE) + redis.set( + redisKey, + JSON.stringify(value), + "EX", + options.expireTime || REDIS_EXPIRE, + ) } } }) @@ -72,10 +84,15 @@ export async function cacheGet(options: { } else { const value = await options.getValueFun() if (value) { - if (options.noUpdate) { + if (options.noExpire) { redis.set(redisKey, JSON.stringify(value)) } else { - redis.set(redisKey, JSON.stringify(value), "EX", REDIS_EXPIRE) + redis.set( + redisKey, + JSON.stringify(value), + "EX", + options.expireTime || REDIS_EXPIRE, + ) } } return value diff --git a/src/queries/page.server.ts b/src/queries/page.server.ts index 6969d49a..2100a50a 100644 --- a/src/queries/page.server.ts +++ b/src/queries/page.server.ts @@ -147,6 +147,7 @@ export async function getSummary({ key: ["summary", cid, lang], allowEmpty: true, noUpdate: true, + noExpire: true, getValueFun: async () => { if (["en", "zh", "zh-TW", "ja"].includes(lang)) { let result diff --git a/src/queries/site.server.ts b/src/queries/site.server.ts index 4ba00d1a..83c7f070 100644 --- a/src/queries/site.server.ts +++ b/src/queries/site.server.ts @@ -4,7 +4,7 @@ import { QueryClient } from "@tanstack/react-query" import { getAverageColor } from "~/lib/fast-average-color-node" import { toGateway } from "~/lib/ipfs-parser" -import { cacheGet, getRedis } from "~/lib/redis.server" +import { cacheGet } from "~/lib/redis.server" import { ExpandedCharacter } from "~/lib/types" import * as siteModel from "~/models/site.model" @@ -114,31 +114,24 @@ export const getNFTs = async (address?: string) => { return null } - const redis = await getRedis() - const redisKey = `nfts/${address}` - - let cache - try { - cache = await redis?.get(redisKey) - } catch (error) {} - if (cache) { - return JSON.parse(cache) - } else { - const result = await ( - await fetch( - `https://restapi.nftscan.com/api/v2/assets/chain/${address}?erc_type=&chain=eth;bnb;polygon;arbitrum;optimism;avax;cronos;platon;moonbeam;fantom;gnosis`, - { - headers: { - "X-API-KEY": NFTSCAN_API_KEY, + return cacheGet({ + key: `nfts/${address}`, + noUpdate: true, + expireTime: 60 * 60 * 24, + getValueFun: async () => { + const result = await ( + await fetch( + `https://restapi.nftscan.com/api/v2/assets/chain/${address}?erc_type=&chain=eth;bnb;polygon;arbitrum;optimism;avax;cronos;platon;moonbeam;fantom;gnosis`, + { + headers: { + "X-API-KEY": NFTSCAN_API_KEY, + }, }, - }, - ) - ).json() - if (result.data) { - redis?.set(redisKey, JSON.stringify(result.data), "EX", 60 * 60 * 24) - } - return result.data - } + ) + ).json() + return result.data + }, + }) } export const getCharacterColors = async (character?: ExpandedCharacter) => {