From ee920f942fac299ad590fda9eb6aa4ba9fee8ac6 Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Thu, 14 Nov 2019 15:08:56 +0700 Subject: [PATCH] Cache system & disk stats longer System stats will be cached for only 1000 ms (1 s) Disk stats will be cached for 60000 ms (60 s) --- controllers/utilsController.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/controllers/utilsController.js b/controllers/utilsController.js index 0ae0ff5..e0730d5 100644 --- a/controllers/utilsController.js +++ b/controllers/utilsController.js @@ -34,11 +34,13 @@ const self = { const statsCache = { system: { cache: null, - generating: false + generating: false, + generatedAt: 0 }, disk: { cache: null, - generating: false + generating: false, + generatedAt: 0 }, albums: { cache: null, @@ -539,10 +541,12 @@ self.stats = async (req, res, next) => { // System info if (!statsCache.system.cache && statsCache.system.generating) { stats.system = false - } else if (statsCache.system.generating) { + } else if (((Date.now() - statsCache.system.generatedAt) <= 1000) || statsCache.system.generating) { + // Use cache for 1000 ms (1 second) stats.system = statsCache.system.cache } else { statsCache.system.generating = true + statsCache.system.generatedAt = Date.now() const currentLoad = await si.currentLoad() const mem = await si.mem() @@ -574,10 +578,12 @@ self.stats = async (req, res, next) => { if (os.platform === 'linux') if (!statsCache.disk.cache && statsCache.disk.generating) { stats.disk = false - } else if (statsCache.disk.generating) { + } else if (((Date.now() - statsCache.disk.generatedAt) <= 60000) || statsCache.disk.generating) { + // Use cache for 60000 ms (60 seconds) stats.disk = statsCache.disk.cache } else { statsCache.disk.generating = true + statsCache.disk.generatedAt = Date.now() stats.disk = { _types: {