From b622f3cb9b7a3f7706db0fb4902927c1e46e3d97 Mon Sep 17 00:00:00 2001 From: sent Date: Thu, 5 Jun 2025 21:18:57 -0700 Subject: [PATCH] Cool Thingy --- others/scaler.mjs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/others/scaler.mjs b/others/scaler.mjs index 36f867d7..9a2b936e 100644 --- a/others/scaler.mjs +++ b/others/scaler.mjs @@ -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 cache = makeCache(maxKeys); +let cache = makeCache(maxKeys); function makeCache(maxEntries) { return new LRUCache({ maxSize: maxEntries, - ttl: 60_000, - allowStale: false, + ttl: 60_000, + allowStale: false, updateAgeOnGet: false, - updateAgeOnHas: 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; - + 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}`); + 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 +const watchPath = path.join(process.cwd(), "public"); +fs.watch(watchPath, { recursive: true }, () => { + cache.clear(); +}); + +export { cache };