Skip to content

Commit

Permalink
Cache the number of replies in the post object. See NodeBB#5050.
Browse files Browse the repository at this point in the history
  • Loading branch information
BenLubar committed Oct 14, 2016
1 parent 6e5a556 commit bbfc52b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 17 deletions.
10 changes: 0 additions & 10 deletions src/posts.js
Expand Up @@ -260,16 +260,6 @@ var plugins = require('./plugins');
});
};

Posts.countReplies = function(pid, callback) {
if (Array.isArray(pid)) {
db.sortedSetsCard(pid.map(function (id) {
return 'pid:' + id + ':replies';
}), callback);
} else {
db.sortedSetCard('pid:' + pid + ':replies', callback);
}
};

Posts.getReplyPids = function(pid, callback) {
db.getSortedSetRange('pid:' + pid + ':replies', 0, -1, callback);
};
Expand Down
5 changes: 4 additions & 1 deletion src/posts/create.js
Expand Up @@ -86,7 +86,10 @@ module.exports = function (Posts) {
if (!postData.toPid) {
return next(null);
}
db.sortedSetAdd('pid:' + postData.toPid + ':replies', timestamp, postData.pid, next);
async.parallel([
async.apply(db.sortedSetAdd, 'pid:' + postData.toPid + ':replies', timestamp, postData.pid),
async.apply(db.incrObjectField, 'post:' + postData.toPid, 'replies')
], next);
},
function (next) {
db.incrObjectField('global', 'postCount', next);
Expand Down
5 changes: 4 additions & 1 deletion src/posts/delete.js
Expand Up @@ -145,7 +145,10 @@ module.exports = function (Posts) {
if (!parseInt(toPid, 10)) {
return next(null);
}
db.sortedSetRemove('pid:' + toPid + ':replies', pid, next);
async.parallel([
async.apply(db.sortedSetRemove, 'pid:' + toPid + ':replies', pid),
async.apply(db.decrObjectField, 'post:' + toPid, 'replies')
], next);
});
},
function (next) {
Expand Down
5 changes: 1 addition & 4 deletions src/topics/posts.js
Expand Up @@ -65,9 +65,6 @@ module.exports = function (Topics) {
voteData: function (next) {
posts.getVoteStatusByPostIDs(pids, uid, next);
},
replies: function (next) {
posts.countReplies(pids, next);
},
userData: function (next) {
var uids = [];

Expand Down Expand Up @@ -126,7 +123,7 @@ module.exports = function (Topics) {
postObj.upvoted = results.voteData.upvotes[i];
postObj.downvoted = results.voteData.downvotes[i];
postObj.votes = postObj.votes || 0;
postObj.replies = results.replies[i] || 0;
postObj.replies = postObj.replies || 0;
postObj.selfPost = !!parseInt(uid, 10) && parseInt(uid, 10) === parseInt(postObj.uid, 10);

// Username override for guests, if enabled
Expand Down
5 changes: 4 additions & 1 deletion src/upgrade.js
Expand Up @@ -927,7 +927,10 @@ Upgrade.upgrade = function (callback) {
if (!parseInt(post.toPid, 10)) {
return next(null);
}
db.sortedSetAdd('pid:' + postData.toPid + ':replies', postData.timestamp, postData.pid, next);
async.parallel([
async.apply(db.sortedSetAdd, 'pid:' + postData.toPid + ':replies', postData.timestamp, postData.pid),
async.apply(db.incrObjectField, 'post:' + postData.toPid, 'replies')
], next);
}, next);
});
}, function(err) {
Expand Down

0 comments on commit bbfc52b

Please sign in to comment.