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)
This commit is contained in:
Bobby Wibowo 2019-11-14 15:08:56 +07:00
parent 2a81e899b4
commit ee920f942f
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF

View File

@ -34,11 +34,13 @@ const self = {
const statsCache = { const statsCache = {
system: { system: {
cache: null, cache: null,
generating: false generating: false,
generatedAt: 0
}, },
disk: { disk: {
cache: null, cache: null,
generating: false generating: false,
generatedAt: 0
}, },
albums: { albums: {
cache: null, cache: null,
@ -539,10 +541,12 @@ self.stats = async (req, res, next) => {
// System info // System info
if (!statsCache.system.cache && statsCache.system.generating) { if (!statsCache.system.cache && statsCache.system.generating) {
stats.system = false 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 stats.system = statsCache.system.cache
} else { } else {
statsCache.system.generating = true statsCache.system.generating = true
statsCache.system.generatedAt = Date.now()
const currentLoad = await si.currentLoad() const currentLoad = await si.currentLoad()
const mem = await si.mem() const mem = await si.mem()
@ -574,10 +578,12 @@ self.stats = async (req, res, next) => {
if (os.platform === 'linux') if (os.platform === 'linux')
if (!statsCache.disk.cache && statsCache.disk.generating) { if (!statsCache.disk.cache && statsCache.disk.generating) {
stats.disk = false 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 stats.disk = statsCache.disk.cache
} else { } else {
statsCache.disk.generating = true statsCache.disk.generating = true
statsCache.disk.generatedAt = Date.now()
stats.disk = { stats.disk = {
_types: { _types: {