From ecf721aac1ca4cd30d4b0f309a9dfdf82361d6a0 Mon Sep 17 00:00:00 2001 From: sent Date: Tue, 20 May 2025 22:12:31 -0700 Subject: [PATCH] Update --- others/scaler.mjs | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/others/scaler.mjs b/others/scaler.mjs index 24444d12..15ac37c9 100644 --- a/others/scaler.mjs +++ b/others/scaler.mjs @@ -1,31 +1,36 @@ -import { LRUCache } from "lru-cache"; +import { LRUCache } from 'lru-cache'; -let maxKeys = 10000; +let maxKeys = 10_000; +let cache = makeCache(maxKeys); -const cache = new LRUCache({ - max: maxKeys, - ttl: 4 * 24 * 60 * 60 * 1000, - allowStale: false, - updateAgeOnGet: false, - updateAgeOnHas: false, -}); +function makeCache(maxEntries) { + return new LRUCache({ + maxSize: maxEntries, + ttl: 4 * 24 * 60 * 60 * 1_000, + allowStale: false, + updateAgeOnGet: false, + updateAgeOnHas: false, + }); +} function scaleCache() { - const mem = process.memoryUsage(); - const freeHeap = mem.heapTotal - mem.heapUsed; - const ratio = freeHeap / mem.heapTotal; + const { heapUsed, heapTotal } = process.memoryUsage(); + const freeHeapRatio = (heapTotal - heapUsed) / heapTotal; - const newMax = ratio > 0.5 ? 20000 : ratio < 0.2 ? 5000 : 10000; + const newMax = freeHeapRatio > 0.5 + ? 20_000 + : freeHeapRatio < 0.2 + ? 5_000 + : 10_000; if (newMax !== maxKeys) { maxKeys = newMax; - cache.max = maxKeys; - console.log(`[SCALER] freeHeap ${(freeHeap / 1e6).toFixed(1)}MB → maxKeys: ${maxKeys}`); + cache = makeCache(maxKeys); + console.log(`[SCALER] freeHeap ${( (heapTotal - heapUsed)/1e6 ).toFixed(1)}MB → maxKeys: ${maxKeys}`); } } setInterval(scaleCache, 60_000); - scaleCache(); -export default cache; \ No newline at end of file +export { cache }; \ No newline at end of file