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

传播图优化

parent 3a715834
......@@ -27,6 +27,7 @@ var forumThreadService = require('../../service/forumThreadService'),
redisThreadList = require('../../utils/redisThreadList');
var userUtil = require('../../utils/user');
var MemoryCache = require('../../utils/simpleCache');
var floorGenerator = require('../../utils/floorGenerator');
module.exports = function(app) {
app.use('/admin/forum', router);
......@@ -1273,94 +1274,80 @@ router.get('/thread/:tid/spreadchain', function(req, res, next) {
router.get('/thread/:tid/spreadchainByRedis', function(req, res, next) {
var tid = req.params.tid || null,
ent_code = req.session.user.ent_code;
var openIds = [];
then.parallel([
function(cont) {
ForumThread.findOne({
_id: tid,
ent_code: ent_code
}).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);
});
var key = "sc_forum_thread_" + tid;
redis.get(key,function(err,store){
if(store){
return res.json(_.assign({
data: key
}, returnCode.SUCCESS));
}
]).then(function(cont, results) {
var thread = results[0],
pvs = results[1];
var data = {
openId: thread.from.uid,
name: thread.from.nickName,
//nickName: thread.from.displayName,
nickName: thread.from.nickName,
children: [] ,
id : 'ROOT'
}
//获取一级传播
for (var i = 0; i < pvs.length; i += 1) {
var pv = pvs[i];
if (!pv.source) {
var flag = false;
for (var j = 0; j < data.children.length; j += 1) {
var c = data.children[j];
if (c.openId === pv.open_id) {
flag = true;
break;
}
}
if (!flag && pv.open_id !== data.openId && !checkOpenIds(openIds, pv.open_id)) {
data.children.push({
openId: pv.open_id,
nickName: pv.user.displayName || '未知',
name: pv.user.nickName || '未知',
id : pv._id
then.parallel([
function(cont) {
ForumThread.findOne({
_id: tid,
ent_code: ent_code
}).select('_id from').populate({
path:'from',
select:'nickName uid'
}).exec(function(err, doc) {
cont(err,doc);
});
},
function(cont) {
var conditions = {
ent_code: ent_code,
thread: tid
};
var s1 = new Date().getTime();
ForumPVLog.count(conditions).exec(function(err, count) {
var pageSize = 5000;
var page_total = Math.ceil((count || 0) / pageSize) + 1;
var tasks = [];
_.each(_.range(page_total),function(pageNo){
tasks.push(function(cb){
ForumPVLog.find(conditions).select('open_id user source').sort('source created')
.skip((pageNo)*pageSize).limit(pageSize)
.populate({
path: 'user source',
select: 'nickName displayName'
}).exec(function(err, docs) {
cb(err,docs);
});
});
});
openIds.push(pv.open_id);
}
}
}
//获取非一级传播
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]);
async.parallel(tasks,function(err,results){
console.log('加载数据耗时',(new Date().getTime()-s1)/1000);
cont(err,_.flatten(results));
});
});
}
}
pvs = new_pvs;
//递归其他传播
var thenTask = [];
_.forEach(data.children, function(c) {
thenTask.push(function(cb) {
getSubSpreadPaths(openIds, c, pvs, cb);
]).then(function(cont, results) {
var thread = results[0],
pvs = results[1];
var datas = [];
datas.push({
openId: thread.from.uid,
nickName: thread.from.nickName || '未知',
name: thread.from.nickName || '未知',
parent:null
});
})
then.parallel(thenTask).then(function(cont, childrens) {
data.children = childrens;
//put data into redis
//var key = tid + "_spreadchain"
var key = "sc_forum_thread_" + tid;
redis.set(key,JSON.stringify(data));
redis.expire(key,60);
var rs = {
_.forEach(pvs,function(pv){
datas.push({
openId: pv.open_id,
nickName: (pv.user && pv.user.displayName) || '未知',
name: (pv.user && pv.user.nickName) || '未知',
parent:(pv.source && pv.source.uid) || thread.from.uid
});
});
redis.set(key,JSON.stringify(datas));
redis.expire(key,60 * 30);
res.json(_.assign({
data: key
}
openIds = [];
res.json(_.assign(rs, returnCode.SUCCESS));
}, returnCode.SUCCESS));
}).fail(function(cont, err) {
console.error(err);
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