Merge pull request #2738 from bobrippling/popcon-tweaks

popcon: trim old entries from the cache
master
Gordon Williams 2023-05-10 08:12:30 +01:00 committed by GitHub
commit 603b24e5f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 12 deletions

View File

@ -1 +1,2 @@
0.01: New App! 0.01: New App!
0.02: Trim old entries from the popcon app cache

View File

@ -3,6 +3,6 @@ Popcon
Display apps sorted by regular use. No config - install the app and all your launchers will sort apps by most popular, based off launch counts within the last month, and then sort them by the most recently launched app. Display apps sorted by regular use. No config - install the app and all your launchers will sort apps by most popular, based off launch counts within the last month, and then sort them by the most recently launched app.
:warning: Warning: this app overrides [`Storage.readJSON`], so may slow down your watch when using apps that perform I/O. ⚠️ Warning: this app overrides [`Storage.readJSON`], so may slow down your watch when using apps that perform I/O.
[`Storage.readJSON`]: https://www.espruino.com/ReferenceBANGLEJS2#l_Storage_readJSON [`Storage.readJSON`]: https://www.espruino.com/ReferenceBANGLEJS2#l_Storage_readJSON

View File

@ -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);
}; };

View File

@ -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);

View File

@ -2,7 +2,7 @@
"id": "popconlaunch", "id": "popconlaunch",
"name": "Popcon Launcher", "name": "Popcon Launcher",
"shortName": "Popcon", "shortName": "Popcon",
"version": "0.01", "version": "0.02",
"description": "Launcher modification - your launchers will display your favourite (popular) apps first. Overrides `readJSON`, may slow down your watch", "description": "Launcher modification - your launchers will display your favourite (popular) apps first. Overrides `readJSON`, may slow down your watch",
"readme": "README.md", "readme": "README.md",
"icon": "app.png", "icon": "app.png",