Update others/scaler.mjs

This commit is contained in:
sent 2025-05-20 21:47:52 -07:00
parent c87b94b3c6
commit d3875151f8

View File

@ -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);
scaleCache();
export default cache;