From 78fbc42a0c321a709b2b6e6aa54966a49a023093 Mon Sep 17 00:00:00 2001 From: sent Date: Thu, 5 Jun 2025 21:25:37 -0700 Subject: [PATCH] Add others/scaler.mjs --- others/scaler.mjs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 others/scaler.mjs diff --git a/others/scaler.mjs b/others/scaler.mjs new file mode 100644 index 00000000..7b31e40f --- /dev/null +++ b/others/scaler.mjs @@ -0,0 +1,36 @@ +What about this import { LRUCache } from 'lru-cache'; + +let maxKeys = 10_000; +let cache = makeCache(maxKeys); + +function makeCache(maxEntries) { + return new LRUCache({ + maxSize: maxEntries, + ttl: 4 * 24 * 60 * 60 * 1_000, + allowStale: false, + updateAgeOnGet: false, + updateAgeOnHas: false, + }); +} + +function scaleCache() { + const { heapUsed, heapTotal } = process.memoryUsage(); + const freeHeapRatio = (heapTotal - heapUsed) / heapTotal; + + const newMax = freeHeapRatio > 0.5 + ? 20_000 + : freeHeapRatio < 0.2 + ? 5_000 + : 10_000; + + if (newMax !== maxKeys) { + maxKeys = newMax; + cache = makeCache(maxKeys); + console.log(`[SCALER] freeHeap ${( (heapTotal - heapUsed)/1e6 ).toFixed(1)}MB → maxKeys: ${maxKeys}`); + } +} + +setInterval(scaleCache, 60_000); +scaleCache(); + +export { cache }; \ No newline at end of file