diff --git a/others/scaler.mjs b/others/scaler.mjs index ad418f10..24444d12 100644 --- a/others/scaler.mjs +++ b/others/scaler.mjs @@ -1,15 +1,31 @@ -import NodeCache from 'node-cache'; +import { LRUCache } from "lru-cache"; -const cache = new NodeCache({ stdTTL: 345600, checkperiod: 3600, useClones: false }); +let maxKeys = 10000; + +const cache = new LRUCache({ + max: maxKeys, + ttl: 4 * 24 * 60 * 60 * 1000, + allowStale: false, + updateAgeOnGet: false, + updateAgeOnHas: false, +}); function scaleCache() { const mem = process.memoryUsage(); const freeHeap = mem.heapTotal - mem.heapUsed; const ratio = freeHeap / mem.heapTotal; - const max = ratio > 0.5 ? 20000 : ratio < 0.2 ? 5000 : 10000; - cache.options.maxKeys = max; - console.log(`[SCALER] freeHeap ${(freeHeap/1e6).toFixed(1)}MB maxKeys → ${max}`); + const newMax = ratio > 0.5 ? 20000 : ratio < 0.2 ? 5000 : 10000; + + if (newMax !== maxKeys) { + maxKeys = newMax; + cache.max = maxKeys; + console.log(`[SCALER] freeHeap ${(freeHeap / 1e6).toFixed(1)}MB → maxKeys: ${maxKeys}`); + } } -setInterval(scaleCache, 60_000); \ No newline at end of file +setInterval(scaleCache, 60_000); + +scaleCache(); + +export default cache; \ No newline at end of file