Improve resource cache key. (#190)

This commit is contained in:
NobyDa 2023-02-08 12:30:26 +08:00 committed by GitHub
parent 5de35c7720
commit 999271fa9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 17 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "sub-store", "name": "sub-store",
"version": "2.13.5", "version": "2.13.6",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js", "main": "src/main.js",
"scripts": { "scripts": {

View File

@ -16,8 +16,13 @@ class ResourceCache {
let clear = false; let clear = false;
Object.entries(this.resourceCache).forEach((entry) => { Object.entries(this.resourceCache).forEach((entry) => {
const [id, updated] = entry; const [id, updated] = entry;
if (new Date().getTime() - updated > this.expires) { if (!updated.time) {
// clear old version cache
delete this.resourceCache[id];
$.delete(`#${id}`); $.delete(`#${id}`);
clear = true;
}
if (new Date().getTime() - updated.time > this.expires) {
delete this.resourceCache[id]; delete this.resourceCache[id];
clear = true; clear = true;
} }
@ -26,9 +31,6 @@ class ResourceCache {
} }
revokeAll() { revokeAll() {
Object.keys(this.resourceCache).forEach((id) => {
$.delete(`#${id}`);
});
this.resourceCache = {}; this.resourceCache = {};
this._persist(); this._persist();
} }
@ -38,17 +40,16 @@ class ResourceCache {
} }
get(id) { get(id) {
const updated = this.resourceCache[id]; const updated = this.resourceCache[id] && this.resourceCache[id].time;
if (updated && new Date().getTime() - updated <= this.expires) { if (updated && new Date().getTime() - updated <= this.expires) {
return $.read(`#${id}`); return this.resourceCache[id].data;
} }
return null; return null;
} }
set(id, value) { set(id, value) {
this.resourceCache[id] = new Date().getTime(); this.resourceCache[id] = { time: new Date().getTime(), data: value }
this._persist(); this._persist();
$.write(value, `#${id}`);
} }
} }

View File

@ -17,8 +17,13 @@ class ResourceCache {
let clear = false; let clear = false;
Object.entries(this.resourceCache).forEach((entry) => { Object.entries(this.resourceCache).forEach((entry) => {
const [id, updated] = entry; const [id, updated] = entry;
if (new Date().getTime() - updated > this.expires) { if (!updated.time) {
// clear old version cache
delete this.resourceCache[id];
$.delete(`#${id}`); $.delete(`#${id}`);
clear = true;
}
if (new Date().getTime() - updated.time > this.expires) {
delete this.resourceCache[id]; delete this.resourceCache[id];
clear = true; clear = true;
} }
@ -27,9 +32,6 @@ class ResourceCache {
} }
revokeAll() { revokeAll() {
Object.keys(this.resourceCache).forEach((id) => {
$.delete(`#${id}`);
});
this.resourceCache = {}; this.resourceCache = {};
this._persist(); this._persist();
} }
@ -39,17 +41,16 @@ class ResourceCache {
} }
get(id) { get(id) {
const updated = this.resourceCache[id]; const updated = this.resourceCache[id] && this.resourceCache[id].time;
if (updated && new Date().getTime() - updated <= this.expires) { if (updated && new Date().getTime() - updated <= this.expires) {
return $.read(`#${id}`); return this.resourceCache[id].data;
} }
return null; return null;
} }
set(id, value) { set(id, value) {
this.resourceCache[id] = new Date().getTime(); this.resourceCache[id] = { time: new Date().getTime(), data: value }
this._persist(); this._persist();
$.write(value, `#${id}`);
} }
} }