From 5abd8d788bc570808b6b5885584bd61e3840f7a9 Mon Sep 17 00:00:00 2001 From: Observer KRypt0n_ Date: Wed, 5 Jan 2022 22:41:03 +0200 Subject: [PATCH] Fixed `Cache.set` work --- src/ts/core/Cache.ts | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/ts/core/Cache.ts b/src/ts/core/Cache.ts index 5c6760f..42cb7d7 100644 --- a/src/ts/core/Cache.ts +++ b/src/ts/core/Cache.ts @@ -24,17 +24,19 @@ export default class Cache return new Promise(async (resolve) => { if (this.cache !== null && this.cache[name] !== undefined) { + const expired = this.cache[name].ttl !== null ? Date.now() > this.cache[name].ttl * 1000 : false; + Debug.log({ function: 'Cache.get', message: [ - `Resolved ${this.cache[name].expired ? 'expired' : 'unexpired'} hot cache record`, + `Resolved ${expired ? 'expired' : 'unexpired'} hot cache record`, `[name] ${name}`, `[value]: ${JSON.stringify(this.cache[name].value)}` ] }); resolve({ - expired: this.cache[name].ttl !== null ? Date.now() > this.cache[name].ttl * 1000 : false, + expired: expired, value: this.cache[name].value }); } @@ -82,22 +84,6 @@ export default class Cache { return new Promise((resolve) => { constants.paths.cache.then((cacheFile) => { - if (this.cache === null) - { - Neutralino.filesystem.readFile(cacheFile) - .then((cacheRaw) => - { - this.cache = JSON.parse(cacheRaw); - - writeCache(); - }) - .catch(() => { - this.cache = {}; - - writeCache(); - }); - } - const writeCache = () => { Debug.log({ function: 'Cache.set', @@ -116,6 +102,24 @@ export default class Cache Neutralino.filesystem.writeFile(cacheFile, JSON.stringify(this.cache)) .then(() => resolve()); }; + + if (this.cache === null) + { + Neutralino.filesystem.readFile(cacheFile) + .then((cacheRaw) => + { + this.cache = JSON.parse(cacheRaw); + + writeCache(); + }) + .catch(() => { + this.cache = {}; + + writeCache(); + }); + } + + else writeCache(); }); }); }