footerData.js
824 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict';
const _ = require('lodash');
const redis = global.yoho.redis;
let footerLinks = [];
let firstTime = 0;
// 底部数据
module.exports = () => {
return (req, res, next) => {
if (req.xhr || req.path !== '/') {
return next();
}
// 5min Invalid
if (footerLinks.length && (Date.now() - firstTime) > 300000) {
Object.assign(res.locals, {footerLinks});
return next();
}
return redis.all([
['get', 'friend:footer:links']
]).then(result => {
footerLinks = _.sortBy(JSON.parse(result[0] || '[]'), o => {
return -o.sort;
});
Object.assign(res.locals, {footerLinks});
firstTime = Date.now();
return next();
});
};
};