Switch to Custom Cache Handler

This commit is contained in:
Ahmad 2025-01-25 23:05:01 -05:00
parent 8900584267
commit 5ef2a3c67f
No known key found for this signature in database
GPG key ID: 8FD8A93530D182BF
4 changed files with 34 additions and 78 deletions

View file

@ -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
}
}

View file

@ -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') {

View file

@ -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",

View file

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