mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-17 19:21:04 +01:00
feat: cache category tag whitelist
This commit is contained in:
@@ -104,6 +104,7 @@ module.exports = function (Categories) {
|
|||||||
'cid:0:children',
|
'cid:0:children',
|
||||||
'cid:' + results.parentCid + ':children',
|
'cid:' + results.parentCid + ':children',
|
||||||
'cid:' + cid + ':children',
|
'cid:' + cid + ':children',
|
||||||
|
'cid:' + cid + ':tag:whitelist',
|
||||||
]);
|
]);
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -178,8 +178,32 @@ Categories.getCategories = function (cids, uid, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Categories.getTagWhitelist = function (cids, callback) {
|
Categories.getTagWhitelist = function (cids, callback) {
|
||||||
const keys = cids.map(cid => 'cid:' + cid + ':tag:whitelist');
|
const cachedData = {};
|
||||||
db.getSortedSetsMembers(keys, callback);
|
|
||||||
|
const nonCachedCids = cids.filter((cid) => {
|
||||||
|
const data = cache.get('cid:' + cid + ':tag:whitelist');
|
||||||
|
const isInCache = data !== undefined;
|
||||||
|
if (isInCache) {
|
||||||
|
cachedData[cid] = data;
|
||||||
|
}
|
||||||
|
return !isInCache;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!nonCachedCids.length) {
|
||||||
|
return setImmediate(callback, null, _.clone(cids.map(cid => cachedData[cid])));
|
||||||
|
}
|
||||||
|
|
||||||
|
const keys = nonCachedCids.map(cid => 'cid:' + cid + ':tag:whitelist');
|
||||||
|
db.getSortedSetsMembers(keys, function (err, data) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
nonCachedCids.forEach((cid, index) => {
|
||||||
|
cachedData[cid] = data[index];
|
||||||
|
cache.set('cid:' + cid + ':tag:whitelist', data[index]);
|
||||||
|
});
|
||||||
|
callback(null, _.clone(cids.map(cid => cachedData[cid])));
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function calculateTopicPostCount(category) {
|
function calculateTopicPostCount(category) {
|
||||||
|
|||||||
@@ -137,6 +137,10 @@ module.exports = function (Categories) {
|
|||||||
var scores = tags.map((tag, index) => index);
|
var scores = tags.map((tag, index) => index);
|
||||||
db.sortedSetAdd('cid:' + cid + ':tag:whitelist', scores, tags, next);
|
db.sortedSetAdd('cid:' + cid + ':tag:whitelist', scores, tags, next);
|
||||||
},
|
},
|
||||||
|
function (next) {
|
||||||
|
cache.del('cid:' + cid + ':tag:whitelist');
|
||||||
|
next();
|
||||||
|
},
|
||||||
], callback);
|
], callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ var validator = require('validator');
|
|||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var db = require('../database');
|
var db = require('../database');
|
||||||
var meta = require('../meta');
|
var meta = require('../meta');
|
||||||
|
var categories = require('../categories');
|
||||||
var plugins = require('../plugins');
|
var plugins = require('../plugins');
|
||||||
var utils = require('../utils');
|
var utils = require('../utils');
|
||||||
var batch = require('../batch');
|
var batch = require('../batch');
|
||||||
@@ -59,13 +60,13 @@ module.exports = function (Topics) {
|
|||||||
Topics.getTopicField(tid, 'cid', next);
|
Topics.getTopicField(tid, 'cid', next);
|
||||||
},
|
},
|
||||||
function (cid, next) {
|
function (cid, next) {
|
||||||
db.getSortedSetRange('cid:' + cid + ':tag:whitelist', 0, -1, next);
|
categories.getTagWhitelist([cid], next);
|
||||||
},
|
},
|
||||||
function (tagWhitelist, next) {
|
function (tagWhitelist, next) {
|
||||||
if (!tagWhitelist.length) {
|
if (!Array.isArray(tagWhitelist[0]) || !tagWhitelist[0].length) {
|
||||||
return next(null, tags);
|
return next(null, tags);
|
||||||
}
|
}
|
||||||
var whitelistSet = new Set(tagWhitelist);
|
const whitelistSet = new Set(tagWhitelist[0]);
|
||||||
tags = tags.filter(tag => whitelistSet.has(tag));
|
tags = tags.filter(tag => whitelistSet.has(tag));
|
||||||
next(null, tags);
|
next(null, tags);
|
||||||
},
|
},
|
||||||
@@ -398,14 +399,14 @@ module.exports = function (Topics) {
|
|||||||
async.waterfall([
|
async.waterfall([
|
||||||
function (next) {
|
function (next) {
|
||||||
if (parseInt(cid, 10)) {
|
if (parseInt(cid, 10)) {
|
||||||
db.getSortedSetRange('cid:' + cid + ':tag:whitelist', 0, -1, next);
|
categories.getTagWhitelist([cid], next);
|
||||||
} else {
|
} else {
|
||||||
setImmediate(next, null, []);
|
setImmediate(next, null, []);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
function (tagWhitelist, next) {
|
function (tagWhitelist, next) {
|
||||||
if (tagWhitelist.length) {
|
if (Array.isArray(tagWhitelist[0]) && tagWhitelist[0].length) {
|
||||||
setImmediate(next, null, tagWhitelist);
|
setImmediate(next, null, tagWhitelist[0]);
|
||||||
} else {
|
} else {
|
||||||
db.getSortedSetRevRange('tags:topic:count', 0, -1, next);
|
db.getSortedSetRevRange('tags:topic:count', 0, -1, next);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user