From 2e446392109458523f7580d3d63dc0d9b4402c31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sat, 7 Nov 2020 22:06:25 -0500 Subject: [PATCH] fix: guest header/footer cache allow clearing individual caches --- .../language/en-GB/admin/advanced/cache.json | 7 +- public/src/admin/advanced/cache.js | 6 +- src/controllers/admin/cache.js | 63 +++++----------- src/middleware/render.js | 20 ++++- src/socket.io/admin/cache.js | 18 +++-- src/views/admin/advanced/cache.tpl | 74 ++++++++++++------- 6 files changed, 103 insertions(+), 85 deletions(-) diff --git a/public/language/en-GB/admin/advanced/cache.json b/public/language/en-GB/admin/advanced/cache.json index cbbae7ad19..2fd7984883 100644 --- a/public/language/en-GB/admin/advanced/cache.json +++ b/public/language/en-GB/admin/advanced/cache.json @@ -2,10 +2,5 @@ "post-cache": "Post Cache", "percent-full": "%1% Full", "post-cache-size": "Post Cache Size", - "items-in-cache": "Items in Cache", - "control-panel": "Control Panel", - "update-settings": "Update Cache Settings", - "clear": "Clear", - "download": "Download", - "enabled": "Enabled" + "items-in-cache": "Items in Cache" } \ No newline at end of file diff --git a/public/src/admin/advanced/cache.js b/public/src/admin/advanced/cache.js index f1cab9af09..7a97a09e8d 100644 --- a/public/src/admin/advanced/cache.js +++ b/public/src/admin/advanced/cache.js @@ -7,14 +7,16 @@ define('admin/advanced/cache', function () { Settings.prepare(); }); - $('#clear').on('click', function () { - socket.emit('admin.cache.clear', function (err) { + $('.clear').on('click', function () { + var name = $(this).attr('data-name'); + socket.emit('admin.cache.clear', { name: name }, function (err) { if (err) { return app.alertError(err.message); } ajaxify.refresh(); }); }); + $('.checkbox').on('change', function () { var input = $(this).find('input'); var flag = input.is(':checked'); diff --git a/src/controllers/admin/cache.js b/src/controllers/admin/cache.js index e96dfffec3..fadec76338 100644 --- a/src/controllers/admin/cache.js +++ b/src/controllers/admin/cache.js @@ -9,56 +9,30 @@ cacheController.get = function (req, res) { const groupCache = require('../../groups').cache; const objectCache = require('../../database').objectCache; const localCache = require('../../cache'); - - let percentFull = 0; - if (postCache.itemCount > 0) { - percentFull = ((postCache.length / postCache.max) * 100).toFixed(2); + const headerFooterCache = require('../../middleware').headerFooterCache; + + function getInfo(cache) { + return { + length: cache.length, + max: cache.max, + itemCount: cache.itemCount, + percentFull: ((cache.length / cache.max) * 100).toFixed(2), + hits: utils.addCommas(String(cache.hits)), + misses: utils.addCommas(String(cache.misses)), + hitRatio: ((cache.hits / (cache.hits + cache.misses) || 0)).toFixed(4), + enabled: cache.enabled, + }; } const data = { - postCache: { - length: postCache.length, - max: postCache.max, - itemCount: postCache.itemCount, - percentFull: percentFull, - hits: utils.addCommas(String(postCache.hits)), - misses: utils.addCommas(String(postCache.misses)), - hitRatio: ((postCache.hits / (postCache.hits + postCache.misses) || 0)).toFixed(4), - enabled: postCache.enabled, - }, - groupCache: { - length: groupCache.length, - max: groupCache.max, - itemCount: groupCache.itemCount, - percentFull: ((groupCache.length / groupCache.max) * 100).toFixed(2), - hits: utils.addCommas(String(groupCache.hits)), - misses: utils.addCommas(String(groupCache.misses)), - hitRatio: (groupCache.hits / (groupCache.hits + groupCache.misses)).toFixed(4), - enabled: groupCache.enabled, - }, - localCache: { - length: localCache.length, - max: localCache.max, - itemCount: localCache.itemCount, - percentFull: ((localCache.length / localCache.max) * 100).toFixed(2), - hits: utils.addCommas(String(localCache.hits)), - misses: utils.addCommas(String(localCache.misses)), - hitRatio: ((localCache.hits / (localCache.hits + localCache.misses) || 0)).toFixed(4), - enabled: localCache.enabled, - }, + postCache: getInfo(postCache), + groupCache: getInfo(groupCache), + localCache: getInfo(localCache), + headerFooterCache: getInfo(headerFooterCache), }; if (objectCache) { - data.objectCache = { - length: objectCache.length, - max: objectCache.max, - itemCount: objectCache.itemCount, - percentFull: ((objectCache.length / objectCache.max) * 100).toFixed(2), - hits: utils.addCommas(String(objectCache.hits)), - misses: utils.addCommas(String(objectCache.misses)), - hitRatio: (objectCache.hits / (objectCache.hits + objectCache.misses)).toFixed(4), - enabled: objectCache.enabled, - }; + data.objectCache = getInfo(objectCache); } res.render('admin/advanced/cache', data); @@ -70,6 +44,7 @@ cacheController.dump = function (req, res, next) { object: require('../../database').objectCache, group: require('../../groups').cache, local: require('../../cache'), + headerfooter: require('../../middleware').headerFooterCache, }; if (!caches[req.query.name]) { return next(); diff --git a/src/middleware/render.js b/src/middleware/render.js index 5a8a239054..ae14a705c7 100644 --- a/src/middleware/render.js +++ b/src/middleware/render.js @@ -10,11 +10,18 @@ const translator = require('../translator'); const widgets = require('../widgets'); const utils = require('../utils'); const slugify = require('../slugify'); -const cache = require('../cache'); +const cacheCreate = require('../cacheCreate'); +const cache = cacheCreate({ + name: 'header-footer', + max: 1000, + maxAge: 0, +}); const relative_path = nconf.get('relative_path'); module.exports = function (middleware) { + middleware.headerFooterCache = cache; + middleware.processRender = function processRender(req, res, next) { // res.render post-processing, modified from here: https://gist.github.com/mrlannigan/5051687 const render = res.render; @@ -97,8 +104,15 @@ module.exports = function (middleware) { async function renderHeaderFooter(method, req, res, options) { let str = ''; const lang = getLang(req, res); + function getCacheKey() { + return [lang, method] + .concat(req.path.split('/').slice(0, 4)) + .join('/'); + } + let cacheKey; if (req.uid === 0 && res.locals.renderHeader) { - str = cache.get('render' + options.template.name + lang + method); + cacheKey = getCacheKey(); + str = cache.get(cacheKey); if (str) { return str; } @@ -114,7 +128,7 @@ module.exports = function (middleware) { } const translated = await translate(str, lang); if (req.uid === 0 && res.locals.renderHeader) { - cache.set('render' + options.template.name + lang + method, translated, 300000); + cache.set(cacheKey, translated, 300000); } return translated; } diff --git a/src/socket.io/admin/cache.js b/src/socket.io/admin/cache.js index ad057534cb..de16f8faaf 100644 --- a/src/socket.io/admin/cache.js +++ b/src/socket.io/admin/cache.js @@ -2,11 +2,18 @@ const SocketCache = module.exports; -SocketCache.clear = async function () { - require('../../posts/cache').reset(); - require('../../database').objectCache.reset(); - require('../../groups').cache.reset(); - require('../../cache').reset(); +SocketCache.clear = async function (socket, data) { + if (data.name === 'post') { + require('../../posts/cache').reset(); + } else if (data.name === 'object') { + require('../../database').objectCache.reset(); + } else if (data.name === 'group') { + require('../../groups').cache.reset(); + } else if (data.name === 'local') { + require('../../cache').reset(); + } else if (data.name === 'headerfooter') { + require('../../middleware').headerFooterCache.reset(); + } }; SocketCache.toggle = async function (socket, data) { @@ -15,6 +22,7 @@ SocketCache.toggle = async function (socket, data) { object: require('../../database').objectCache, group: require('../../groups').cache, local: require('../../cache'), + headerfooter: require('../../middleware').headerFooterCache, }; if (!caches[data.name]) { return; diff --git a/src/views/admin/advanced/cache.tpl b/src/views/admin/advanced/cache.tpl index 972491291e..32b7371fdb 100644 --- a/src/views/admin/advanced/cache.tpl +++ b/src/views/admin/advanced/cache.tpl @@ -1,15 +1,14 @@
-
+
-
+
-
[[admin/advanced/cache:post-cache]]
+
[[admin/advanced/cache:post-cache]]
@@ -31,20 +30,20 @@
- [[admin/advanced/cache:download]] + +
-
+
-
Object Cache
+
Object Cache
{objectCache.length} / {objectCache.max}
@@ -57,19 +56,20 @@ {objectCache.hits}
{objectCache.misses}
{objectCache.hitRatio}
- [[admin/advanced/cache:download]] + +
-
+ +
-
Group Cache
+
Group Cache
{groupCache.length} / {groupCache.max}
@@ -83,18 +83,19 @@ {groupCache.hits}
{groupCache.misses}
{groupCache.hitRatio}
- [[admin/advanced/cache:download]] + +
-
+ +
-
Local Cache
+
Local Cache
{localCache.length} / {localCache.max}
@@ -108,19 +109,42 @@ {localCache.hits}
{localCache.misses}
{localCache.hitRatio}
- [[admin/advanced/cache:download]] + +
-
-
-
-
-
[[admin/advanced/cache:control-panel]]
-
- - + +
+
+
Header Footer Cache
+
+
+ +
+ {headerFooterCache.length} / {headerFooterCache.max}
+ +
+
+ [[admin/advanced/cache:percent-full, {headerFooterCache.percentFull}]] +
+
+ + {headerFooterCache.hits}
+ {headerFooterCache.misses}
+ {headerFooterCache.hitRatio}
+ + +
+
+ + +