From cb50b3f441310cdcfb3ed27bbcf1321dddfff42f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 21 Aug 2019 01:20:02 -0400 Subject: [PATCH] feat: cache stats, mongo count sucks --- src/controllers/admin/dashboard.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/controllers/admin/dashboard.js b/src/controllers/admin/dashboard.js index 885d045602..43dbb8b4b5 100644 --- a/src/controllers/admin/dashboard.js +++ b/src/controllers/admin/dashboard.js @@ -108,6 +108,12 @@ dashboardController.getAnalytics = async (req, res, next) => { }; async function getStats() { + const cache = require('../../cache'); + const cachedStats = cache.get('admin:stats'); + if (cachedStats !== undefined) { + return cachedStats; + } + const results = await Promise.all([ getStatsForSet('ip:recent', 'uniqueIPCount'), getStatsForSet('users:joindate', 'userCount'), @@ -118,6 +124,7 @@ async function getStats() { results[1].name = '[[admin/general/dashboard:users]]'; results[2].name = '[[admin/general/dashboard:posts]]'; results[3].name = '[[admin/general/dashboard:topics]]'; + cache.set('admin:stats', results, 600000); return results; } @@ -138,6 +145,7 @@ async function getStatsForSet(set, field) { thismonth: db.sortedSetCount(set, now - terms.month, '+inf'), alltime: getGlobalField(field), }); + function textClass(num) { if (num > 0) { return 'text-success';