mirror of
https://github.com/ahmadk953/tasko.git
synced 2025-05-04 04:33:10 +00:00
Change to ioredis and Update Caching
This commit is contained in:
parent
275e2e4e73
commit
15f879ebb0
5 changed files with 91 additions and 104 deletions
|
@ -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],
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue