This commit is contained in:
sent 2025-05-20 22:12:31 -07:00
parent d3875151f8
commit ecf721aac1

View File

@ -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({ function makeCache(maxEntries) {
max: maxKeys, return new LRUCache({
ttl: 4 * 24 * 60 * 60 * 1000, maxSize: maxEntries,
allowStale: false, ttl: 4 * 24 * 60 * 60 * 1_000,
updateAgeOnGet: false, allowStale: false,
updateAgeOnHas: false, updateAgeOnGet: false,
}); updateAgeOnHas: false,
});
}
function scaleCache() { function scaleCache() {
const mem = process.memoryUsage(); const { heapUsed, heapTotal } = process.memoryUsage();
const freeHeap = mem.heapTotal - mem.heapUsed; const freeHeapRatio = (heapTotal - heapUsed) / heapTotal;
const ratio = freeHeap / mem.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) { if (newMax !== maxKeys) {
maxKeys = newMax; maxKeys = newMax;
cache.max = maxKeys; cache = makeCache(maxKeys);
console.log(`[SCALER] freeHeap ${(freeHeap / 1e6).toFixed(1)}MB → maxKeys: ${maxKeys}`); console.log(`[SCALER] freeHeap ${( (heapTotal - heapUsed)/1e6 ).toFixed(1)}MB → maxKeys: ${maxKeys}`);
} }
} }
setInterval(scaleCache, 60_000); setInterval(scaleCache, 60_000);
scaleCache(); scaleCache();
export default cache; export { cache };