feat: optimize cache

This commit is contained in:
DIYgod 2023-07-22 13:44:53 +01:00
parent 76bfc8d21a
commit b0b9b7c55b
No known key found for this signature in database
2 changed files with 16 additions and 25 deletions

View File

@ -10,21 +10,10 @@ const withPWA = require("@ducanh2912/next-pwa").default({
disable: process.env.NODE_ENV === "development",
dest: "public",
publicExcludes: ["*"],
buildExcludes: [
/\.map$/,
/^manifest.*\.js$/,
/\/chunks\/app\/dashboard/,
/\/chunks\/app\/\(home\)/,
],
buildExcludes: [/\.map$/, /^manifest.*\.js$/, /\/chunks\/app\/dashboard/],
extendDefaultRuntimeCaching: true,
workboxOptions: {
runtimeCaching: [
{
urlPattern: ({ request }) => {
return request.headers.get("x-middleware-prefetch")
},
handler: "NetworkOnly",
},
{
urlPattern: ({ url }) => {
return /\/ipfs\/([^/?#]+)$/.test(url.toString())
@ -33,7 +22,7 @@ const withPWA = require("@ducanh2912/next-pwa").default({
options: {
cacheName: "next-ipfs",
expiration: {
maxEntries: 64,
maxEntries: 200,
maxAgeSeconds: 365 * 24 * 60 * 60, // 365 days
},
cacheableResponse: {
@ -48,7 +37,7 @@ const withPWA = require("@ducanh2912/next-pwa").default({
options: {
cacheName: "next-ipfs",
expiration: {
maxEntries: 64,
maxEntries: 200,
maxAgeSeconds: 365 * 24 * 60 * 60, // 365 days
},
},

View File

@ -1,6 +1,6 @@
import Redis from "ioredis"
import { REDIS_EXPIRE, REDIS_URL } from "~/lib/env.server"
import { REDIS_EXPIRE, REDIS_REFRESH, REDIS_URL } from "~/lib/env.server"
if (!REDIS_URL) {
console.error("REDIS_URL not set")
@ -52,16 +52,18 @@ export async function cacheGet(options: {
const cacheValue = await redis.get(redisKey)
if (cacheValue && cacheValue !== "undefined" && cacheValue !== "null") {
if (!options.noUpdate) {
options.getValueFun().then((value) => {
if (value) {
redis.set(
redisKey,
JSON.stringify(value),
"EX",
options.expireTime || REDIS_EXPIRE,
)
}
})
setTimeout(() => {
options.getValueFun().then((value) => {
if (value) {
redis.set(
redisKey,
JSON.stringify(value),
"EX",
options.expireTime || REDIS_EXPIRE,
)
}
})
}, Math.random() * REDIS_REFRESH)
}
return JSON.parse(cacheValue)
} else {