mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-16 10:46:14 +01:00
store category recent tids for faster retrieval
This commit is contained in:
@@ -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([
|
||||
|
||||
Reference in New Issue
Block a user