mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-23 00:40:23 +01:00
refactor: posts cache to get rid of require in functions
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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'),
|
||||
|
||||
@@ -3,10 +3,30 @@
|
||||
const cacheCreate = require('../cache/lru');
|
||||
const meta = require('../meta');
|
||||
|
||||
module.exports = cacheCreate({
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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'),
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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'),
|
||||
|
||||
Reference in New Issue
Block a user