From 15f879ebb08ea01b22ee39e09ffacc002cae0ae6 Mon Sep 17 00:00:00 2001 From: Ahmad <103906421+ahmadk953@users.noreply.github.com> Date: Thu, 23 Jan 2025 22:01:17 -0500 Subject: [PATCH 1/9] Change to ioredis and Update Caching --- cache-handler.mjs | 35 ++++----- next.config.ts | 1 + package.json | 2 +- sentry.server.config.ts | 2 +- yarn.lock | 155 +++++++++++++++++++--------------------- 5 files changed, 91 insertions(+), 104 deletions(-) diff --git a/cache-handler.mjs b/cache-handler.mjs index dbb5905..eb3247f 100644 --- a/cache-handler.mjs +++ b/cache-handler.mjs @@ -1,15 +1,16 @@ import { CacheHandler } from '@neshca/cache-handler'; import createLruHandler from '@neshca/cache-handler/local-lru'; import createRedisHandler from '@neshca/cache-handler/redis-stack'; -import { createClient } from 'redis'; +import Redis from 'ioredis'; CacheHandler.onCreation(async () => { + /** @type {import("@neshca/cache-handler").Handler | null} */ + let handler; + let client; try { - client = createClient({ - url: process.env.REDIS_URL, - }); + client = new Redis(`${process.env.REDIS_URL}`); client.on('error', (error) => { if (typeof process.env.NEXT_PRIVATE_DEBUG_CACHE !== 'undefined') { @@ -26,6 +27,11 @@ CacheHandler.onCreation(async () => { await client.connect(); console.info('Redis client connected.'); + + handler = createRedisHandler({ + client, + timeoutMs: 1000, + }); } catch (error) { console.warn('Failed to connect Redis client:', error); @@ -40,25 +46,14 @@ CacheHandler.onCreation(async () => { 'Failed to quit the Redis client after failing to connect.' ); }); + + handler = createLruHandler(); + console.warn( + 'Falling back to LRU handler because Redis client is not available.' + ); } } - /** @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], }; diff --git a/next.config.ts b/next.config.ts index 2045ae2..a5779ec 100644 --- a/next.config.ts +++ b/next.config.ts @@ -55,6 +55,7 @@ const nextConfig: NextConfig = { process.env.NODE_ENV === 'production' ? require.resolve('./cache-handler.mjs') : undefined, + cacheMaxMemorySize: process.env.NODE_ENV === 'production' ? 0 : undefined, }; const withMDX = createMDX({}); diff --git a/package.json b/package.json index bcc5648..28bb1f5 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "clsx": "^2.1.1", "date-fns": "^4.1.0", "dompurify": "^3.2.3", + "ioredis": "^5.4.2", "lodash": "^4.17.21", "lucide-react": "^0.473.0", "next": "^15.1.6", @@ -55,7 +56,6 @@ "react": "^19.0.0", "react-day-picker": "^9.5.0", "react-dom": "^19.0.0", - "redis": "^4.7.0", "sharp": "^0.33.5", "sonner": "^1.7.2", "stripe": "^17.4.0", diff --git a/sentry.server.config.ts b/sentry.server.config.ts index f5f46cc..387ec5c 100644 --- a/sentry.server.config.ts +++ b/sentry.server.config.ts @@ -9,7 +9,7 @@ Sentry.init({ integrations: [ Sentry.redisIntegration({ - cachePrefixes: ['tasko:'], + cachePrefixes: [''], }), ], diff --git a/yarn.lock b/yarn.lock index dfdc6eb..f130515 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1554,6 +1554,13 @@ __metadata: languageName: node 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": version: 8.0.2 resolution: "@isaacs/cliui@npm:8.0.2" @@ -3653,62 +3660,6 @@ __metadata: languageName: node 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": version: 28.0.1 resolution: "@rollup/plugin-commonjs@npm:28.0.1" @@ -5771,7 +5722,7 @@ __metadata: languageName: node linkType: hard -"cluster-key-slot@npm:1.1.2": +"cluster-key-slot@npm:1.1.2, cluster-key-slot@npm:^1.1.0": version: 1.1.2 resolution: "cluster-key-slot@npm:1.1.2" checksum: 10c0/d7d39ca28a8786e9e801eeb8c770e3c3236a566625d7299a47bb71113fb2298ce1039596acb82590e598c52dbc9b1f088c8f587803e697cb58e1867a95ff94d3 @@ -6154,6 +6105,13 @@ __metadata: languageName: node 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": version: 2.3.1 resolution: "deprecation@npm:2.3.1" @@ -7526,13 +7484,6 @@ __metadata: languageName: node 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": version: 1.0.0-beta.2 resolution: "gensync@npm:1.0.0-beta.2" @@ -8057,6 +8008,23 @@ __metadata: languageName: node linkType: hard +"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": version: 9.0.5 resolution: "ip-address@npm:9.0.5" @@ -9281,6 +9249,20 @@ __metadata: languageName: node 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": version: 4.0.6 resolution: "lodash.isplainobject@npm:4.0.6" @@ -11425,17 +11407,19 @@ __metadata: languageName: node linkType: hard -"redis@npm:^4.7.0": - version: 4.7.0 - resolution: "redis@npm:4.7.0" +"redis-errors@npm:^1.0.0, redis-errors@npm:^1.2.0": + version: 1.2.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: - "@redis/bloom": "npm:1.2.0" - "@redis/client": "npm:1.6.0" - "@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 + redis-errors: "npm:^1.0.0" + checksum: 10c0/ee16ac4c7b2a60b1f42a2cdaee22b005bd4453eb2d0588b8a4939718997ae269da717434da5d570fe0b05030466eeb3f902a58cf2e8e1ca058bf6c9c596f632f languageName: node linkType: hard @@ -12221,6 +12205,13 @@ __metadata: languageName: node 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": version: 3.8.0 resolution: "std-env@npm:3.8.0" @@ -12662,6 +12653,7 @@ __metadata: eslint-config-prettier: "npm:^10.0.1" eslint-plugin-react-compiler: "npm:^19.0.0-beta-55955c9-20241229" fluid-tailwind: "npm:^1.0.4" + ioredis: "npm:^5.4.2" jest: "npm:^29.7.0" jest-environment-jsdom: "npm:^29.7.0" jest-junit: "npm:^16.0.0" @@ -12676,7 +12668,6 @@ __metadata: react: "npm:^19.0.0" react-day-picker: "npm:^9.5.0" react-dom: "npm:^19.0.0" - redis: "npm:^4.7.0" sharp: "npm:^0.33.5" sonner: "npm:^1.7.2" stripe: "npm:^17.4.0" @@ -13630,13 +13621,6 @@ __metadata: languageName: node 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": version: 3.1.1 resolution: "yallist@npm:3.1.1" @@ -13644,6 +13628,13 @@ __metadata: languageName: node 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": version: 5.0.0 resolution: "yallist@npm:5.0.0" From 5ef2a3c67fa7fd636a750ac71661e18a7caeebfb Mon Sep 17 00:00:00 2001 From: Ahmad <103906421+ahmadk953@users.noreply.github.com> Date: Sat, 25 Jan 2025 23:05:01 -0500 Subject: [PATCH 2/9] Switch to Custom Cache Handler --- cache-handler.mjs | 85 +++++++++++++++++----------------------------- instrumentation.ts | 8 ----- package.json | 1 - yarn.lock | 18 ++-------- 4 files changed, 34 insertions(+), 78 deletions(-) diff --git a/cache-handler.mjs b/cache-handler.mjs index eb3247f..574309e 100644 --- a/cache-handler.mjs +++ b/cache-handler.mjs @@ -1,62 +1,41 @@ -import { CacheHandler } from '@neshca/cache-handler'; -import createLruHandler from '@neshca/cache-handler/local-lru'; -import createRedisHandler from '@neshca/cache-handler/redis-stack'; -import Redis from 'ioredis'; +import Redis from 'ioredis' -CacheHandler.onCreation(async () => { - /** @type {import("@neshca/cache-handler").Handler | null} */ - let handler; +const redis = new Redis(`${process.env.REDIS_URL}`) - let client; - - try { - client = new Redis(`${process.env.REDIS_URL}`); - - client.on('error', (error) => { - if (typeof process.env.NEXT_PRIVATE_DEBUG_CACHE !== 'undefined') { - console.error('Redis client error:', error); - } - }); - } catch (error) { - console.warn('Failed to create Redis client:', error); +export default class CacheHandler { + constructor(options) { + this.options = options } - if (client) { - try { - console.info('Connecting Redis client...'); + async get(key) { + const data = await redis.get(key) + return data ? JSON.parse(data) : null + } - await client.connect(); - console.info('Redis client connected.'); + async set(key, data, ctx) { + const cacheData = { + value: data, + lastModified: Date.now(), + tags: ctx.tags, + } + await redis.set(key, JSON.stringify(cacheData)) + } - handler = createRedisHandler({ - client, - timeoutMs: 1000, - }); - } 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.' - ); - }); - - handler = createLruHandler(); - console.warn( - 'Falling back to LRU handler because Redis client is not available.' - ); + async revalidateTag(tags) { + tags = [tags].flat() + const keys = await redis.keys('*') + for (const key of keys) { + const value = await redis.get(key) + if (value) { + const parsed = JSON.parse(value) + if (parsed.tags.some(tag => tags.includes(tag))) { + await redis.del(key) + } + } } } - return { - handlers: [handler], - }; -}); - -export default CacheHandler; + resetRequestCache() { + // Implement request-specific cache reset if needed + } +} \ No newline at end of file diff --git a/instrumentation.ts b/instrumentation.ts index dab2871..964f937 100644 --- a/instrumentation.ts +++ b/instrumentation.ts @@ -3,14 +3,6 @@ import * as Sentry from '@sentry/nextjs'; export async function register() { if (process.env.NEXT_RUNTIME === 'nodejs') { 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') { diff --git a/package.json b/package.json index 178f1ac..4daed9c 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,6 @@ "@liveblocks/react": "^2.16.1", "@mdx-js/loader": "^3.1.0", "@mdx-js/react": "^3.1.0", - "@neshca/cache-handler": "^1.9.0", "@next/mdx": "^15.1.6", "@prisma/client": "^6.2.1", "@prisma/extension-accelerate": "^1.2.1", diff --git a/yarn.lock b/yarn.lock index 5cce0c5..7b2e28d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2012,19 +2012,6 @@ __metadata: languageName: node 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": version: 15.1.6 resolution: "@next/env@npm:15.1.6" @@ -5722,7 +5709,7 @@ __metadata: languageName: node linkType: hard -"cluster-key-slot@npm:1.1.2, cluster-key-slot@npm:^1.1.0": +"cluster-key-slot@npm:^1.1.0": version: 1.1.2 resolution: "cluster-key-slot@npm:1.1.2" checksum: 10c0/d7d39ca28a8786e9e801eeb8c770e3c3236a566625d7299a47bb71113fb2298ce1039596acb82590e598c52dbc9b1f088c8f587803e697cb58e1867a95ff94d3 @@ -9311,7 +9298,7 @@ __metadata: languageName: node 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 resolution: "lru-cache@npm:10.4.3" checksum: 10c0/ebd04fbca961e6c1d6c0af3799adcc966a1babe798f685bb84e6599266599cd95d94630b10262f5424539bc4640107e8a33aa28585374abf561d30d16f4b39fb @@ -12609,7 +12596,6 @@ __metadata: "@mdx-js/loader": "npm:^3.1.0" "@mdx-js/react": "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/mdx": "npm:^15.1.6" "@prisma/client": "npm:^6.2.1" From 20055c396123c28f5527194c7557348ac3a0d0fa Mon Sep 17 00:00:00 2001 From: Ahmad <103906421+ahmadk953@users.noreply.github.com> Date: Sat, 25 Jan 2025 23:55:00 -0500 Subject: [PATCH 3/9] Added Error Handling to Cache --- cache-handler.mjs | 53 ++++++++++++++++++++++++++++------------------- lib/redis.ts | 13 ++++++++++++ 2 files changed, 45 insertions(+), 21 deletions(-) create mode 100644 lib/redis.ts diff --git a/cache-handler.mjs b/cache-handler.mjs index 574309e..d024d1c 100644 --- a/cache-handler.mjs +++ b/cache-handler.mjs @@ -1,41 +1,52 @@ -import Redis from 'ioredis' - -const redis = new Redis(`${process.env.REDIS_URL}`) +import { redis } from '@/lib/redis'; export default class CacheHandler { constructor(options) { - this.options = options + this.options = options; } async get(key) { - const data = await redis.get(key) - return data ? JSON.parse(data) : null + try { + const data = await redis.get(key); + return data ? JSON.parse(data) : null; + } catch (error) { + console.error('Cache Get Error:', error); + return null; + } } async set(key, data, ctx) { - const cacheData = { - value: data, - lastModified: Date.now(), - tags: ctx.tags, + try { + const cacheData = { + value: data, + lastModified: Date.now(), + tags: ctx.tags, + }; + await redis.set(key, JSON.stringify(cacheData)); + } catch (error) { + console.error('Cache Set Error:', error); } - await redis.set(key, JSON.stringify(cacheData)) } async revalidateTag(tags) { - tags = [tags].flat() - const keys = await redis.keys('*') - for (const key of keys) { - const value = await redis.get(key) - if (value) { - const parsed = JSON.parse(value) - if (parsed.tags.some(tag => tags.includes(tag))) { - await redis.del(key) + try { + tags = [tags].flat(); + const keys = await redis.keys('*'); + for (const key of keys) { + const value = await redis.get(key); + if (value) { + const parsed = JSON.parse(value); + if (parsed.tags.some((tag) => tags.includes(tag))) { + await redis.del(key); + } } } + } catch (error) { + console.error('Cache RevalidateTag Error:', error); } } resetRequestCache() { - // Implement request-specific cache reset if needed + // TODO: Implement request-specific cache reset if needed } -} \ No newline at end of file +} diff --git a/lib/redis.ts b/lib/redis.ts new file mode 100644 index 0000000..bc0e1fe --- /dev/null +++ b/lib/redis.ts @@ -0,0 +1,13 @@ +import Redis from 'ioredis'; + +let redis; + +if (!redis) { + redis = new Redis(process.env.REDIS_URL); + + redis.on('error', (err) => { + console.error('Redis Client Error:', err); + }); +} + +export default redis; From e0be79f5fd776d3da2d73d97045f425803096359 Mon Sep 17 00:00:00 2001 From: Ahmad <103906421+ahmadk953@users.noreply.github.com> Date: Sun, 26 Jan 2025 00:05:50 -0500 Subject: [PATCH 4/9] Updated Types for Redis and Fixed Compile Errors --- lib/redis.ts | 2 +- package.json | 1 + yarn.lock | 12 +++++++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/redis.ts b/lib/redis.ts index bc0e1fe..6076321 100644 --- a/lib/redis.ts +++ b/lib/redis.ts @@ -3,7 +3,7 @@ import Redis from 'ioredis'; let redis; if (!redis) { - redis = new Redis(process.env.REDIS_URL); + redis = new Redis(process.env.REDIS_URL!); redis.on('error', (err) => { console.error('Redis Client Error:', err); diff --git a/package.json b/package.json index 4daed9c..91e65fa 100644 --- a/package.json +++ b/package.json @@ -81,6 +81,7 @@ "@testing-library/react": "^16.2.0", "@types/compression": "^1.7.5", "@types/dompurify": "^3", + "@types/ioredis": "^5.0.0", "@types/jest": "^29.5.14", "@types/lodash": "^4.17.14", "@types/mdx": "^2.0.13", diff --git a/yarn.lock b/yarn.lock index 7b2e28d..9ab9b3c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4325,6 +4325,15 @@ __metadata: languageName: node 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": version: 2.0.6 resolution: "@types/istanbul-lib-coverage@npm:2.0.6" @@ -7995,7 +8004,7 @@ __metadata: languageName: node linkType: hard -"ioredis@npm:^5.4.2": +"ioredis@npm:*, ioredis@npm:^5.4.2": version: 5.4.2 resolution: "ioredis@npm:5.4.2" dependencies: @@ -12618,6 +12627,7 @@ __metadata: "@testing-library/react": "npm:^16.2.0" "@types/compression": "npm:^1.7.5" "@types/dompurify": "npm:^3" + "@types/ioredis": "npm:^5.0.0" "@types/jest": "npm:^29.5.14" "@types/lodash": "npm:^4.17.14" "@types/mdx": "npm:^2.0.13" From e9b6d285917ab0b104cd7cedd6bafb6b355ba8ce Mon Sep 17 00:00:00 2001 From: Ahmad <103906421+ahmadk953@users.noreply.github.com> Date: Sun, 26 Jan 2025 00:11:30 -0500 Subject: [PATCH 5/9] Updated Types and Fixed Imports --- cache-handler.mjs | 2 +- lib/redis.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cache-handler.mjs b/cache-handler.mjs index d024d1c..a6debc3 100644 --- a/cache-handler.mjs +++ b/cache-handler.mjs @@ -1,4 +1,4 @@ -import { redis } from '@/lib/redis'; +import redis from './lib/redis' export default class CacheHandler { constructor(options) { diff --git a/lib/redis.ts b/lib/redis.ts index 6076321..a91fa6a 100644 --- a/lib/redis.ts +++ b/lib/redis.ts @@ -1,6 +1,6 @@ import Redis from 'ioredis'; -let redis; +let redis: Redis | null = null; if (!redis) { redis = new Redis(process.env.REDIS_URL!); From e55fe00bad6f06554cdd13df7d3402f9a8f20452 Mon Sep 17 00:00:00 2001 From: Ahmad <103906421+ahmadk953@users.noreply.github.com> Date: Sun, 26 Jan 2025 15:44:15 -0500 Subject: [PATCH 6/9] Fixed Cache Handler --- cache-handler.mjs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cache-handler.mjs b/cache-handler.mjs index a6debc3..76b1b07 100644 --- a/cache-handler.mjs +++ b/cache-handler.mjs @@ -1,4 +1,4 @@ -import redis from './lib/redis' +import redis from './lib/redis.ts'; export default class CacheHandler { constructor(options) { @@ -20,7 +20,7 @@ export default class CacheHandler { const cacheData = { value: data, lastModified: Date.now(), - tags: ctx.tags, + tags: ctx.tags || [], }; await redis.set(key, JSON.stringify(cacheData)); } catch (error) { @@ -34,9 +34,12 @@ export default class CacheHandler { const keys = await redis.keys('*'); for (const key of keys) { const value = await redis.get(key); - if (value) { + if (value && tags.length > 0) { const parsed = JSON.parse(value); - if (parsed.tags.some((tag) => tags.includes(tag))) { + if ( + Array.isArray(parsed.tags) && + parsed.tags.some((tag) => tags.includes(tag)) + ) { await redis.del(key); } } From 4e1528d63f6198fe0d69a9952820b1cd9e394716 Mon Sep 17 00:00:00 2001 From: Ahmad <103906421+ahmadk953@users.noreply.github.com> Date: Sun, 26 Jan 2025 15:49:23 -0500 Subject: [PATCH 7/9] Fix Compile Issues --- cache-handler.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cache-handler.mjs b/cache-handler.mjs index 76b1b07..b08a544 100644 --- a/cache-handler.mjs +++ b/cache-handler.mjs @@ -1,4 +1,4 @@ -import redis from './lib/redis.ts'; +import redis from './lib/redis'; export default class CacheHandler { constructor(options) { From 80d0f53c70b73fc98a66794ea1f53e54423eef42 Mon Sep 17 00:00:00 2001 From: Ahmad <103906421+ahmadk953@users.noreply.github.com> Date: Sun, 26 Jan 2025 15:54:53 -0500 Subject: [PATCH 8/9] Fix Compile Issues --- cache-handler.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cache-handler.mjs b/cache-handler.mjs index b08a544..9e93ca1 100644 --- a/cache-handler.mjs +++ b/cache-handler.mjs @@ -1,4 +1,4 @@ -import redis from './lib/redis'; +import redis from './lib/redis.js'; export default class CacheHandler { constructor(options) { From 5b578ff9b5f4585daf1df9936252f8609daa61a3 Mon Sep 17 00:00:00 2001 From: Ahmad <103906421+ahmadk953@users.noreply.github.com> Date: Sun, 26 Jan 2025 16:13:41 -0500 Subject: [PATCH 9/9] Fix Compile Issues (once and for all) --- cache-handler.mjs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cache-handler.mjs b/cache-handler.mjs index 9e93ca1..b1958d3 100644 --- a/cache-handler.mjs +++ b/cache-handler.mjs @@ -1,4 +1,14 @@ -import redis from './lib/redis.js'; +import Redis from 'ioredis'; + +let redis; + +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) {