diff --git a/apps/popconlaunch/boot.js b/apps/popconlaunch/boot.js index eb3f18e1b..9124344b2 100644 --- a/apps/popconlaunch/boot.js +++ b/apps/popconlaunch/boot.js @@ -1,6 +1,7 @@ { var oldRead_1 = require("Storage").readJSON; - var monthAgo_1 = Date.now() - 1000 * 86400 * 28; + var oneMonth_1 = 1000 * 86400 * 28; + var monthAgo_1 = Date.now() - oneMonth_1; var cache_1; var ensureCache_1 = function () { if (!cache_1) { @@ -10,8 +11,20 @@ } return cache_1; }; - var saveCache_1 = function (orderChanged) { - require("Storage").writeJSON("popcon.cache.json", cache_1); + var trimCache_1 = function (cache) { + var threeMonthsBack = Date.now() - oneMonth_1 * 3; + var del = []; + for (var k in cache) + if (cache[k].last < threeMonthsBack) + del.push(k); + for (var _i = 0, del_1 = del; _i < del_1.length; _i++) { + var k = del_1[_i]; + delete cache[k]; + } + }; + var saveCache_1 = function (cache, orderChanged) { + trimCache_1(cache); + require("Storage").writeJSON("popcon.cache.json", cache); if (orderChanged) { var info = oldRead_1("popcon.info", true); info.cacheBuster = !info.cacheBuster; @@ -74,7 +87,7 @@ ent.pop++; ent.last = Date.now(); var orderChanged = sortCache_1(); - saveCache_1(orderChanged); + saveCache_1(cache_3, orderChanged); } return oldLoad_1(src); }; diff --git a/apps/popconlaunch/boot.ts b/apps/popconlaunch/boot.ts index a7ac73518..306d4844a 100644 --- a/apps/popconlaunch/boot.ts +++ b/apps/popconlaunch/boot.ts @@ -1,9 +1,6 @@ { type Timestamp = number; - -const oldRead = require("Storage").readJSON; -const monthAgo = Date.now() - 1000 * 86400 * 28; -let cache: undefined | { +type Cache = { [key: string]: { sortorder: number, pop: number, // amount of launches @@ -11,6 +8,11 @@ let cache: undefined | { } }; +const oldRead = require("Storage").readJSON; +const oneMonth = 1000 * 86400 * 28; +const monthAgo = Date.now() - oneMonth; +let cache: undefined | Cache; + const ensureCache = (): NonNull => { if(!cache){ cache = oldRead("popcon.cache.json", true); @@ -20,7 +22,19 @@ const ensureCache = (): NonNull => { return cache; }; -const saveCache = (orderChanged: boolean) => { +const trimCache = (cache: Cache) => { + const threeMonthsBack = Date.now() - oneMonth * 3; + const del = []; + for(const k in cache) + if(cache[k]!.last < threeMonthsBack) + del.push(k); + + for(const k of del) + delete cache[k]; +}; + +const saveCache = (cache: Cache, orderChanged: boolean) => { + trimCache(cache); require("Storage").writeJSON("popcon.cache.json", cache); if(orderChanged){ // ensure launchers reload their caches: @@ -94,7 +108,7 @@ global.load = (src: string) => { ent.pop++; ent.last = Date.now(); const orderChanged = sortCache(); - saveCache(orderChanged); + saveCache(cache, orderChanged); } return oldLoad(src);