Commit bf6b7648 authored by strong's avatar strong

话题查询增加10秒缓存

parent 063067ee
...@@ -348,6 +348,8 @@ exports.getThreadById = function(tid, callback) { ...@@ -348,6 +348,8 @@ exports.getThreadById = function(tid, callback) {
cb(null, null); cb(null, null);
return; return;
} }
getAllThreadByFidHelp(conditions, 1, 10, null, function(err, threads) { getAllThreadByFidHelp(conditions, 1, 10, null, function(err, threads) {
if (err) { if (err) {
console.error(err); console.error(err);
...@@ -401,26 +403,56 @@ exports.getThreadById = function(tid, callback) { ...@@ -401,26 +403,56 @@ exports.getThreadById = function(tid, callback) {
}, },
function(cb) { function(cb) {
//获取发帖人数 //获取发帖人数
ForumThread.aggregate({ var key = 'pisns-forum-thread:' + tid + ':amount';
$match: { redis.get(key,function(err,value){
pid: mongoose.Types.ObjectId(tid) if (value) {
} cb(null, Number(value));
}, {
$group: {
_id: {
from: '$from'
},
count: {
$sum: 1
}
}
}, function(err, data) {
if (err) {
cb(err, null);
} else { } else {
cb(null, data.length); ForumThread.aggregate({
$match: {
pid: mongoose.Types.ObjectId(tid)
}
}, {
$group: {
_id: {
from: '$from'
},
count: {
$sum: 1
}
}
}, function(err, data) {
if (err) {
cb(err, null);
} else {
redis.set(key, data.length);
redis.expire(key, 10);
cb(null, data.length);
}
});
} }
}); }
// ForumThread.aggregate({
// $match: {
// pid: mongoose.Types.ObjectId(tid)
// }
// }, {
// $group: {
// _id: {
// from: '$from'
// },
// count: {
// $sum: 1
// }
// }
// }, function(err, data) {
// if (err) {
// cb(err, null);
// } else {
// cb(null, data.length);
// }
// });
} }
], function(err, results) { ], function(err, results) {
if (err) { if (err) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment