Commit 13ab9b0f authored by 刘文胜's avatar 刘文胜

传播图优化

parent 3a715834
...@@ -27,6 +27,7 @@ var forumThreadService = require('../../service/forumThreadService'), ...@@ -27,6 +27,7 @@ var forumThreadService = require('../../service/forumThreadService'),
redisThreadList = require('../../utils/redisThreadList'); redisThreadList = require('../../utils/redisThreadList');
var userUtil = require('../../utils/user'); var userUtil = require('../../utils/user');
var MemoryCache = require('../../utils/simpleCache');
var floorGenerator = require('../../utils/floorGenerator'); var floorGenerator = require('../../utils/floorGenerator');
module.exports = function(app) { module.exports = function(app) {
app.use('/admin/forum', router); app.use('/admin/forum', router);
...@@ -1273,94 +1274,80 @@ router.get('/thread/:tid/spreadchain', function(req, res, next) { ...@@ -1273,94 +1274,80 @@ router.get('/thread/:tid/spreadchain', function(req, res, next) {
router.get('/thread/:tid/spreadchainByRedis', function(req, res, next) { router.get('/thread/:tid/spreadchainByRedis', function(req, res, next) {
var tid = req.params.tid || null, var tid = req.params.tid || null,
ent_code = req.session.user.ent_code; ent_code = req.session.user.ent_code;
var openIds = []; var key = "sc_forum_thread_" + tid;
then.parallel([ redis.get(key,function(err,store){
function(cont) { if(store){
ForumThread.findOne({ return res.json(_.assign({
_id: tid, data: key
ent_code: ent_code }, returnCode.SUCCESS));
}).populate('from').exec(function(err, doc) {
cont(err, doc);
});
},
function(cont) {
ForumPVLog.find({
thread: tid,
ent_code: ent_code
}).sort('source created').populate('user source').exec(function(err, docs) {
cont(err, docs);
});
} }
]).then(function(cont, results) { then.parallel([
var thread = results[0], function(cont) {
pvs = results[1]; ForumThread.findOne({
_id: tid,
var data = { ent_code: ent_code
openId: thread.from.uid, }).select('_id from').populate({
name: thread.from.nickName, path:'from',
//nickName: thread.from.displayName, select:'nickName uid'
nickName: thread.from.nickName, }).exec(function(err, doc) {
children: [] , cont(err,doc);
id : 'ROOT' });
} },
//获取一级传播 function(cont) {
for (var i = 0; i < pvs.length; i += 1) { var conditions = {
var pv = pvs[i]; ent_code: ent_code,
if (!pv.source) { thread: tid
var flag = false; };
for (var j = 0; j < data.children.length; j += 1) { var s1 = new Date().getTime();
var c = data.children[j]; ForumPVLog.count(conditions).exec(function(err, count) {
if (c.openId === pv.open_id) { var pageSize = 5000;
flag = true; var page_total = Math.ceil((count || 0) / pageSize) + 1;
break; var tasks = [];
} _.each(_.range(page_total),function(pageNo){
} tasks.push(function(cb){
if (!flag && pv.open_id !== data.openId && !checkOpenIds(openIds, pv.open_id)) { ForumPVLog.find(conditions).select('open_id user source').sort('source created')
data.children.push({ .skip((pageNo)*pageSize).limit(pageSize)
openId: pv.open_id, .populate({
nickName: pv.user.displayName || '未知', path: 'user source',
name: pv.user.nickName || '未知', select: 'nickName displayName'
id : pv._id }).exec(function(err, docs) {
cb(err,docs);
});
});
}); });
openIds.push(pv.open_id); async.parallel(tasks,function(err,results){
} console.log('加载数据耗时',(new Date().getTime()-s1)/1000);
} cont(err,_.flatten(results));
} });
//获取非一级传播 });
var new_pvs = [];
for (var i = 0; i < pvs.length; i += 1) {
if (pvs[i].source && pvs[i].source.uid !== data.openId && pvs[i].open_id !== data.openId) {
new_pvs.push(pvs[i]);
} }
} ]).then(function(cont, results) {
pvs = new_pvs; var thread = results[0],
//递归其他传播 pvs = results[1];
var thenTask = []; var datas = [];
_.forEach(data.children, function(c) { datas.push({
thenTask.push(function(cb) { openId: thread.from.uid,
getSubSpreadPaths(openIds, c, pvs, cb); nickName: thread.from.nickName || '未知',
name: thread.from.nickName || '未知',
parent:null
}); });
}) _.forEach(pvs,function(pv){
then.parallel(thenTask).then(function(cont, childrens) { datas.push({
data.children = childrens; openId: pv.open_id,
nickName: (pv.user && pv.user.displayName) || '未知',
//put data into redis name: (pv.user && pv.user.nickName) || '未知',
//var key = tid + "_spreadchain" parent:(pv.source && pv.source.uid) || thread.from.uid
var key = "sc_forum_thread_" + tid; });
redis.set(key,JSON.stringify(data)); });
redis.expire(key,60); redis.set(key,JSON.stringify(datas));
redis.expire(key,60 * 30);
var rs = { res.json(_.assign({
data: key data: key
} }, returnCode.SUCCESS));
}).fail(function(cont, err) {
openIds = []; console.error(err);
res.json(_.assign(rs, returnCode.SUCCESS)); res.json(returnCode.BUSY);
}); });
}).fail(function(cont, err) {
console.error(err);
res.json(returnCode.BUSY);
}); });
}); });
......
'use strict';
var caches = {//缓存集合
};
var CacheNode = function(content,expire){//定义缓存对象
this.content = content;
this.expire = expire || (3600 * 1000);
this.created = new Date().getTime();
};
CacheNode.prototype.expireIf = function(){//定义缓存对象的静态方法,判断缓存是否过期
return new Date().getTime() - this.created > this.expire;
};
function getKey(key){
return 'forum_simple_cache_'+key;
}
function get(key){
var cache = caches[getKey(key)];
if(cache && !cache.expireIf()){
return cache.content;
}
return null;
}
function getIfAbsentDO(key,operate,expire,callback){//不过不存在就调用opreate的回调的参数为err,result 并把结果放入cache,然后调用callback 返回结果
var content = get(key);
if(content){
return callback && callback(null,content);
}
if(operate){
operate(function(err,result){
if(!err){//放入缓存
put(key,result,expire);
}
callback(err,result);
});
}else{
return callback && callback(null,null);
}
}
function put(key,val,expire){
var cache = new CacheNode(val,expire);
caches[getKey(key)] = cache;
return true;
}
function putIfAbsent(key,val,expire){
var cache = get(key);
if(!cache || cache.expireIf()){//不存在或者过期
return put(key,val,expire);
}
return false;
}
function clear(){
caches = {};
}
function remove(key){
delete caches[getKey(key)];
}
exports.put = put;
exports.putIfAbsent = putIfAbsent;
exports.get = get;
exports.getIfAbsent = getIfAbsentDO;
exports.remove = remove;
exports.clear = clear;
/*getIfAbsentDO('test',function(callback){
callback(false,{11111111111:1111111111111});
},333,function(err,result){
setTimeout(function(){
console.log(caches);
console.log(get('test'));;
},11);
});*/
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