From a9bbb586fcb3a1c61b5fb69052236e78cdf7d743 Mon Sep 17 00:00:00 2001 From: Opliko Date: Sun, 14 Aug 2022 02:35:59 +0200 Subject: [PATCH] feat: allow plugins to add to admin cache list (#10833) * feat: allow plugins to add to admin cache list resolves #10820 plugins will have to use `filter:admin.cache.get` hook and just add their own cache to the object there. * feat: add dump to ttlCache and expose properties * feat: also expose properties under their current names * feat: display TTL if set --- .../language/en-GB/admin/advanced/cache.json | 3 + src/cache/lru.js | 5 +- src/cache/ttl.js | 25 ++++ src/controllers/admin/cache.js | 27 +++-- src/socket.io/admin/cache.js | 22 ++-- src/views/admin/advanced/cache.tpl | 109 +++--------------- 6 files changed, 78 insertions(+), 113 deletions(-) diff --git a/public/language/en-GB/admin/advanced/cache.json b/public/language/en-GB/admin/advanced/cache.json index 2fd7984883..0148c2d889 100644 --- a/public/language/en-GB/admin/advanced/cache.json +++ b/public/language/en-GB/admin/advanced/cache.json @@ -1,5 +1,8 @@ { "post-cache": "Post Cache", + "group-cache": "Group Cache", + "local-cache": "Local Cache", + "object-cache": "Object Cache", "percent-full": "%1% Full", "post-cache-size": "Post Cache Size", "items-in-cache": "Items in Cache" diff --git a/src/cache/lru.js b/src/cache/lru.js index d0a92c46d5..3434e0773b 100644 --- a/src/cache/lru.js +++ b/src/cache/lru.js @@ -37,12 +37,15 @@ module.exports = function (opts) { cache.enabled = opts.hasOwnProperty('enabled') ? opts.enabled : true; const cacheSet = lruCache.set; - // backwards compatibility + // expose properties while keeping backwards compatibility const propertyMap = new Map([ ['length', 'calculatedSize'], + ['calculatedSize', 'calculatedSize'], ['max', 'max'], ['maxSize', 'maxSize'], ['itemCount', 'size'], + ['size', 'size'], + ['ttl', 'ttl'], ]); propertyMap.forEach((lruProp, cacheProp) => { Object.defineProperty(cache, cacheProp, { diff --git a/src/cache/ttl.js b/src/cache/ttl.js index 3b9d3fee77..be7e795616 100644 --- a/src/cache/ttl.js +++ b/src/cache/ttl.js @@ -13,6 +13,23 @@ module.exports = function (opts) { cache.enabled = opts.hasOwnProperty('enabled') ? opts.enabled : true; const cacheSet = ttlCache.set; + // expose properties + const propertyMap = new Map([ + ['max', 'max'], + ['itemCount', 'size'], + ['size', 'size'], + ['ttl', 'ttl'], + ]); + propertyMap.forEach((ttlProp, cacheProp) => { + Object.defineProperty(cache, cacheProp, { + get: function () { + return ttlCache[ttlProp]; + }, + configurable: true, + enumerable: true, + }); + }); + cache.set = function (key, value, ttl) { if (!cache.enabled) { return; @@ -90,5 +107,13 @@ module.exports = function (opts) { return unCachedKeys; }; + cache.dump = function () { + return Array.from(ttlCache.entries()); + }; + + cache.peek = function (key) { + return ttlCache.get(key, { updateAgeOnGet: false }); + }; + return cache; }; diff --git a/src/controllers/admin/cache.js b/src/controllers/admin/cache.js index 67c9864ae5..6d5c385a22 100644 --- a/src/controllers/admin/cache.js +++ b/src/controllers/admin/cache.js @@ -3,8 +3,9 @@ const cacheController = module.exports; const utils = require('../../utils'); +const plugins = require('../../plugins'); -cacheController.get = function (req, res) { +cacheController.get = async function (req, res) { const postCache = require('../../posts/cache'); const groupCache = require('../../groups').cache; const { objectCache } = require('../../database'); @@ -23,29 +24,33 @@ cacheController.get = function (req, res) { misses: utils.addCommas(String(cache.misses)), hitRatio: ((cache.hits / (cache.hits + cache.misses) || 0)).toFixed(4), enabled: cache.enabled, + ttl: cache.ttl, }; } - - const data = { - postCache: getInfo(postCache), - groupCache: getInfo(groupCache), - localCache: getInfo(localCache), + let caches = { + post: postCache, + group: groupCache, + local: localCache, }; - if (objectCache) { - data.objectCache = getInfo(objectCache); + caches.object = objectCache; + } + caches = await plugins.hooks.fire('filter:admin.cache.get', caches); + for (const [key, value] of Object.entries(caches)) { + caches[key] = getInfo(value); } - res.render('admin/advanced/cache', data); + res.render('admin/advanced/cache', { caches }); }; -cacheController.dump = function (req, res, next) { - const caches = { +cacheController.dump = async function (req, res, next) { + let caches = { post: require('../../posts/cache'), object: require('../../database').objectCache, group: require('../../groups').cache, local: require('../../cache'), }; + caches = await plugins.hooks.fire('filter:admin.cache.get', caches); if (!caches[req.query.name]) { return next(); } diff --git a/src/socket.io/admin/cache.js b/src/socket.io/admin/cache.js index 9188ac8f5b..1d382720f5 100644 --- a/src/socket.io/admin/cache.js +++ b/src/socket.io/admin/cache.js @@ -3,26 +3,30 @@ const SocketCache = module.exports; const db = require('../../database'); +const plugins = require('../../plugins'); SocketCache.clear = async function (socket, data) { - if (data.name === 'post') { - require('../../posts/cache').reset(); - } else if (data.name === 'object' && db.objectCache) { - db.objectCache.reset(); - } else if (data.name === 'group') { - require('../../groups').cache.reset(); - } else if (data.name === 'local') { - require('../../cache').reset(); + let caches = { + post: require('../../posts/cache'), + object: db.objectCache, + group: require('../../groups').cache, + local: require('../../cache'), + }; + caches = await plugins.hooks.fire('filter:admin.cache.get', caches); + if (!caches[data.name]) { + return; } + caches[data.name].reset(); }; SocketCache.toggle = async function (socket, data) { - const caches = { + let caches = { post: require('../../posts/cache'), object: db.objectCache, group: require('../../groups').cache, local: require('../../cache'), }; + caches = await plugins.hooks.fire('filter:admin.cache.get', caches); if (!caches[data.name]) { return; } diff --git a/src/views/admin/advanced/cache.tpl b/src/views/admin/advanced/cache.tpl index 3ac75a0d38..c5a2dc2143 100644 --- a/src/views/admin/advanced/cache.tpl +++ b/src/views/admin/advanced/cache.tpl @@ -2,118 +2,43 @@
+ {{{each caches}}}
-
[[admin/advanced/cache:post-cache]]
+
[[admin/advanced/cache:{@key}-cache]]
-
+
- {postCache.length} / {postCache.maxSize}
+ {{{if ../length}}}{../length}{{{else}}}{../itemCount}{{{end}}} / {{{if ../max}}}{../max}{{{else}}}{../maxSize}{{{end}}}
-
- [[admin/advanced/cache:percent-full, {postCache.percentFull}]] +
+ [[admin/advanced/cache:percent-full, {../percentFull}]]
- {postCache.hits}
- {postCache.misses}
- {postCache.hitRatio}
- + {../hits}
+ {../misses}
+ {../hitRatio}
+ {{{if ../ttl}}} {../ttl}
{{{end}}} + {{{if (@key == "post")}}}
-
- - -
-
-
- - -
-
-
Object Cache
-
-
- -
- {objectCache.itemCount} / {objectCache.max}
-
-
- [[admin/advanced/cache:percent-full, {objectCache.percentFull}]] -
-
- - {objectCache.hits}
- {objectCache.misses}
- {objectCache.hitRatio}
- - -
-
-
- - -
-
-
Group Cache
-
-
- -
- {groupCache.itemCount} / {groupCache.max}
- -
-
- [[admin/advanced/cache:percent-full, {groupCache.percentFull}]] -
-
- - {groupCache.hits}
- {groupCache.misses}
- {groupCache.hitRatio}
- - -
-
-
- -
-
-
Local Cache
-
-
- -
- {localCache.itemCount} / {localCache.max}
- -
-
- [[admin/advanced/cache:percent-full, {localCache.percentFull}]] -
-
- - {localCache.hits}
- {localCache.misses}
- {localCache.hitRatio}
- - + {{{end}}} + +
+ {{{end}}} +