footerData.js 824 Bytes
'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();
        });
    };
};