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] 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"