popcon: trim old entries from the cache
parent
2f084b4c8a
commit
e3fed6ceb7
|
|
@ -1,6 +1,7 @@
|
||||||
{
|
{
|
||||||
var oldRead_1 = require("Storage").readJSON;
|
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 cache_1;
|
||||||
var ensureCache_1 = function () {
|
var ensureCache_1 = function () {
|
||||||
if (!cache_1) {
|
if (!cache_1) {
|
||||||
|
|
@ -10,8 +11,20 @@
|
||||||
}
|
}
|
||||||
return cache_1;
|
return cache_1;
|
||||||
};
|
};
|
||||||
var saveCache_1 = function (orderChanged) {
|
var trimCache_1 = function (cache) {
|
||||||
require("Storage").writeJSON("popcon.cache.json", cache_1);
|
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) {
|
if (orderChanged) {
|
||||||
var info = oldRead_1("popcon.info", true);
|
var info = oldRead_1("popcon.info", true);
|
||||||
info.cacheBuster = !info.cacheBuster;
|
info.cacheBuster = !info.cacheBuster;
|
||||||
|
|
@ -74,7 +87,7 @@
|
||||||
ent.pop++;
|
ent.pop++;
|
||||||
ent.last = Date.now();
|
ent.last = Date.now();
|
||||||
var orderChanged = sortCache_1();
|
var orderChanged = sortCache_1();
|
||||||
saveCache_1(orderChanged);
|
saveCache_1(cache_3, orderChanged);
|
||||||
}
|
}
|
||||||
return oldLoad_1(src);
|
return oldLoad_1(src);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,6 @@
|
||||||
{
|
{
|
||||||
type Timestamp = number;
|
type Timestamp = number;
|
||||||
|
type Cache = {
|
||||||
const oldRead = require("Storage").readJSON;
|
|
||||||
const monthAgo = Date.now() - 1000 * 86400 * 28;
|
|
||||||
let cache: undefined | {
|
|
||||||
[key: string]: {
|
[key: string]: {
|
||||||
sortorder: number,
|
sortorder: number,
|
||||||
pop: number, // amount of launches
|
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<typeof cache> => {
|
const ensureCache = (): NonNull<typeof cache> => {
|
||||||
if(!cache){
|
if(!cache){
|
||||||
cache = oldRead("popcon.cache.json", true);
|
cache = oldRead("popcon.cache.json", true);
|
||||||
|
|
@ -20,7 +22,19 @@ const ensureCache = (): NonNull<typeof cache> => {
|
||||||
return cache;
|
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);
|
require("Storage").writeJSON("popcon.cache.json", cache);
|
||||||
if(orderChanged){
|
if(orderChanged){
|
||||||
// ensure launchers reload their caches:
|
// ensure launchers reload their caches:
|
||||||
|
|
@ -94,7 +108,7 @@ global.load = (src: string) => {
|
||||||
ent.pop++;
|
ent.pop++;
|
||||||
ent.last = Date.now();
|
ent.last = Date.now();
|
||||||
const orderChanged = sortCache();
|
const orderChanged = sortCache();
|
||||||
saveCache(orderChanged);
|
saveCache(cache, orderChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
return oldLoad(src);
|
return oldLoad(src);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue