Merge pull request #985 from ahmadk953/caching-improvements
Some checks are pending
ESLint / Run eslint scanning (push) Waiting to run
Code Format Check / check-format (push) Waiting to run
Run tests and upload coverage / Run tests and collect coverage (push) Waiting to run

Caching improvements
This commit is contained in:
Ahmad 2025-01-26 16:36:54 -05:00 committed by GitHub
commit 2d4984e1c7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 153 additions and 162 deletions

View file

@ -1,67 +1,65 @@
import { CacheHandler } from '@neshca/cache-handler'; import Redis from 'ioredis';
import createLruHandler from '@neshca/cache-handler/local-lru';
import createRedisHandler from '@neshca/cache-handler/redis-stack';
import { createClient } from 'redis';
CacheHandler.onCreation(async () => { let redis;
let client;
if (!redis) {
redis = new Redis(process.env.REDIS_URL);
redis.on('error', (err) => {
console.error('Redis Client Error:', err);
});
}
export default class CacheHandler {
constructor(options) {
this.options = options;
}
async get(key) {
try { try {
client = createClient({ const data = await redis.get(key);
url: process.env.REDIS_URL, return data ? JSON.parse(data) : null;
});
client.on('error', (error) => {
if (typeof process.env.NEXT_PRIVATE_DEBUG_CACHE !== 'undefined') {
console.error('Redis client error:', error);
}
});
} catch (error) { } catch (error) {
console.warn('Failed to create Redis client:', error); console.error('Cache Get Error:', error);
return null;
}
} }
if (client) { async set(key, data, ctx) {
try { try {
console.info('Connecting Redis client...'); const cacheData = {
value: data,
await client.connect(); lastModified: Date.now(),
console.info('Redis client connected.'); tags: ctx.tags || [],
} catch (error) {
console.warn('Failed to connect Redis client:', error);
console.warn('Disconnecting the Redis client...');
client
.disconnect()
.then(() => {
console.info('Redis client disconnected.');
})
.catch(() => {
console.warn(
'Failed to quit the Redis client after failing to connect.'
);
});
}
}
/** @type {import("@neshca/cache-handler").Handler | null} */
let handler;
if (client?.isReady) {
handler = await createRedisHandler({
client,
keyPrefix: 'tasko:',
timeoutMs: 1000,
});
} else {
handler = createLruHandler();
console.warn(
'Falling back to LRU handler because Redis client is not available.'
);
}
return {
handlers: [handler],
}; };
}); await redis.set(key, JSON.stringify(cacheData));
} catch (error) {
console.error('Cache Set Error:', error);
}
}
export default CacheHandler; async revalidateTag(tags) {
try {
tags = [tags].flat();
const keys = await redis.keys('*');
for (const key of keys) {
const value = await redis.get(key);
if (value && tags.length > 0) {
const parsed = JSON.parse(value);
if (
Array.isArray(parsed.tags) &&
parsed.tags.some((tag) => tags.includes(tag))
) {
await redis.del(key);
}
}
}
} catch (error) {
console.error('Cache RevalidateTag Error:', error);
}
}
resetRequestCache() {
// TODO: Implement request-specific cache reset if needed
}
}

View file

