Commit fd749970 authored by 陈家荣's avatar 陈家荣

帖子修改排序号后及时在移动端更新效果

parent 18abca5d
...@@ -322,7 +322,7 @@ router.post('/thread/:tid/:fid/unTop', function(req, res, next) { ...@@ -322,7 +322,7 @@ router.post('/thread/:tid/:fid/unTop', function(req, res, next) {
} }
}); });
//文章推荐 //文章加精
router.post('/thread/:tid/:fid/recommend', function(req, res, next) { router.post('/thread/:tid/:fid/recommend', function(req, res, next) {
var tid = req.params.tid, var tid = req.params.tid,
fid = req.params.fid, fid = req.params.fid,
...@@ -364,7 +364,7 @@ router.post('/thread/:tid/:fid/recommend', function(req, res, next) { ...@@ -364,7 +364,7 @@ router.post('/thread/:tid/:fid/recommend', function(req, res, next) {
} }
}); });
//文章取消推荐 //文章取消加精
router.post('/thread/:tid/:fid/unRecommend', function(req, res, next) { router.post('/thread/:tid/:fid/unRecommend', function(req, res, next) {
var tid = req.params.tid; //文章ID var tid = req.params.tid; //文章ID
var fid = req.params.fid; //板块ID var fid = req.params.fid; //板块ID
...@@ -555,32 +555,43 @@ router.get('/threads/list', function(req, res, next) { ...@@ -555,32 +555,43 @@ router.get('/threads/list', function(req, res, next) {
//直接修改序号 //直接修改序号
function updateThreadTopIdx(id,idx,callback){ function updateThreadTopIdx(id,idx,callback){
ForumThread.update( // ForumThread.update({_id:id},{ top_order_idx: idx },
{_id:id}, // null,
{ top_order_idx: idx }, // function(err,result){
null, // if(err){
function(err,result){ // console.error(err);
if(err){ // callback(err,null);
console.error(err); // }else{
callback(err,null); // callback(null,true);
}else{ // }
callback(null,true); // });
} forumThreadService.updateThread({_id:id}, { top_order_idx: idx }, function(err, doc) {
}); if(err){
console.error(err);
callback(err,null);
}else{
callback(null,true);
}
});
} }
function updateThreadNewRecommendIdx(id,idx,callback){ function updateThreadNewRecommendIdx(id,idx,callback){
ForumThread.update( // ForumThread.update({_id:id},{ new_recommend_order_idx: idx },null, function(err,result){
{_id:id}, // if(err){
{ new_recommend_order_idx: idx }, // console.error(err);
null, // callback(err,null);
function(err,result){ // }else{
if(err){ // callback(null,true);
console.error(err); // }
callback(err,null); // });
}else{
callback(null,true); forumThreadService.updateThread({_id:id}, { new_recommend_order_idx: idx }, function(err, doc) {
} if(err){
}); console.error(err);
callback(err,null);
}else{
callback(null,true);
}
});
} }
router.put('/top_threads/orderIDX/:id/:order', function(req, res, next) { router.put('/top_threads/orderIDX/:id/:order', function(req, res, next) {
var id=req.params.id; var id=req.params.id;
......
...@@ -666,13 +666,16 @@ router.post('/thread/:tid/update', function(req, res, next) { ...@@ -666,13 +666,16 @@ router.post('/thread/:tid/update', function(req, res, next) {
router.post('/thread/:tid/delete', function(req, res, next) { router.post('/thread/:tid/delete', function(req, res, next) {
var tid = req.params.tid; var tid = req.params.tid;
if (tid) { if (tid) {
forumThreadService.deleteThreadById(tid, function(err, thread) { //注销此方法
if (err) {
res.json(returnCode.BUSY); // forumThreadService.deleteThreadById(tid, function(err, thread) {
} else { // if (err) {
res.json(returnCode.SUCCESS); // res.json(returnCode.BUSY);
} // } else {
}); // res.json(returnCode.SUCCESS);
// }
// });
res.json(returnCode.BUSY);
} else { } else {
res.json(returnCode.WRONG_PARAM); res.json(returnCode.WRONG_PARAM);
} }
...@@ -1756,8 +1759,6 @@ router.get('/thread/getThreadWithEssence', function(req, res, next) { ...@@ -1756,8 +1759,6 @@ router.get('/thread/getThreadWithEssence', function(req, res, next) {
var infoId = req.query.infoId || null; var infoId = req.query.infoId || null;
var ent_code = req.session.user.ent_code; var ent_code = req.session.user.ent_code;
var skip = (pageNo - 1) * pageSize; var skip = (pageNo - 1) * pageSize;
var redis_type = '_thread_essence';
var sort = { var sort = {
new_recommend:-1, new_recommend:-1,
new_recommend_order_idx:-1, new_recommend_order_idx:-1,
...@@ -1786,7 +1787,6 @@ router.get('/thread/getThreadWithEssence', function(req, res, next) { ...@@ -1786,7 +1787,6 @@ router.get('/thread/getThreadWithEssence', function(req, res, next) {
}] }]
}; };
if((pageNo * pageSize) > redisThreadList.getThreadRedisCount() || infoId){ if((pageNo * pageSize) > redisThreadList.getThreadRedisCount() || infoId){
if(infoId){ if(infoId){
conditions.info = infoId; conditions.info = infoId;
...@@ -1805,10 +1805,10 @@ router.get('/thread/getThreadWithEssence', function(req, res, next) { ...@@ -1805,10 +1805,10 @@ router.get('/thread/getThreadWithEssence', function(req, res, next) {
}); });
}else{ }else{
//更新推荐列表redis //更新推荐列表redis
redisThreadList.get(ent_code, "", redis_type, function(error, docs) { redisThreadList.get(ent_code, function(error, docs) {
var results = JSON.parse(docs); var results = JSON.parse(docs);
if(results.items == null || results.items.length <= 0){ //如果不存在,继续重新获取 if(results.items == null || results.items.length <= 0){ //如果不存在,继续重新获取
redisThreadList.getRedisEsenceThreads(ent_code, "", redis_type, function(error, docs){ redisThreadList.getRedisEsenceThreads(ent_code, function(error, docs){
if(error){ if(error){
console.error(err); console.error(err);
res.json(returnCode.BUSY); res.json(returnCode.BUSY);
...@@ -1945,7 +1945,7 @@ router.get('/thread/:tid/get/subThreads/:type', function(req, res, next) { ...@@ -1945,7 +1945,7 @@ router.get('/thread/:tid/get/subThreads/:type', function(req, res, next) {
if(type == 1){ //最新 if(type == 1){ //最新
sort = '-created'; sort = '-created';
}else if(type == 2){ //最热 }else if(type == 2){ //最热
sort = '-praise_count -comment_count';
} }
if (tid) { if (tid) {
......
...@@ -10,6 +10,8 @@ var forumCommentService = require('./forumCommentService'); ...@@ -10,6 +10,8 @@ var forumCommentService = require('./forumCommentService');
var async = require('async'); var async = require('async');
var then = require('thenjs'); var then = require('thenjs');
var redisThreadList = require('../utils/redisThreadList');
var listThreadFields = { var listThreadFields = {
content:1, content:1,
...@@ -53,8 +55,6 @@ var singleThreadFields = { ...@@ -53,8 +55,6 @@ var singleThreadFields = {
}; };
var subThreadFields = { var subThreadFields = {
from:1 from:1
}; };
...@@ -89,14 +89,33 @@ function handleContent(content) { ...@@ -89,14 +89,33 @@ function handleContent(content) {
return content.trim(); return content.trim();
} }
//更新帖子方法, afterOperation为更新之后做的操作的条件
function findOneAndUpdate(id, entity, callback) { function findOneAndUpdate(condition, entity, afterOperation, callback) {
ForumThread.findOneAndUpdate(id, entity, function(err, doc) { ForumThread.findOneAndUpdate(condition, entity, function(err, doc) {
callback(err, doc); callback(err, doc);
if(afterOperation == 'updateRedisRecommentThreads' && doc && doc.new_recommend == 1){ //刷新缓存
redisThreadList.updateRedisEsenceThreads(doc.ent_code);
}
}); });
}; };
//更新帖子方法, afterOperation为更新之后做的操作的条件
function update(condition, entity, operation, afterOperation, callback) {
ForumThread.update(condition, entity, operation,function(err, doc) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
if(afterOperation == 'updateRedisRecommentThreads' && doc && doc.new_recommend == 1){ //刷新缓存
redisThreadList.updateRedisEsenceThreads(doc.ent_code);
}
}
});
};
//分页查询话题列表 //分页查询话题列表
exports.findThread = function(pageNo, pageSize, q, sort, callback) { exports.findThread = function(pageNo, pageSize, q, sort, callback) {
then(function(cont) { then(function(cont) {
...@@ -550,17 +569,31 @@ exports.updateThreadById = function(tid, entity, callback) { ...@@ -550,17 +569,31 @@ exports.updateThreadById = function(tid, entity, callback) {
entity.share = ''; entity.share = '';
} }
if (entity.share) { if (entity.share) {
// ForumThread.update({
ForumThread.update({ // _id: tid
_id: tid // }, entity, null, function(err, result) {
}, entity, null, function(err, result) { // if (err) {
// console.error(err);
// callback(err, null);
// } else {
// ForumShare.update({
// _id: shareEntity._id
// }, shareEntity, null, function(err, se) {
// if (err) {
// console.error(err);
// callback(err, null);
// } else {
// callback(null, entity);
// }
// });
// }
// });
findOneAndUpdate({_id: tid}, entity, 'updateRedisRecommentThreads', function(err, result) {
if (err) { if (err) {
console.error(err); console.error(err);
callback(err, null); callback(err, null);
} else { } else {
ForumShare.update({ ForumShare.update({_id: shareEntity._id}, shareEntity, null, function(err, se) {
_id: shareEntity._id
}, shareEntity, null, function(err, se) {
if (err) { if (err) {
console.error(err); console.error(err);
callback(err, null); callback(err, null);
...@@ -568,14 +601,11 @@ exports.updateThreadById = function(tid, entity, callback) { ...@@ -568,14 +601,11 @@ exports.updateThreadById = function(tid, entity, callback) {
callback(null, entity); callback(null, entity);
} }
}); });
} }
}); });
} else { } else {
if (shareEntity && (shareEntity.title || shareEntity.description || shareEntity.icon)) { if (shareEntity && (shareEntity.title || shareEntity.description || shareEntity.icon)) {
shareEntity.ent_code = entity.ent_code; shareEntity.ent_code = entity.ent_code;
var forumShare = new ForumShare(shareEntity); var forumShare = new ForumShare(shareEntity);
forumShare.save(function(err, forumShare) { forumShare.save(function(err, forumShare) {
entity.share = forumShare._id; entity.share = forumShare._id;
...@@ -587,34 +617,59 @@ exports.updateThreadById = function(tid, entity, callback) { ...@@ -587,34 +617,59 @@ exports.updateThreadById = function(tid, entity, callback) {
callback(err, null); callback(err, null);
} else { } else {
callback(null, entity); callback(null, entity);
} }
}); });
}); });
} else { } else {
delete entity.share; delete entity.share;
ForumThread.update({ // ForumThread.update({
_id: tid // _id: tid
}, entity, null, function(err, result) { // }, entity, null, function(err, result) {
// if (err) {
// console.error(err);
// callback(err, null);
// } else {
// callback(null, entity);
// }
// });
findOneAndUpdate({_id: tid}, entity, 'updateRedisRecommentThreads', function(err, result) {
if (err) { if (err) {
console.error(err); console.error(err);
callback(err, null); callback(err, null);
} else { } else {
callback(null, entity); ForumShare.update({_id: shareEntity._id}, shareEntity, null, function(err, se) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, entity);
}
});
} }
}); });
} }
} }
}; };
//逻辑删除文章 //逻辑删除文章
exports.logicDeleteThreadById = function(tid, callback) { exports.logicDeleteThreadById = function(tid, callback) {
ForumThread.update({ // ForumThread.update({
_id: mongoose.Types.ObjectId(tid) // _id: mongoose.Types.ObjectId(tid)
}, { // }, {
status : 3 // status : 3
}, null, function(err, result) { // }, null, function(err, result) {
// if (err) {
// console.error(err);
// callback(err, false);
// } else {
// callback(null, true);
// }
// });
findOneAndUpdate({_id: mongoose.Types.ObjectId(tid)}, {status : 3}, 'updateRedisRecommentThreads', function(err, result) {
if (err) { if (err) {
console.error(err); console.error(err);
callback(err, false); callback(err, false);
...@@ -625,11 +680,9 @@ exports.logicDeleteThreadById = function(tid, callback) { ...@@ -625,11 +680,9 @@ exports.logicDeleteThreadById = function(tid, callback) {
}; };
//根据ID更新文章 //根据ID删除文章
exports.deleteThreadById = function(tid, callback) { exports.deleteThreadById = function(tid, callback) {
ForumThread.remove({ ForumThread.remove({_id: tid}, function(err, result) {
_id: tid
}, function(err, result) {
if (err) { if (err) {
console.error(err); console.error(err);
callback(err, null); callback(err, null);
...@@ -923,41 +976,45 @@ exports.getAllThreadByFid = function(conditions, pageNo, pageSize, sort, callbac ...@@ -923,41 +976,45 @@ exports.getAllThreadByFid = function(conditions, pageNo, pageSize, sort, callbac
//根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶) //根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶)
exports.updateTopByThreadId = function(infoId, threadId, callback) { exports.updateTopByThreadId = function(infoId, threadId, callback) {
// ForumThread.update({info:infoId}, {top:0},{multi:true}, function(err, doc) { // ForumThread.findOneAndUpdate({
// if(err){ // _id: threadId
// callback(err,null); // }, {
// }else{ // top: 1,
// ForumThread.findOneAndUpdate({_id:threadId}, {top:1}, function(err, doc) { // topTime: new Date()
// if(err){ // }, function(err, doc) {
// callback(err,null); // if (err) {
// }else{ // callback(err, null);
// callback(null,null); // } else {
// } // callback(null, null);
// }); // }
// }
// }); // });
ForumThread.findOneAndUpdate({
_id: threadId findOneAndUpdate({ _id: threadId}, {top: 1, topTime: new Date()}, 'updateRedisRecommentThreads', function(err, doc){
}, {
top: 1,
topTime: new Date()
}, function(err, doc) {
if (err) { if (err) {
console.error(err);
callback(err, null); callback(err, null);
} else { } else {
callback(null, null); callback(null, doc);
} }
}); });
}; };
//根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶) //根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶)
exports.updateUnTopByThreadId = function(infoId, threadId, callback) { exports.updateUnTopByThreadId = function(infoId, threadId, callback) {
ForumThread.findOneAndUpdate({ // ForumThread.findOneAndUpdate({
_id: threadId // _id: threadId
}, { // }, {
top: 0, // top: 0,
topTime: null // topTime: null
}, function(err, doc) { // }, function(err, doc) {
// if (err) {
// callback(err, null);
// } else {
// callback(null, null);
// }
// });
findOneAndUpdate({ _id: threadId}, {top: 0,topTime: null}, 'updateRedisRecommentThreads', function(err, doc) {
if (err) { if (err) {
callback(err, null); callback(err, null);
} else { } else {
...@@ -965,23 +1022,31 @@ exports.updateUnTopByThreadId = function(infoId, threadId, callback) { ...@@ -965,23 +1022,31 @@ exports.updateUnTopByThreadId = function(infoId, threadId, callback) {
} }
}); });
}; };
//根据文章ID更新文章为推荐 //根据文章ID更新文章为加精
exports.updateRecommendByThreadId = function(threadId, callback) { exports.updateRecommendByThreadId = function(threadId, callback) {
ForumThread.findOneAndUpdate({ // ForumThread.findOneAndUpdate({
_id: threadId // _id: threadId
}, { // }, {
recommend: 1 // recommend: 1
}, function(err, doc) { // }, function(err, doc) {
// callback(err,doc);
// });
findOneAndUpdate({_id: threadId}, {recommend: 1}, 'updateRedisRecommentThreads', function(err, doc) {
callback(err,doc); callback(err,doc);
}); });
}; };
//根据文章ID更新文章为不推荐 //根据文章ID更新文章为不加精
exports.updateUnRecommendByThreadId = function(threadId, callback) { exports.updateUnRecommendByThreadId = function(threadId, callback) {
ForumThread.findOneAndUpdate({ // ForumThread.findOneAndUpdate({
_id: threadId // _id: threadId
}, { // }, {
recommend: 0 // recommend: 0
}, function(err, doc) { // }, function(err, doc) {
// callback(err,null);
// });
findOneAndUpdate({_id: threadId}, {recommend: 0}, 'updateRedisRecommentThreads', function(err, doc) {
callback(err,null); callback(err,null);
}); });
}; };
...@@ -993,12 +1058,20 @@ exports.updateTagTop = function(tid,tag_top,callback){ ...@@ -993,12 +1058,20 @@ exports.updateTagTop = function(tid,tag_top,callback){
}else{ }else{
time = null; time = null;
} }
ForumThread.findOneAndUpdate({ // ForumThread.findOneAndUpdate({
_id: tid // _id: tid
}, { // }, {
tag_top: tag_top, // tag_top: tag_top,
tag_topTime: time // tag_topTime: time
}, function(err, doc) { // }, function(err, doc) {
// if (err) {
// callback(err, null);
// } else {
// callback(null, doc);
// }
// });
findOneAndUpdate({_id: tid}, {tag_top: tag_top, tag_topTime: time}, 'updateRedisRecommentThreads', function(err, doc) {
if (err) { if (err) {
callback(err, null); callback(err, null);
} else { } else {
...@@ -1009,39 +1082,58 @@ exports.updateTagTop = function(tid,tag_top,callback){ ...@@ -1009,39 +1082,58 @@ exports.updateTagTop = function(tid,tag_top,callback){
//更新文章点赞数 //更新文章点赞数
exports.updateThreadRaiseCount = function(threadId, callback) { exports.updateThreadRaiseCount = function(threadId, callback) {
ForumThread.update({ // ForumThread.update({
_id: threadId // _id: threadId
}, { // }, {
$inc: { // $inc: {
praise_count: 1 // praise_count: 1
} // }
}, { // }, {
w: 1, // w: 1,
safe: true // safe: true
}, // },
function(err, result) { // function(err, result) {
if (err) { // if (err) {
console.error(err); // console.error(err);
callback(err, null); // callback(err, null);
} else { // } else {
callback(null, null); // callback(null, null);
} // }
}); // });
update({_id: threadId}, {$inc: {praise_count: 1}}, { w: 1,safe: true}, 'updateRedisRecommentThreads',function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
}; };
//减少文章点赞数 //减少文章点赞数
exports.updateThreadRaiseCountDec = function(threadId, callback) { exports.updateThreadRaiseCountDec = function(threadId, callback) {
ForumThread.update({ // ForumThread.update({
_id: threadId // _id: threadId
}, { // }, {
$inc: { // $inc: {
praise_count: -1 // praise_count: -1
} // }
}, { // }, {
w: 1, // w: 1,
safe: true // safe: true
}, // },
function(err, result) { // function(err, result) {
// if (err) {
// console.error(err);
// callback(err, null);
// } else {
// callback(null, null);
// }
// });
update({_id: threadId}, {$inc: {praise_count: -1}}, { w: 1,safe: true}, 'updateRedisRecommentThreads',function(err, result) {
if (err) { if (err) {
console.error(err); console.error(err);
callback(err, null); callback(err, null);
...@@ -1053,90 +1145,127 @@ exports.updateThreadRaiseCountDec = function(threadId, callback) { ...@@ -1053,90 +1145,127 @@ exports.updateThreadRaiseCountDec = function(threadId, callback) {
//更新文章分享数 //更新文章分享数
exports.updateThreadShareCount = function(threadId, callback) { exports.updateThreadShareCount = function(threadId, callback) {
ForumThread.update({ // ForumThread.update({
_id: threadId // _id: threadId
}, { // }, {
$inc: { // $inc: {
share_count: 1 // share_count: 1
} // }
}, { // }, {
w: 1, // w: 1,
safe: true // safe: true
}, // },
function(err, result) { // function(err, result) {
if (err) { // if (err) {
console.error(err); // console.error(err);
callback(err, null); // callback(err, null);
} else { // } else {
callback(null, null); // callback(null, null);
} // }
}); // });
update({_id: threadId}, {$inc: {share_count: 1}}, { w: 1,safe: true}, null,function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
}; };
//更新文章评论数 //更新文章评论数
exports.updateThreadCommentCountInc = function(threadId, callback) { exports.updateThreadCommentCountInc = function(threadId, callback) {
ForumThread.update({ // ForumThread.update({
_id: threadId // _id: threadId
}, { // }, {
$inc: { // $inc: {
comment_count: 1 // comment_count: 1
} // }
}, { // }, {
w: 1, // w: 1,
safe: true // safe: true
}, // },
function(err, result) { // function(err, result) {
if (err) { // if (err) {
console.error(err); // console.error(err);
callback(err, null); // callback(err, null);
} else { // } else {
callback(null, null); // callback(null, null);
} // }
}); // });
update({_id: threadId}, {$inc: {comment_count: 1}}, { w: 1,safe: true}, 'updateRedisRecommentThreads',function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
}; };
//更新文章评论数 //更新文章评论数
exports.updateThreadCommentCount = function(threadId, callback) { exports.updateThreadCommentCount = function(threadId, callback) {
ForumThread.update({ // ForumThread.update({
_id: threadId // _id: threadId
}, { // }, {
$inc: { // $inc: {
comment_count: -1 // comment_count: -1
} // }
}, { // }, {
w: 1, // w: 1,
safe: true // safe: true
}, // },
function(err, result) { // function(err, result) {
if (err) { // if (err) {
console.error(err); // console.error(err);
callback(err, null); // callback(err, null);
} else { // } else {
callback(null, null); // callback(null, null);
} // }
}); // });
update({_id: threadId}, {$inc: {comment_count: -1}}, { w: 1,safe: true}, 'updateRedisRecommentThreads',function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
}; };
//更新文章浏览数 //更新文章浏览数
exports.updateThreadPvCount = function(threadId, callback) { exports.updateThreadPvCount = function(threadId, callback) {
ForumThread.update({ // ForumThread.update({
_id: threadId // _id: threadId
}, { // }, {
$inc: { // $inc: {
pv_count: 1 // pv_count: 1
} // }
}, { // }, {
w: 1, // w: 1,
safe: true // safe: true
}, // },
function(err, result) { // function(err, result) {
if (err) { // if (err) {
console.error(err); // console.error(err);
callback(err, null); // callback(err, null);
} else { // } else {
callback(null, null); // callback(null, null);
} // }
}); // });
update({_id: threadId}, {$inc: {pv_count: 1}}, { w: 1,safe: true}, 'updateRedisRecommentThreads',function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
}; };
// 查找数组元素下标 // 查找数组元素下标
...@@ -1239,7 +1368,7 @@ exports.getAllThreadByFidAndMid = function(mid, conditions, pageNo, pageSize, so ...@@ -1239,7 +1368,7 @@ exports.getAllThreadByFidAndMid = function(mid, conditions, pageNo, pageSize, so
//更新文章 //更新文章
exports.updateThread = function(condition, entity, callback) { exports.updateThread = function(condition, entity, callback) {
ForumThread.findOneAndUpdate(condition, entity, function(err, doc){ findOneAndUpdate(condition, entity, 'updateRedisRecommentThreads', function(err, doc){
if (err) { if (err) {
console.error(err); console.error(err);
callback(err, null); callback(err, null);
......
'use strict'; 'use strict';
var redis=global.redis; var redis=global.redis;
var expire = 60*10; var expire = 60*30;
//缓存数量 //缓存数量
var ThreadRedisCount = 50; var ThreadRedisCount = 50;
...@@ -12,6 +12,10 @@ var forumThreadService=require('../service/forumThreadService'); ...@@ -12,6 +12,10 @@ var forumThreadService=require('../service/forumThreadService');
var redisPraiseLog=require('./redisPraiseLog'); var redisPraiseLog=require('./redisPraiseLog');
var util=require('./util'); var util=require('./util');
var RefreshFlag = true;
var tyhread_redis_salt = '_tyhread_redis_salt';
var sort = { var sort = {
new_recommend:-1, new_recommend:-1,
new_recommend_order_idx:-1, new_recommend_order_idx:-1,
...@@ -25,13 +29,13 @@ var sort = { ...@@ -25,13 +29,13 @@ var sort = {
created:-1 created:-1
}; };
function getKey(ent_code,keyID,type){ function getKey(ent_code, salt){
var key = ent_code + keyID + type; var key = ent_code + salt;
return key; return key;
} }
function setToRedis(ent_code,keyID,type,value){ function setToRedis(ent_code,salt,value){
var key = getKey(ent_code,keyID,type); var key = getKey(ent_code,salt);
if(!redis){ if(!redis){
console.log('redis error'); console.log('redis error');
return 'error' return 'error'
...@@ -42,8 +46,37 @@ function setToRedis(ent_code,keyID,type,value){ ...@@ -42,8 +46,37 @@ function setToRedis(ent_code,keyID,type,value){
}); });
} }
exports.get=function(ent_code,keyID,type,callback){ function getEsenceThreads(ent_code, salt,callback){
var key = getKey(ent_code,keyID,type); var today = new Date();
var conditions = {
ent_code: ent_code,
level: 1,
status: 1,
new_recommend: 1,
$or: [{
isEvent: 0
}, {
isEvent: null
}, {
isEvent: 1,
'event.eventStartTime' : {$lte : today}
}]
};
forumThreadService.getThreadWithNotPopulateComment(conditions, 1, ThreadRedisCount, sort, function(err, results) {
if (err) {
console.error(err);
callback(err);
} else {
util.loadLevel(ent_code, results.items, function() {
setToRedis(ent_code, salt ,JSON.stringify(results));
callback(null);
});
}
});
};
exports.get=function(ent_code, callback){
var key = getKey(ent_code, tyhread_redis_salt);
if(!redis){ if(!redis){
console.log('redis error'); console.log('redis error');
return callback && callback('error'); return callback && callback('error');
...@@ -51,7 +84,7 @@ exports.get=function(ent_code,keyID,type,callback){ ...@@ -51,7 +84,7 @@ exports.get=function(ent_code,keyID,type,callback){
redis.get(key, function(error, res){ redis.get(key, function(error, res){
if(!res){ if(!res){
//不存在,先获取再返回 //不存在,先获取再返回
getEsenceThreads(ent_code, keyID, type,function(err){ getEsenceThreads(ent_code, tyhread_redis_salt,function(err){
redis.get(key, function(error, res){ redis.get(key, function(error, res){
return callback && callback(error, res.split(",")); return callback && callback(error, res.split(","));
}); });
...@@ -62,8 +95,8 @@ exports.get=function(ent_code,keyID,type,callback){ ...@@ -62,8 +95,8 @@ exports.get=function(ent_code,keyID,type,callback){
}); });
}; };
exports.set=function(ent_code,keyID,type,value,callback){ exports.set=function(ent_code, salt, value, callback){
var key = getKey(ent_code,keyID,type); var key = getKey(ent_code, salt);
if(!redis){ if(!redis){
console.log('redis error'); console.log('redis error');
return callback && callback('error'); return callback && callback('error');
...@@ -74,8 +107,8 @@ exports.set=function(ent_code,keyID,type,value,callback){ ...@@ -74,8 +107,8 @@ exports.set=function(ent_code,keyID,type,value,callback){
}); });
}; };
exports.clear = function(ent_code,keyID,type,callback){ exports.clear = function(ent_code,salt,callback){
var key = getKey(ent_code,keyID,type); var key = getKey(ent_code, salt);
if(!redis){ if(!redis){
console.log('redis error'); console.log('redis error');
return callback && callback('error'); return callback && callback('error');
...@@ -85,53 +118,24 @@ exports.clear = function(ent_code,keyID,type,callback){ ...@@ -85,53 +118,24 @@ exports.clear = function(ent_code,keyID,type,callback){
}); });
}; };
function getEsenceThreads(ent_code,keyID,type,callback){
var today = new Date();
var conditions = {
ent_code: ent_code,
level: 1,
status: 1,
new_recommend: 1,
$or: [{
isEvent: 0
}, {
isEvent: null
}, {
isEvent: 1,
'event.eventStartTime' : {$lte : today}
}]
};
forumThreadService.getThreadWithNotPopulateComment(conditions, 1, ThreadRedisCount, sort, function(err, results) {
if (err) {
console.error(err);
callback(err);
} else {
util.loadLevel(ent_code, results.items, function() {
setToRedis(ent_code,keyID,type,JSON.stringify(results));
callback(null);
});
}
});
};
//获取缓存列表 //获取缓存列表
exports.getRedisEsenceThreads = function(ent_code,keyID,type,callback){ exports.getRedisEsenceThreads = function(ent_code, callback){
var key = getKey(ent_code,keyID,type); var key = getKey(ent_code, tyhread_redis_salt);
getEsenceThreads(ent_code, keyID,type, function(err){ getEsenceThreads(ent_code, tyhread_redis_salt, function(err){
redis.get(key, function(error, res){ redis.get(key, function(error, res){
return callback && callback(error, res.split(",")); return callback && callback(error, res.split(","));
}); });
}); });
}; };
//更新缓存列表
/*exports.updateRedisEsenceThreads = function(ent_code,keyID,type,callback){
getEsenceThreads(ent_code, keyID,type, function(err){
callback();
});
};*/
//获取缓存数值 //获取缓存数值
exports.getThreadRedisCount = function(){ exports.getThreadRedisCount = function(){
return ThreadRedisCount; return ThreadRedisCount;
};
//更新缓存列表
exports.updateRedisEsenceThreads = function(ent_code){
if(RefreshFlag){
getEsenceThreads(ent_code, tyhread_redis_salt, function(err){});
}
}; };
\ No newline at end of file
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