cache categories:cid and cid:<cid>:children

these rarely change, no need to go to db for them
This commit is contained in:
Barış Soner Uşaklı
2018-11-27 19:38:28 -05:00
parent 7357926fe7
commit 00a066985a
15 changed files with 168 additions and 43 deletions

View File

@@ -14,9 +14,6 @@ module.exports = function (User) {
};
User.getWatchedCategories = function (uid, callback) {
if (parseInt(uid, 10) <= 0) {
return setImmediate(callback, null, []);
}
async.waterfall([
function (next) {
async.parallel({
@@ -24,16 +21,13 @@ module.exports = function (User) {
User.getIgnoredCategories(uid, next);
},
all: function (next) {
db.getSortedSetRange('categories:cid', 0, -1, next);
categories.getAllCidsFromSet('categories:cid', next);
},
}, next);
},
function (results, next) {
const ignored = new Set(results.ignored);
var watched = results.all.filter(function (cid) {
return cid && !ignored.has(String(cid));
});
const watched = results.all.filter(cid => cid && !ignored.has(String(cid)));
next(null, watched);
},
], callback);

View File

@@ -7,6 +7,7 @@ var groups = require('../groups');
var plugins = require('../plugins');
var db = require('../database');
var privileges = require('../privileges');
var categories = require('../categories');
var meta = require('../meta');
var User = module.exports;
@@ -288,7 +289,7 @@ User.getAdminsandGlobalModsandModerators = function (callback) {
User.getModeratorUids = function (callback) {
async.waterfall([
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
async.apply(categories.getAllCidsFromSet, 'categories:cid'),
function (cids, next) {
var groupNames = cids.reduce(function (memo, cid) {
memo.push('cid:' + cid + ':privileges:moderate');
@@ -321,19 +322,20 @@ User.getModeratorUids = function (callback) {
};
User.getModeratedCids = function (uid, callback) {
if (parseInt(uid, 10) <= 0) {
return setImmediate(callback, null, []);
}
var cids;
async.waterfall([
function (next) {
db.getSortedSetRange('categories:cid', 0, -1, next);
categories.getAllCidsFromSet('categories:cid', next);
},
function (_cids, next) {
cids = _cids;
User.isModerator(uid, cids, next);
},
function (isMods, next) {
cids = cids.filter(function (cid, index) {
return cid && isMods[index];
});
cids = cids.filter((cid, index) => cid && isMods[index]);
next(null, cids);
},
], callback);