@ -3,14 +3,6 @@ import * as Sentry from '@sentry/nextjs';
export async function register() { export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') { if (process.env.NEXT_RUNTIME === 'nodejs') {
await import('./sentry.server.config'); await import('./sentry.server.config');
const { registerInitialCache } = await import(
'@neshca/cache-handler/instrumentation'
);
const CacheHandler = (await import('./cache-handler.mjs')).default;
await registerInitialCache(CacheHandler);
} }
if (process.env.NEXT_RUNTIME === 'edge') { if (process.env.NEXT_RUNTIME === 'edge') {

13
lib/redis.ts Normal file
View file

@ -0,0 +1,13 @@
import Redis from 'ioredis';
let redis: Redis | null = null;
if (!redis) {
redis = new Redis(process.env.REDIS_URL!);
redis.on('error', (err) => {
console.error('Redis Client Error:', err);
});
}
export default redis;

View file

@ -55,6 +55,7 @@ const nextConfig: NextConfig = {
process.env.NODE_ENV === 'production' process.env.NODE_ENV === 'production'
? require.resolve('./cache-handler.mjs') ? require.resolve('./cache-handler.mjs')
: undefined, : undefined,
cacheMaxMemorySize: process.env.NODE_ENV === 'production' ? 0 : undefined,
}; };
const withMDX = createMDX({}); const withMDX = createMDX({});

View file

@ -26,7 +26,6 @@
"@liveblocks/react": "^2.16.1", "@liveblocks/react": "^2.16.1",
"@mdx-js/loader": "^3.1.0", "@mdx-js/loader": "^3.1.0",
"@mdx-js/react": "^3.1.0", "@mdx-js/react": "^3.1.0",
"@neshca/cache-handler": "^1.9.0",
"@next/mdx": "^15.1.6", "@next/mdx": "^15.1.6",
"@prisma/client": "^6.2.1", "@prisma/client": "^6.2.1",
"@prisma/extension-accelerate": "^1.2.1", "@prisma/extension-accelerate": "^1.2.1",
@ -48,6 +47,7 @@
"clsx": "^2.1.1", "clsx": "^2.1.1",
"date-fns": "^4.1.0", "date-fns": "^4.1.0",
"dompurify": "^3.2.3", "dompurify": "^3.2.3",
"ioredis": "^5.4.2",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"lucide-react": "^0.473.0", "lucide-react": "^0.473.0",
"next": "^15.1.6", "next": "^15.1.6",
@ -55,7 +55,6 @@
"react": "^19.0.0", "react": "^19.0.0",
"react-day-picker": "^9.5.0", "react-day-picker": "^9.5.0",
"react-dom": "^19.0.0", "react-dom": "^19.0.0",
"redis": "^4.7.0",
"sharp": "^0.33.5", "sharp": "^0.33.5",
"sonner": "^1.7.2", "sonner": "^1.7.2",
"stripe": "^17.4.0", "stripe": "^17.4.0",
@ -82,6 +81,7 @@
"@testing-library/react": "^16.2.0", "@testing-library/react": "^16.2.0",
"@types/compression": "^1.7.5", "@types/compression": "^1.7.5",
"@types/dompurify": "^3", "@types/dompurify": "^3",
"@types/ioredis": "^5.0.0",
"@types/jest": "^29.5.14", "@types/jest": "^29.5.14",
"@types/lodash": "^4.17.14", "@types/lodash": "^4.17.14",
"@types/mdx": "^2.0.13", "@types/mdx": "^2.0.13",

View file

@ -9,7 +9,7 @@ Sentry.init({
integrations: [ integrations: [
Sentry.redisIntegration({ Sentry.redisIntegration({
cachePrefixes: ['tasko:'], cachePrefixes: [''],
}), }),
], ],

181
yarn.lock
View file

@ -1554,6 +1554,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@ioredis/commands@npm:^1.1.1":
version: 1.2.0
resolution: "@ioredis/commands@npm:1.2.0"
checksum: 10c0/a5d3c29dd84d8a28b7c67a441ac1715cbd7337a7b88649c0f17c345d89aa218578d2b360760017c48149ef8a70f44b051af9ac0921a0622c2b479614c4f65b36
languageName: node
linkType: hard
"@isaacs/cliui@npm:^8.0.2": "@isaacs/cliui@npm:^8.0.2":
version: 8.0.2 version: 8.0.2
resolution: "@isaacs/cliui@npm:8.0.2" resolution: "@isaacs/cliui@npm:8.0.2"
@ -2005,19 +2012,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@neshca/cache-handler@npm:^1.9.0":
version: 1.9.0
resolution: "@neshca/cache-handler@npm:1.9.0"
dependencies:
cluster-key-slot: "npm:1.1.2"
lru-cache: "npm:10.4.3"
peerDependencies:
next: ">= 13.5.1 < 15"
redis: ">= 4.6"
checksum: 10c0/883360e0979181448c8a81886d69e3460d3ea60283ceaaf1941a8bc8255cb1dedf5ba448951cf0cabf9969381a4481777d3c408cd7a5537e7e4d0c13f89544a4
languageName: node
linkType: hard
"@next/env@npm:15.1.6": "@next/env@npm:15.1.6":
version: 15.1.6 version: 15.1.6
resolution: "@next/env@npm:15.1.6" resolution: "@next/env@npm:15.1.6"
@ -3653,62 +3647,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@redis/bloom@npm:1.2.0":
version: 1.2.0
resolution: "@redis/bloom@npm:1.2.0"
peerDependencies:
"@redis/client": ^1.0.0
checksum: 10c0/7dde8e67188164e96226c8a5c78ebd2801f1662947371e78fb95fb180c1e9ddff8d237012eb5e9182775be61cb546f67f759927cdaee0d178d863ee290e1fb27
languageName: node
linkType: hard
"@redis/client@npm:1.6.0":
version: 1.6.0
resolution: "@redis/client@npm:1.6.0"
dependencies:
cluster-key-slot: "npm:1.1.2"
generic-pool: "npm:3.9.0"
yallist: "npm:4.0.0"
checksum: 10c0/c80a01b4f72d32284515dac6d1aefe0e9c881d08b8db33281f87b51650c1c116b18074a29ca81599d15dccb37b29eef9b26a75a5755150ae27d163e680c34bf6
languageName: node
linkType: hard
"@redis/graph@npm:1.1.1":
version: 1.1.1
resolution: "@redis/graph@npm:1.1.1"
peerDependencies:
"@redis/client": ^1.0.0
checksum: 10c0/64199db2cb3669c4911af8aba3b7116c4c2c1df37ca74b2a65555e62c863935a0cea74bc41bd92acf2e551074eb2a30c75f54a9f439b40e0f9bb67fc5fb66614
languageName: node
linkType: hard
"@redis/json@npm:1.0.7":
version: 1.0.7
resolution: "@redis/json@npm:1.0.7"
peerDependencies:
"@redis/client": ^1.0.0
checksum: 10c0/cef473711d66f7568a16edbd728acca7d237cfeaa15e0326b5b628dfab4afc0c76c7354e7f8efad6ecc64a1cb774e4aa060ee46497b633e18ba0a2f0aace1cc4
languageName: node
linkType: hard
"@redis/search@npm:1.2.0":
version: 1.2.0
resolution: "@redis/search@npm:1.2.0"
peerDependencies:
"@redis/client": ^1.0.0
checksum: 10c0/01d57ac10d2c5698e04e4a2f945440db3087e8834643ca950c099879dbcd77526604ca6f5c2ee883dfd4b337b0a24cb7d81ac56845aa83f89a4f161362a08dc6
languageName: node
linkType: hard
"@redis/time-series@npm:1.1.0":
version: 1.1.0
resolution: "@redis/time-series@npm:1.1.0"
peerDependencies:
"@redis/client": ^1.0.0
checksum: 10c0/503d0d5cbc9113d26666bb7b4dea57619badbcdfeee0369abf647250f26c5482ed5827c83f88f9f0cf22e021e3e7cb562459669d733fac05652972e208d6ba0f
languageName: node
linkType: hard
"@rollup/plugin-commonjs@npm:28.0.1": "@rollup/plugin-commonjs@npm:28.0.1":
version: 28.0.1 version: 28.0.1
resolution: "@rollup/plugin-commonjs@npm:28.0.1" resolution: "@rollup/plugin-commonjs@npm:28.0.1"
@ -4387,6 +4325,15 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@types/ioredis@npm:^5.0.0":
version: 5.0.0
resolution: "@types/ioredis@npm:5.0.0"
dependencies:
ioredis: "npm:*"
checksum: 10c0/e52ce4239f0334701fc95fb5aaf1753d75f7582099fdf152743192f49d9ee4a88478b339d015e50cb5e111e38925846cf20668355f4046af7855021d2be181f0
languageName: node
linkType: hard
"@types/istanbul-lib-coverage@npm:*, @types/istanbul-lib-coverage@npm:^2.0.0, @types/istanbul-lib-coverage@npm:^2.0.1": "@types/istanbul-lib-coverage@npm:*, @types/istanbul-lib-coverage@npm:^2.0.0, @types/istanbul-lib-coverage@npm:^2.0.1":
version: 2.0.6 version: 2.0.6
resolution: "@types/istanbul-lib-coverage@npm:2.0.6" resolution: "@types/istanbul-lib-coverage@npm:2.0.6"
@ -5771,7 +5718,7 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"cluster-key-slot@npm:1.1.2": "cluster-key-slot@npm:^1.1.0":
version: 1.1.2 version: 1.1.2
resolution: "cluster-key-slot@npm:1.1.2" resolution: "cluster-key-slot@npm:1.1.2"
checksum: 10c0/d7d39ca28a8786e9e801eeb8c770e3c3236a566625d7299a47bb71113fb2298ce1039596acb82590e598c52dbc9b1f088c8f587803e697cb58e1867a95ff94d3 checksum: 10c0/d7d39ca28a8786e9e801eeb8c770e3c3236a566625d7299a47bb71113fb2298ce1039596acb82590e598c52dbc9b1f088c8f587803e697cb58e1867a95ff94d3
@ -6154,6 +6101,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"denque@npm:^2.1.0":
version: 2.1.0
resolution: "denque@npm:2.1.0"
checksum: 10c0/f9ef81aa0af9c6c614a727cb3bd13c5d7db2af1abf9e6352045b86e85873e629690f6222f4edd49d10e4ccf8f078bbeec0794fafaf61b659c0589d0c511ec363
languageName: node
linkType: hard
"deprecation@npm:^2.0.0": "deprecation@npm:^2.0.0":
version: 2.3.1 version: 2.3.1
resolution: "deprecation@npm:2.3.1" resolution: "deprecation@npm:2.3.1"
@ -7526,13 +7480,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"generic-pool@npm:3.9.0":
version: 3.9.0
resolution: "generic-pool@npm:3.9.0"
checksum: 10c0/6b314d0d71170d5cbaf7162c423f53f8d6556b2135626a65bcdc03c089840b0a2f59eeb2d907939b8200e945eaf71ceb6630426f22d2128a1d242aec4b232aa7
languageName: node
linkType: hard
"gensync@npm:^1.0.0-beta.2": "gensync@npm:^1.0.0-beta.2":
version: 1.0.0-beta.2 version: 1.0.0-beta.2
resolution: "gensync@npm:1.0.0-beta.2" resolution: "gensync@npm:1.0.0-beta.2"
@ -8057,6 +8004,23 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"ioredis@npm:*, ioredis@npm:^5.4.2":
version: 5.4.2
resolution: "ioredis@npm:5.4.2"
dependencies:
"@ioredis/commands": "npm:^1.1.1"
cluster-key-slot: "npm:^1.1.0"
debug: "npm:^4.3.4"
denque: "npm:^2.1.0"
lodash.defaults: "npm:^4.2.0"
lodash.isarguments: "npm:^3.1.0"
redis-errors: "npm:^1.2.0"
redis-parser: "npm:^3.0.0"
standard-as-callback: "npm:^2.1.0"
checksum: 10c0/e59d2cceb43ed74b487d7b50fa91b93246e734e5d4835c7e62f64e44da072f12ab43b044248012e6f8b76c61a7c091a2388caad50e8ad69a8ce5515a730b23b8
languageName: node
linkType: hard
"ip-address@npm:^9.0.5": "ip-address@npm:^9.0.5":
version: 9.0.5 version: 9.0.5
resolution: "ip-address@npm:9.0.5" resolution: "ip-address@npm:9.0.5"
@ -9281,6 +9245,20 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"lodash.defaults@npm:^4.2.0":
version: 4.2.0
resolution: "lodash.defaults@npm:4.2.0"
checksum: 10c0/d5b77aeb702caa69b17be1358faece33a84497bcca814897383c58b28a2f8dfc381b1d9edbec239f8b425126a3bbe4916223da2a576bb0411c2cefd67df80707
languageName: node
linkType: hard
"lodash.isarguments@npm:^3.1.0":
version: 3.1.0
resolution: "lodash.isarguments@npm:3.1.0"
checksum: 10c0/5e8f95ba10975900a3920fb039a3f89a5a79359a1b5565e4e5b4310ed6ebe64011e31d402e34f577eca983a1fc01ff86c926e3cbe602e1ddfc858fdd353e62d8
languageName: node
linkType: hard
"lodash.isplainobject@npm:^4.0.6": "lodash.isplainobject@npm:^4.0.6":
version: 4.0.6 version: 4.0.6
resolution: "lodash.isplainobject@npm:4.0.6" resolution: "lodash.isplainobject@npm:4.0.6"
@ -9329,7 +9307,7 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"lru-cache@npm:10.4.3, lru-cache@npm:^10.0.1, lru-cache@npm:^10.2.0": "lru-cache@npm:^10.0.1, lru-cache@npm:^10.2.0":
version: 10.4.3 version: 10.4.3
resolution: "lru-cache@npm:10.4.3" resolution: "lru-cache@npm:10.4.3"
checksum: 10c0/ebd04fbca961e6c1d6c0af3799adcc966a1babe798f685bb84e6599266599cd95d94630b10262f5424539bc4640107e8a33aa28585374abf561d30d16f4b39fb checksum: 10c0/ebd04fbca961e6c1d6c0af3799adcc966a1babe798f685bb84e6599266599cd95d94630b10262f5424539bc4640107e8a33aa28585374abf561d30d16f4b39fb
@ -11425,17 +11403,19 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"redis@npm:^4.7.0": "redis-errors@npm:^1.0.0, redis-errors@npm:^1.2.0":
version: 4.7.0 version: 1.2.0
resolution: "redis@npm:4.7.0" resolution: "redis-errors@npm:1.2.0"
checksum: 10c0/5b316736e9f532d91a35bff631335137a4f974927bb2fb42bf8c2f18879173a211787db8ac4c3fde8f75ed6233eb0888e55d52510b5620e30d69d7d719c8b8a7
languageName: node
linkType: hard
"redis-parser@npm:^3.0.0":
version: 3.0.0
resolution: "redis-parser@npm:3.0.0"
dependencies: dependencies:
"@redis/bloom": "npm:1.2.0" redis-errors: "npm:^1.0.0"
"@redis/client": "npm:1.6.0" checksum: 10c0/ee16ac4c7b2a60b1f42a2cdaee22b005bd4453eb2d0588b8a4939718997ae269da717434da5d570fe0b05030466eeb3f902a58cf2e8e1ca058bf6c9c596f632f
"@redis/graph": "npm:1.1.1"
"@redis/json": "npm:1.0.7"
"@redis/search": "npm:1.2.0"
"@redis/time-series": "npm:1.1.0"
checksum: 10c0/a05632a58adbcaa4566238073cd6d00ed008522d2ef015a31aaef200c184a4eff4fa007c514eda91dda1e1205350b5901d0c7b58824dbfa593feb81a0087bf4d
languageName: node languageName: node
linkType: hard linkType: hard
@ -12221,6 +12201,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"standard-as-callback@npm:^2.1.0":
version: 2.1.0
resolution: "standard-as-callback@npm:2.1.0"
checksum: 10c0/012677236e3d3fdc5689d29e64ea8a599331c4babe86956bf92fc5e127d53f85411c5536ee0079c52c43beb0026b5ce7aa1d834dd35dd026e82a15d1bcaead1f
languageName: node
linkType: hard
"std-env@npm:^3.7.0": "std-env@npm:^3.7.0":
version: 3.8.0 version: 3.8.0
resolution: "std-env@npm:3.8.0" resolution: "std-env@npm:3.8.0"
@ -12618,7 +12605,6 @@ __metadata:
"@mdx-js/loader": "npm:^3.1.0" "@mdx-js/loader": "npm:^3.1.0"
"@mdx-js/react": "npm:^3.1.0" "@mdx-js/react": "npm:^3.1.0"
"@microsoft/eslint-formatter-sarif": "npm:^3.1.0" "@microsoft/eslint-formatter-sarif": "npm:^3.1.0"
"@neshca/cache-handler": "npm:^1.9.0"
"@next/eslint-plugin-next": "npm:15.1.6" "@next/eslint-plugin-next": "npm:15.1.6"
"@next/mdx": "npm:^15.1.6" "@next/mdx": "npm:^15.1.6"
"@prisma/client": "npm:^6.2.1" "@prisma/client": "npm:^6.2.1"
@ -12641,6 +12627,7 @@ __metadata:
"@testing-library/react": "npm:^16.2.0" "@testing-library/react": "npm:^16.2.0"
"@types/compression": "npm:^1.7.5" "@types/compression": "npm:^1.7.5"
"@types/dompurify": "npm:^3" "@types/dompurify": "npm:^3"
"@types/ioredis": "npm:^5.0.0"
"@types/jest": "npm:^29.5.14" "@types/jest": "npm:^29.5.14"
"@types/lodash": "npm:^4.17.14" "@types/lodash": "npm:^4.17.14"
"@types/mdx": "npm:^2.0.13" "@types/mdx": "npm:^2.0.13"
@ -12662,6 +12649,7 @@ __metadata:
eslint-config-prettier: "npm:^10.0.1" eslint-config-prettier: "npm:^10.0.1"
eslint-plugin-react-compiler: "npm:^19.0.0-beta-55955c9-20241229" eslint-plugin-react-compiler: "npm:^19.0.0-beta-55955c9-20241229"
fluid-tailwind: "npm:^1.0.4" fluid-tailwind: "npm:^1.0.4"
ioredis: "npm:^5.4.2"
jest: "npm:^29.7.0" jest: "npm:^29.7.0"
jest-environment-jsdom: "npm:^29.7.0" jest-environment-jsdom: "npm:^29.7.0"
jest-junit: "npm:^16.0.0" jest-junit: "npm:^16.0.0"
@ -12676,7 +12664,6 @@ __metadata:
react: "npm:^19.0.0" react: "npm:^19.0.0"
react-day-picker: "npm:^9.5.0" react-day-picker: "npm:^9.5.0"
react-dom: "npm:^19.0.0" react-dom: "npm:^19.0.0"
redis: "npm:^4.7.0"
sharp: "npm:^0.33.5" sharp: "npm:^0.33.5"
sonner: "npm:^1.7.2" sonner: "npm:^1.7.2"
stripe: "npm:^17.4.0" stripe: "npm:^17.4.0"
@ -13630,13 +13617,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"yallist@npm:4.0.0, yallist@npm:^4.0.0":
version: 4.0.0
resolution: "yallist@npm:4.0.0"
checksum: 10c0/2286b5e8dbfe22204ab66e2ef5cc9bbb1e55dfc873bbe0d568aa943eb255d131890dfd5bf243637273d31119b870f49c18fcde2c6ffbb7a7a092b870dc90625a
languageName: node
linkType: hard
"yallist@npm:^3.0.2": "yallist@npm:^3.0.2":
version: 3.1.1 version: 3.1.1
resolution: "yallist@npm:3.1.1" resolution: "yallist@npm:3.1.1"
@ -13644,6 +13624,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"yallist@npm:^4.0.0":
version: 4.0.0
resolution: "yallist@npm:4.0.0"
checksum: 10c0/2286b5e8dbfe22204ab66e2ef5cc9bbb1e55dfc873bbe0d568aa943eb255d131890dfd5bf243637273d31119b870f49c18fcde2c6ffbb7a7a092b870dc90625a
languageName: node
linkType: hard
"yallist@npm:^5.0.0": "yallist@npm:^5.0.0":
version: 5.0.0 version: 5.0.0
resolution: "yallist@npm:5.0.0" resolution: "yallist@npm:5.0.0"