const md5 = require('md5');
const redis = global.yoho.redis;
const _ = require('lodash');

module.exports = (type, query, req) => {
    query = type === 'url' ? md5(query) : query;

    if (redis && _.get(req.app.locals.wap, 'ci.tdk', false)) {
        let arr = [];

        arr.push(redis.multi([
            ['exists', `tdk:${type}:${query}`],
            ['hmget', `tdk:${type}:${query}`, 'key', 'title', 'keywords', 'description']
        ]).execAsync());

        arr.push(new Promise((resolve)=>{
            setTimeout(resolve, 500, []);
        }));

        return Promise.race(arr).then(function(res) {
            if (res.length) {
                return res[1];
            } else {
                return [];
            }
        });
    } else {
        return Promise.resolve([]);
    }

};