feat: cache category tag whitelist

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 81aa3a0b8a
commit 78fa734017

@ -104,6 +104,7 @@ module.exports = function (Categories) {
'cid:0:children',
'cid:' + results.parentCid + ':children',
'cid:' + cid + ':children',
'cid:' + cid + ':tag:whitelist',
]);
next();
});

@ -178,8 +178,32 @@ Categories.getCategories = function (cids, uid, callback) {
};
Categories.getTagWhitelist = function (cids, callback) {
const keys = cids.map(cid => 'cid:' + cid + ':tag:whitelist');
db.getSortedSetsMembers(keys, callback);
const cachedData = {};
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) {

@ -137,6 +137,10 @@ module.exports = function (Categories) {
var scores = tags.map((tag, index) => index);
db.sortedSetAdd('cid:' + cid + ':tag:whitelist', scores, tags, next);
},
function (next) {
cache.del('cid:' + cid + ':tag:whitelist');
next();
},
], callback);
}

@ -7,6 +7,7 @@ var validator = require('validator');
var _ = require('lodash');
var db = require('../database');
var meta = require('../meta');
var categories = require('../categories');
var plugins = require('../plugins');
var utils = require('../utils');
var batch = require('../batch');
@ -59,13 +60,13 @@ module.exports = function (Topics) {
Topics.getTopicField(tid, 'cid', next);
},
function (cid, next) {
db.getSortedSetRange('cid:' + cid + ':tag:whitelist', 0, -1, next);
categories.getTagWhitelist([cid], next);
},
function (tagWhitelist, next) {
if (!tagWhitelist.length) {
if (!Array.isArray(tagWhitelist[0]) || !tagWhitelist[0].length) {
return next(null, tags);
}
var whitelistSet = new Set(tagWhitelist);
const whitelistSet = new Set(tagWhitelist[0]);
tags = tags.filter(tag => whitelistSet.has(tag));
next(null, tags);
},
@ -398,14 +399,14 @@ module.exports = function (Topics) {
async.waterfall([
function (next) {
if (parseInt(cid, 10)) {
db.getSortedSetRange('cid:' + cid + ':tag:whitelist', 0, -1, next);
categories.getTagWhitelist([cid], next);
} else {
setImmediate(next, null, []);
}
},
function (tagWhitelist, next) {
if (tagWhitelist.length) {
setImmediate(next, null, tagWhitelist);
if (Array.isArray(tagWhitelist[0]) && tagWhitelist[0].length) {
setImmediate(next, null, tagWhitelist[0]);
} else {
db.getSortedSetRevRange('tags:topic:count', 0, -1, next);
}

Loading…
Cancel
Save