fix(ACUC): do not refresh cache every set

This commit is contained in:
Gergő Móricz 2024-09-26 22:06:50 +02:00
parent 9bdd344b36
commit 2cb493321a
2 changed files with 6 additions and 3 deletions

View File

@ -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 // Cache for 10 minutes. This means that changing subscription tier could have
// a maximum of 10 minutes of a delay. - mogery // 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) { } catch (error) {
Logger.error(`Error updating cached ACUC: ${error}`); Logger.error(`Error updating cached ACUC: ${error}`);

View File

@ -35,12 +35,15 @@ redisRateLimitClient.on("connect", (err) => {
* @param {string} value The value to store. * @param {string} value The value to store.
* @param {number} [expire] Optional expiration time in seconds. * @param {number} [expire] Optional expiration time in seconds.
*/ */
const setValue = async (key: string, value: string, expire?: number) => { const setValue = async (key: string, value: string, expire?: number, nx = false) => {
if (expire) { if (expire && !nx) {
await redisRateLimitClient.set(key, value, "EX", expire); await redisRateLimitClient.set(key, value, "EX", expire);
} else { } else {
await redisRateLimitClient.set(key, value); await redisRateLimitClient.set(key, value);
} }
if (expire && nx) {
await redisRateLimitClient.expire(key, expire, "NX");
}
}; };
/** /**