store category recent tids for faster retrieval

This commit is contained in:
barisusakli
2016-09-22 18:21:49 +03:00
parent 930f3b28a5
commit a6c3be0bb1
4 changed files with 83 additions and 41 deletions

View File

@@ -32,6 +32,39 @@ module.exports = function(Categories) {
], callback);
};
Categories.updateRecentTid = function(cid, tid, callback) {
async.parallel({
count: function(next) {
db.sortedSetCard('cid:' + cid + ':recent_tids', next);
},
numRecentReplies: function(next) {
db.getObjectField('category:' + cid, 'numRecentReplies', next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
if (results.count < results.numRecentReplies) {
return db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, callback);
}
async.waterfall([
function(next) {
db.getSortedSetRangeWithScores('cid:' + cid + ':recent_tids', 0, results.count - results.numRecentReplies, next);
},
function(data, next) {
if (!data.length) {
return next();
}
db.sortedSetsRemoveRangeByScore(['cid:' + cid + ':recent_tids'], '-inf', data[data.length - 1].score, next);
},
function(next) {
db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, next);
}
], callback);
});
};
Categories.getRecentTopicReplies = function(categoryData, uid, callback) {
if (!Array.isArray(categoryData) || !categoryData.length) {
return callback();
@@ -39,7 +72,10 @@ module.exports = function(Categories) {
async.waterfall([
function(next) {
async.map(categoryData, getRecentTopicTids, next);
var keys = categoryData.map(function(category) {
return 'cid:' + category.cid + ':recent_tids';
});
db.getSortedSetsMembers(keys, next);
},
function(results, next) {
var tids = _.flatten(results);
@@ -62,45 +98,6 @@ module.exports = function(Categories) {
], callback);
};
function getRecentTopicTids(category, callback) {
var count = parseInt(category.numRecentReplies, 10);
if (!count) {
return callback(null, []);
}
if (count === 1) {
async.waterfall([
function (next) {
db.getSortedSetRevRange('cid:' + category.cid + ':pids', 0, 0, next);
},
function (pid, next) {
posts.getPostField(pid, 'tid', next);
},
function (tid, next) {
next(null, [tid]);
}
], callback);
return;
}
async.parallel({
pinnedTids: function(next) {
db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, -1, '+inf', Date.now(), next);
},
tids: function(next) {
db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, Math.max(1, count), Date.now(), '-inf', next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
results.tids = results.tids.concat(results.pinnedTids);
callback(null, results.tids);
});
}
function getTopics(tids, callback) {
var topicData;
async.waterfall([