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

@@ -7,6 +7,7 @@ var groups = require('../groups');
var plugins = require('../plugins');
var privileges = require('../privileges');
var utils = require('../utils');
var cache = require('../cache');
module.exports = function (Categories) {
Categories.create = function (data, callback) {
@@ -82,6 +83,7 @@ module.exports = function (Categories) {
], next);
},
function (results, next) {
cache.del(['categories:cid', 'cid:' + parentCid + ':children']);
if (data.cloneFromCid && parseInt(data.cloneFromCid, 10)) {
return Categories.copySettingsFrom(data.cloneFromCid, category.cid, !data.parentCid, next);
}
@@ -153,9 +155,11 @@ module.exports = function (Categories) {
const newParent = parseInt(results.source.parentCid, 10) || 0;
if (copyParent) {
tasks.push(async.apply(db.sortedSetRemove, 'cid:' + oldParent + ':children', toCid));
}
if (copyParent) {
tasks.push(async.apply(db.sortedSetAdd, 'cid:' + newParent + ':children', results.source.order, toCid));
tasks.push(function (next) {
cache.del(['cid:' + oldParent + ':children', 'cid:' + newParent + ':children']);
setImmediate(next);
});
}
destination.description = results.source.description;

View File

@@ -56,7 +56,7 @@ module.exports = function (Categories) {
Categories.getAllCategoryFields = function (fields, callback) {
async.waterfall([
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
async.apply(Categories.getAllCidsFromSet, 'categories:cid'),
function (cids, next) {
Categories.getCategoriesFields(cids, fields, next);
},

View File

@@ -7,6 +7,7 @@ var plugins = require('../plugins');
var topics = require('../topics');
var groups = require('../groups');
var privileges = require('../privileges');
var cache = require('../cache');
module.exports = function (Categories) {
Categories.purge = function (cid, uid, callback) {
@@ -94,7 +95,18 @@ module.exports = function (Categories) {
], next);
}, next);
},
], next);
], function (err) {
if (err) {
return next(err);
}
cache.del([
'categories:cid',
'cid:0:children',
'cid:' + results.parentCid + ':children',
'cid:' + cid + ':children',
]);
next();
});
},
], function (err) {
callback(err);

View File

@@ -9,6 +9,7 @@ var user = require('../user');
var Groups = require('../groups');
var plugins = require('../plugins');
var privileges = require('../privileges');
const cache = require('../cache');
var Categories = module.exports;
@@ -82,10 +83,25 @@ Categories.isIgnored = function (cids, uid, callback) {
db.isSortedSetMembers('uid:' + uid + ':ignored:cids', cids, callback);
};
Categories.getAllCidsFromSet = function (key, callback) {
const cids = cache.get(key);
if (cids) {
return setImmediate(callback, null, cids.slice());
}
db.getSortedSetRange(key, 0, -1, function (err, cids) {
if (err) {
return callback(err);
}
cache.set(key, cids);
callback(null, cids.slice());
});
};
Categories.getAllCategories = function (uid, callback) {
async.waterfall([
function (next) {
db.getSortedSetRange('categories:cid', 0, -1, next);
Categories.getAllCids(next);
},
function (cids, next) {
Categories.getCategories(cids, uid, next);
@@ -96,7 +112,7 @@ Categories.getAllCategories = function (uid, callback) {
Categories.getCidsByPrivilege = function (set, uid, privilege, callback) {
async.waterfall([
function (next) {
db.getSortedSetRange(set, 0, -1, next);
Categories.getAllCidsFromSet(set, next);
},
function (cids, next) {
privileges.categories.filterCids(privilege, cids, uid, next);
@@ -268,7 +284,7 @@ function getChildrenTree(category, uid, callback) {
}
Categories.getChildrenCids = function (rootCid, callback) {
var allCids = [];
let allCids = [];
function recursive(keys, callback) {
db.getSortedSetRange(keys, 0, -1, function (err, childrenCids) {
if (err) {
@@ -283,9 +299,19 @@ Categories.getChildrenCids = function (rootCid, callback) {
recursive(keys, callback);
});
}
const key = 'cid:' + rootCid + ':children';
const childrenCids = cache.get(key);
if (childrenCids) {
return setImmediate(callback, null, childrenCids.slice());
}
recursive('cid:' + rootCid + ':children', function (err) {
callback(err, _.uniq(allCids));
recursive(key, function (err) {
if (err) {
return callback(err);
}
allCids = _.uniq(allCids);
cache.set(key, allCids);
callback(null, allCids.slice());
});
};

View File

@@ -1,4 +1,3 @@
'use strict';
var async = require('async');
@@ -8,6 +7,7 @@ var meta = require('../meta');
var utils = require('../utils');
var translator = require('../translator');
var plugins = require('../plugins');
var cache = require('../cache');
module.exports = function (Categories) {
Categories.update = function (modified, callback) {
@@ -112,6 +112,10 @@ module.exports = function (Categories) {
function (next) {
db.setObjectField('category:' + cid, 'parentCid', newParent, next);
},
function (next) {
cache.del(['cid:' + oldParent + ':children', 'cid:' + newParent + ':children']);
next();
},
], next);
},
], function (err) {
@@ -149,6 +153,10 @@ module.exports = function (Categories) {
function (next) {
db.sortedSetAdd('cid:' + parentCid + ':children', order, cid, next);
},
function (next) {
cache.del(['categories:cid', 'cid:' + parentCid + ':children']);
next();
},
], next);
},
], function (err) {