Change to ioredis and Update Caching

This commit is contained in:
Ahmad 2025-01-23 22:01:17 -05:00
parent 275e2e4e73
commit 15f879ebb0
No known key found for this signature in database
GPG key ID: 8FD8A93530D182BF
5 changed files with 91 additions and 104 deletions

View file

@ -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],
};