Skip to content

Commit

Permalink
One less call when dealing with lru cache for ttl result and ensure r…
Browse files Browse the repository at this point in the history
…eset keys cleared
  • Loading branch information
ajayyy committed Sep 14, 2024
1 parent 17059fd commit 8ba68e1
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/utils/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ if (config.redis?.enabled) {

activeRequestPromises[key as string] = request;

void request.finally(() => delete activeRequestPromises[key as string]);
void request.finally(() => {
delete activeRequestPromises[key as string];

resetKeys.delete(key);
});

return request;
};
Expand Down Expand Up @@ -202,11 +206,12 @@ if (config.redis?.enabled) {

const ttl = client.ttl.bind(client);
exportClient.ttl = async (key) => {
if (cache && cacheClient && ttlCache.has(key)) {
const ttlResult = cache && cacheClient && ttlCache.get(key);
if (ttlResult != null) {
// Trigger usage of cache
cache.get(key);

return ttlCache.get(key) + config.redis?.expiryTime - Math.floor(Date.now() / 1000);
return ttlResult + config.redis?.expiryTime - Math.floor(Date.now() / 1000);
} else {
const result = await ttl(createKeyName(key));
if (ttlCache) ttlCache.set(key, Math.floor(Date.now() / 1000) - (config.redis?.expiryTime - result));
Expand Down

0 comments on commit 8ba68e1

Please sign in to comment.