Skip to content

Node.js based forum software built for the modern web

Cache the number of replies in the post object. See #5050.

Ben Lubar123c66e3ecce
diff --git a/src/posts.js b/src/posts.js
index 0abba7e5a7..851735a9ba 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -260,21 +260,11 @@ 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) {
+ Posts.getReplyPids = function (pid, callback) {
db.getSortedSetRange('pid:' + pid + ':replies', 0, -1, callback);
};
- Posts.getReplyPosts = function(pid, uid, callback) {
+ Posts.getReplyPosts = function (pid, uid, callback) {
async.waterfall([
function (next) {
Posts.getReplyPids(pid, next);
diff --git a/src/posts/create.js b/src/posts/create.js
index 1c24280f25..20df3768e4 100644
--- a/src/posts/create.js
+++ b/src/posts/create.js
@@ -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);
diff --git a/src/posts/delete.js b/src/posts/delete.js
index a6b042e768..ada4e2ec6b 100644
--- a/src/posts/delete.js
+++ b/src/posts/delete.js
@@ -137,7 +137,7 @@ module.exports = function (Posts) {
function (next) {
deletePostFromUsersVotes(pid, next);
},
- function(next) {
+ function (next) {
Posts.getPostField(pid, 'toPid', function (err, toPid) {
if (err) {
return next(err);
@@ -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) {
diff --git a/src/topics/posts.js b/src/topics/posts.js
index 159030b56e..c5140f633a 100644
--- a/src/topics/posts.js
+++ b/src/topics/posts.js
@@ -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 = [];
@@ -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
diff --git a/src/upgrade.js b/src/upgrade.js
index d3547aa280..d542644639 100644
--- a/src/upgrade.js
+++ b/src/upgrade.js
@@ -908,7 +908,7 @@ Upgrade.upgrade = function (callback) {
next();
}
},
- function(next) {
+ function (next) {
thisSchemaDate = Date.UTC(2016, 9, 14);
if (schemaDate < thisSchemaDate) {
@@ -917,20 +917,23 @@ Upgrade.upgrade = function (callback) {
var posts = require('./posts');
var batch = require('./batch');
- batch.processSortedSet('posts:pid', function(ids, next) {
- posts.getPostsFields(ids, ['pid', 'toPid', 'timestamp'], function(err, data) {
+ batch.processSortedSet('posts:pid', function (ids, next) {
+ posts.getPostsFields(ids, ['pid', 'toPid', 'timestamp'], function (err, data) {
if (err) {
return next(err);
}
- async.each(data, function(postData, next) {
+ async.each(data, function (postData, next) {
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) {
+ }, function (err) {
if (err) {
return next(err);
}