Skip to content

Node.js based forum software built for the modern web

refactor: posts cache to get rid of require in functions

Barış Soner Uşaklı236ac32303c7
diff --git a/src/api/posts.js b/src/api/posts.js
index 791fcdb598..2bf4a7c3d2 100644
--- a/src/api/posts.js
+++ b/src/api/posts.js
@@ -7,6 +7,7 @@ const db = require('../database');
const utils = require('../utils');
const user = require('../user');
const posts = require('../posts');
+const postsCache = require('../posts/cache');
const topics = require('../topics');
const groups = require('../groups');
const plugins = require('../plugins');
@@ -225,7 +226,7 @@ postsAPI.purge = async function (caller, data) {
if (!canPurge) {
throw new Error('[[error:no-privileges]]');
}
- require('../posts/cache').del(data.pid);
+ postsCache.del(data.pid);
await posts.purge(data.pid, caller.uid);
websockets.in(`topic_${postData.tid}`).emit('event:post_purged', postData);
diff --git a/src/controllers/admin/cache.js b/src/controllers/admin/cache.js
index 43d6f4ddca..2faad03fc2 100644
--- a/src/controllers/admin/cache.js
+++ b/src/controllers/admin/cache.js
@@ -6,7 +6,7 @@ const utils = require('../../utils');
const plugins = require('../../plugins');
cacheController.get = async function (req, res) {
- const postCache = require('../../posts/cache');
+ const postCache = require('../../posts/cache').getOrCreate();
const groupCache = require('../../groups').cache;
const { objectCache } = require('../../database');
const localCache = require('../../cache');
@@ -46,7 +46,7 @@ cacheController.get = async function (req, res) {
cacheController.dump = async function (req, res, next) {
let caches = {
- post: require('../../posts/cache'),
+ post: require('../../posts/cache').getOrCreate(),
object: require('../../database').objectCache,
group: require('../../groups').cache,
local: require('../../cache'),
diff --git a/src/posts/cache.js b/src/posts/cache.js
index 7f4711d0cd..7b029432df 100644
--- a/src/posts/cache.js
+++ b/src/posts/cache.js
@@ -3,10 +3,30 @@
const cacheCreate = require('../cache/lru');
const meta = require('../meta');
-module.exports = cacheCreate({
- name: 'post',
- maxSize: meta.config.postCacheSize,
- sizeCalculation: function (n) { return n.length || 1; },
- ttl: 0,
- enabled: global.env === 'production',
-});
+let cache = null;
+
+exports.getOrCreate = function () {
+ if (!cache) {
+ cache = cacheCreate({
+ name: 'post',
+ maxSize: meta.config.postCacheSize,
+ sizeCalculation: function (n) { return n.length || 1; },
+ ttl: 0,
+ enabled: global.env === 'production',
+ });
+ }
+
+ return cache;
+};
+
+exports.del = function (pid) {
+ if (cache) {
+ cache.del(pid);
+ }
+}
+
+exports.reset = function () {
+ if (cache) {
+ cache.reset();
+ }
+}
diff --git a/src/posts/parse.js b/src/posts/parse.js
index f36013dd77..4e16a111ad 100644
--- a/src/posts/parse.js
+++ b/src/posts/parse.js
@@ -10,6 +10,7 @@ const meta = require('../meta');
const plugins = require('../plugins');
const translator = require('../translator');
const utils = require('../utils');
+const postCache = require('./cache');
let sanitizeConfig = {
allowedTags: sanitize.defaults.allowedTags.concat([
@@ -52,7 +53,7 @@ module.exports = function (Posts) {
return postData;
}
postData.content = String(postData.content || '');
- const cache = require('./cache');
+ const cache = postCache.getOrCreate();
const pid = String(postData.pid);
const cachedContent = cache.get(pid);
if (postData.pid && cachedContent !== undefined) {
diff --git a/src/socket.io/admin/cache.js b/src/socket.io/admin/cache.js
index 1d382720f5..65ddfbefe1 100644
--- a/src/socket.io/admin/cache.js
+++ b/src/socket.io/admin/cache.js
@@ -7,7 +7,7 @@ const plugins = require('../../plugins');
SocketCache.clear = async function (socket, data) {
let caches = {
- post: require('../../posts/cache'),
+ post: require('../../posts/cache').getOrCreate(),
object: db.objectCache,
group: require('../../groups').cache,
local: require('../../cache'),
@@ -21,7 +21,7 @@ SocketCache.clear = async function (socket, data) {
SocketCache.toggle = async function (socket, data) {
let caches = {
- post: require('../../posts/cache'),
+ post: require('../../posts/cache').getOrCreate(),
object: db.objectCache,
group: require('../../groups').cache,
local: require('../../cache'),
diff --git a/src/socket.io/admin/plugins.js b/src/socket.io/admin/plugins.js
index b8890f9e61..2d6f705be9 100644
--- a/src/socket.io/admin/plugins.js
+++ b/src/socket.io/admin/plugins.js
@@ -5,12 +5,13 @@ const nconf = require('nconf');
const plugins = require('../../plugins');
const events = require('../../events');
const db = require('../../database');
+const postsCache = require('../../posts/cache');
const { pluginNamePattern } = require('../../constants');
const Plugins = module.exports;
Plugins.toggleActive = async function (socket, plugin_id) {
- require('../../posts/cache').reset();
+ postsCache.reset();
const data = await plugins.toggleActive(plugin_id);
await events.log({
type: `plugin-${data.active ? 'activate' : 'deactivate'}`,
@@ -21,7 +22,7 @@ Plugins.toggleActive = async function (socket, plugin_id) {
};
Plugins.toggleInstall = async function (socket, data) {
- require('../../posts/cache').reset();
+ postsCache.reset();
await plugins.checkWhitelist(data.id, data.version);
const pluginData = await plugins.toggleInstall(data.id, data.version);
await events.log({
diff --git a/test/mocks/databasemock.js b/test/mocks/databasemock.js
index 5cb8af1d34..507f29d6fc 100644
--- a/test/mocks/databasemock.js
+++ b/test/mocks/databasemock.js
@@ -194,7 +194,7 @@ async function setupMockDefaults() {
meta.config.autoDetectLang = 0;
require('../../src/groups').cache.reset();
- require('../../src/posts/cache').reset();
+ require('../../src/posts/cache').getOrCreate().reset();
require('../../src/cache').reset();
require('../../src/middleware/uploads').clearCache();
// privileges must be given after cache reset
diff --git a/test/socket.io.js b/test/socket.io.js
index eacab90ac5..6c0a5a2367 100644
--- a/test/socket.io.js
+++ b/test/socket.io.js
@@ -740,7 +740,7 @@ describe('socket.io', () => {
it('should toggle caches', async () => {
const caches = {
- post: require('../src/posts/cache'),
+ post: require('../src/posts/cache').getOrCreate(),
object: require('../src/database').objectCache,
group: require('../src/groups').cache,
local: require('../src/cache'),