From 854e203d28b6e5b3a9a58ac4fb2d0793223d14f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20M=C3=B3ricz?= Date: Thu, 26 Sep 2024 22:06:50 +0200 Subject: [PATCH] fix(ACUC): do not refresh cache every set --- apps/api/src/controllers/auth.ts | 2 +- apps/api/src/services/redis.ts | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.ts index 91bfef1..1309447 100644 --- a/apps/api/src/controllers/auth.ts +++ b/apps/api/src/controllers/auth.ts @@ -61,7 +61,7 @@ export async function setCachedACUC(api_key: string, acuc: AuthCreditUsageChunk // Cache for 10 minutes. This means that changing subscription tier could have // a maximum of 10 minutes of a delay. - mogery - await setValue(cacheKeyACUC, JSON.stringify(acuc), 600); + await setValue(cacheKeyACUC, JSON.stringify(acuc), 600, true); }); } catch (error) { Logger.error(`Error updating cached ACUC: ${error}`); diff --git a/apps/api/src/services/redis.ts b/apps/api/src/services/redis.ts index 173d2b7..6080378 100644 --- a/apps/api/src/services/redis.ts +++ b/apps/api/src/services/redis.ts @@ -35,12 +35,15 @@ redisRateLimitClient.on("connect", (err) => { * @param {string} value The value to store. * @param {number} [expire] Optional expiration time in seconds. */ -const setValue = async (key: string, value: string, expire?: number) => { - if (expire) { +const setValue = async (key: string, value: string, expire?: number, nx = false) => { + if (expire && !nx) { await redisRateLimitClient.set(key, value, "EX", expire); } else { await redisRateLimitClient.set(key, value); } + if (expire && nx) { + await redisRateLimitClient.expire(key, expire, "NX"); + } }; /**