feat: add noExpire and expireTime for cacheGet

This commit is contained in:
DIYgod
2023-07-13 23:29:42 +01:00
parent b0a92e0dba
commit c8eaef0628
6 changed files with 45 additions and 32 deletions
+1
View File
@@ -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()),
+1
View File
@@ -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 () => {
+2 -2
View File
@@ -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 }) => {
}}
></VirtuosoGrid>
{feed.isFetching && feed.hasNextPage && <Loader />}
{feed.isFetching && feed.hasNextPage && !isServerSide && <Loader />}
</div>
)}
</div>
+22 -5
View File
@@ -35,6 +35,8 @@ export async function cacheGet(options: {
key: string | (Record<string, any> | string | undefined | number)[]
getValueFun: () => Promise<any>
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
+1
View File
@@ -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
+18 -25
View File
@@ -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) => {