diff --git a/src/utils/redis.ts b/src/utils/redis.ts index 78349e1..e158250 100644 --- a/src/utils/redis.ts +++ b/src/utils/redis.ts @@ -76,6 +76,12 @@ const cache = config.redis.clientCacheSize ? new LRUCache({ + max: config.redis.clientCacheSize / 10 / 4, // 4 byte integer per element + ttl: 1000 * 60 * 30, + ttlResolution: 1000 * 60 * 15 +}) : null; // For redis let cacheConnectionClientId = ""; @@ -194,14 +200,17 @@ if (config.redis?.enabled) { }; const ttl = client.ttl.bind(client); - exportClient.ttl = (key) => { - if (cache && cacheClient && cache.has(key)) { + exportClient.ttl = async (key) => { + if (cache && cacheClient && ttlCache.has(key)) { // Trigger usage of cache cache.get(key); - return Promise.resolve(config.redis?.expiryTime - Math.floor((cache.ttl - cache.info(key).ttl) / 1000)); + return ttlCache.get(key) + config.redis?.expiryTime - Math.floor(Date.now() / 1000); } else { - return ttl(createKeyName(key)); + const result = await ttl(createKeyName(key)); + if (ttlCache) ttlCache.set(key, Math.floor(Date.now() / 1000) - (config.redis?.expiryTime - result)); + + return result; } }; @@ -401,6 +410,8 @@ async function setupCacheClientListener(cacheClient: RedisClientType, lastInvalidation = Date.now(); } + ttlCache.get(key); + // To tell it to not save the result of this currently running request if (key && activeRequestPromises[key] !== undefined) { resetKeys.add(key);