Cool Thingy
This commit is contained in:
parent
ff50de3064
commit
b622f3cb9b
|
@ -1,36 +1,37 @@
|
||||||
import { LRUCache } from 'lru-cache';
|
import fs from "fs";
|
||||||
|
import path from "path";
|
||||||
|
import { LRUCache } from "lru-cache";
|
||||||
|
|
||||||
let maxKeys = 10_000;
|
let maxKeys = 10_000;
|
||||||
let cache = makeCache(maxKeys);
|
let cache = makeCache(maxKeys);
|
||||||
|
|
||||||
function makeCache(maxEntries) {
|
function makeCache(maxEntries) {
|
||||||
return new LRUCache({
|
return new LRUCache({
|
||||||
maxSize: maxEntries,
|
maxSize: maxEntries,
|
||||||
ttl: 60_000,
|
ttl: 60_000,
|
||||||
allowStale: false,
|
allowStale: false,
|
||||||
updateAgeOnGet: false,
|
updateAgeOnGet: false,
|
||||||
updateAgeOnHas: false,
|
updateAgeOnHas: false
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function scaleCache() {
|
function scaleCache() {
|
||||||
const { heapUsed, heapTotal } = process.memoryUsage();
|
const { heapUsed, heapTotal } = process.memoryUsage();
|
||||||
const freeHeapRatio = (heapTotal - heapUsed) / heapTotal;
|
const freeHeapRatio = (heapTotal - heapUsed) / heapTotal;
|
||||||
|
const newMax = freeHeapRatio > 0.5 ? 20_000 : freeHeapRatio < 0.2 ? 5_000 : 10_000;
|
||||||
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 = makeCache(maxKeys);
|
cache = makeCache(maxKeys);
|
||||||
console.log(`[SCALER] freeHeap ${( (heapTotal - heapUsed)/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 { cache };
|
const watchPath = path.join(process.cwd(), "public");
|
||||||
|
fs.watch(watchPath, { recursive: true }, () => {
|
||||||
|
cache.clear();
|
||||||
|
});
|
||||||
|
|
||||||
|
export { cache };
|
||||||
|
|
Loading…
Reference in New Issue
Block a user