Authored by 周少峰

Merge branch 'feature/qps' of git.yoho.cn:fe/yohobuy-node into feature/qps

... ... @@ -23,7 +23,7 @@ exports.index = (req, res, next) => {
let resData = {};
let qs = decodeURIComponent(req._parsedOriginalUrl.query || '');
req.ctx(list).getListData(Object.assign(req.query, {uid: req.user.uid, prid: req.user.prid}),
req.ctx(list).getListDataPre(Object.assign(req.query, {uid: req.user.uid, prid: req.user.prid}),
req.yoho.channel).then(result => {
Object.assign(resData, result);
... ... @@ -58,7 +58,7 @@ exports.index = (req, res, next) => {
exports.new = (req, res, next) => {
let resData = {};
req.ctx(list).getListNewData(Object.assign({order: 's_t_desc'}, req.query), req.yoho.channel).then(result => {
req.ctx(list).getListNewDataPre(Object.assign({order: 's_t_desc'}, req.query), req.yoho.channel).then(result => {
Object.assign(resData, result, {
hideInfo: {from: 'newProduct'}
});
... ...
... ... @@ -32,7 +32,7 @@ const index = (req, res, next) => {
return;
}
return req.ctx(search).getSearchData(Object.assign(params, {uid: req.user.uid, prid: req.user.prid}),
return req.ctx(search).getSearchDataPre(Object.assign(params, {uid: req.user.uid, prid: req.user.prid}),
req.yoho.channel).then(result => {
let queryKey = req.query.query;
... ...
... ... @@ -14,6 +14,11 @@ const shopHandler = require('./shop-handler');
const helpers = global.yoho.helpers;
const crypto = global.yoho.crypto;
const _ = require('lodash');
const Fn = require('lodash/fp');
const md5 = require('md5');
const cache = global.yoho.cache;
const config = global.yoho.config;
const logger = global.yoho.logger;
// const limitNum = 60; // 商品每页显示数目
const needParams = ['query', 'msort', 'misort', 'category_id', 'gender', 'shelveTime'];
... ... @@ -29,6 +34,15 @@ const positionId = 10;
// 获取分类左侧广告id
const sortAdsId = 79;
const CACHE_TIME_S = 60;
function _getCacheKey(params, page) {
let sortByKey = Fn.pipe(Fn.toPairs, Fn.sortBy(0), Fn.flatten);
let genKey = Fn.pipe(Fn.cloneDeep, sortByKey, Fn.join('_'));
return `render_cache_${page}_${md5(genKey(params))}`;
}
/**
* 获取商品分类列表数据
*/
... ... @@ -137,6 +151,49 @@ function getListData(params, channel) {
});
}
function getListDataPre(params, channel) {
let cKey = _getCacheKey(Object.assign({channel: channel}, params), 'list');
let getOriginData = () => {
return this.getListData(params, channel).then(result => {
// 查询结果为空则不cache
if (config.useCache && !_.isEmpty(_.get(result, 'list.goods', []))) {
cache.set(cKey, result, CACHE_TIME_S)
.catch(err => logger.debug(`list render cache data save fail:${err.toString()}`));
}
return result;
});
};
if (config.useCache) {
return cache.get(cKey).catch(err => {
logger.debug(`get list render cache data fail:${err.toString()}`);
return getOriginData();
}).then(cdata => {
let hasCache = false;
if (cdata) {
try {
hasCache = true;
cdata = JSON.parse(cdata);
} catch (e) {
logger.debug('list render cache data parse fail.');
}
}
if (hasCache) {
return cdata;
} else {
return getOriginData();
}
});
}
return getOriginData();
}
/**
* 获取新品到着数据
*/
... ... @@ -215,6 +272,49 @@ function getListNewData(params, channel) {
});
}
function getListNewDataPre(params, channel) {
let cKey = _getCacheKey(Object.assign({channel: channel}, params), 'listnew');
let getOriginData = () => {
return this.getListNewData(params, channel).then(result => {
// 查询结果为空则不cache
if (config.useCache && !_.isEmpty(_.get(result, 'list.goods', []))) {
cache.set(cKey, result, CACHE_TIME_S)
.catch(err => logger.debug(`list_new render cache data save fail:${err.toString()}`));
}
return result;
});
};
if (config.useCache) {
return cache.get(cKey).catch(err => {
logger.debug(`get list_new render cache data fail:${err.toString()}`);
return getOriginData();
}).then(cdata => {
let hasCache = false;
if (cdata) {
try {
hasCache = true;
cdata = JSON.parse(cdata);
} catch (e) {
logger.debug('list_new render cache data parse fail.');
}
}
if (hasCache) {
return cdata;
} else {
return getOriginData();
}
});
}
return getOriginData();
}
/**
* 根据品牌域名查询品牌信息
*/
... ... @@ -588,7 +688,9 @@ module.exports = class extends global.yoho.BaseModel {
this.shopApi = new ShopApiModel(ctx);
this.getListData = getListData.bind(this);
this.getListDataPre = getListDataPre.bind(this);
this.getListNewData = getListNewData.bind(this);
this.getListNewDataPre = getListNewDataPre.bind(this);
this.getBrandInfo = getBrandInfo.bind(this);
this.getBrandData = getBrandData.bind(this);
this.getBrandAbout = getBrandAbout.bind(this);
... ...
... ... @@ -19,8 +19,23 @@ const headerModel = require('../../../doraemon/models/header');
const productProcess = require(`${utils}/product-process`);
const searchHandler = require('./search-handler');
const _ = require('lodash');
const Fn = require('lodash/fp');
const md5 = require('md5');
const cache = global.yoho.cache;
const config = global.yoho.config;
const logger = global.yoho.logger;
const needParams = ['query', 'msort', 'misort'];
const CACHE_TIME_S = 60;
function _getCacheKey(params, page) {
let sortByKey = Fn.pipe(Fn.toPairs, Fn.sortBy(0), Fn.flatten);
let genKey = Fn.pipe(Fn.cloneDeep, sortByKey, Fn.join('_'));
return `render_cache_${page}_${md5(genKey(params))}`;
}
function getKeyActivity(query) {
return this.searchApi.getKeyActivityAsync(query).then(result => {
return _.get(result, 'data.urlobj.pcUrl', '');
... ... @@ -159,6 +174,49 @@ function getSearchData(params, channel) {
});
}
function getSearchDataPre(params, channel) {
let cKey = _getCacheKey(Object.assign({channel: channel}, params), 'search');
let getOriginData = () => {
return this.getSearchData(params, channel).then(result => {
// 查询结果为空则不cache
if (config.useCache && !_.isEmpty(_.get(result, 'search.goods', []))) {
cache.set(cKey, result, CACHE_TIME_S)
.catch(err => logger.debug(`search render cache data save fail:${err.toString()}`));
}
return result;
});
};
if (config.useCache) {
return cache.get(cKey).catch(err => {
logger.debug(`get search render cache data fail:${err.toString()}`);
return getOriginData();
}).then(cdata => {
let hasCache = false;
if (cdata) {
try {
hasCache = true;
cdata = JSON.parse(cdata);
} catch (e) {
logger.debug('search render cache data parse fail.');
}
}
if (hasCache) {
return cdata;
} else {
return getOriginData();
}
});
}
return getOriginData();
}
/**
* 搜索提示
*/
... ... @@ -472,6 +530,7 @@ module.exports = class extends global.yoho.BaseModel {
this.getKeyActivity = getKeyActivity.bind(this);
this.getSearchData = getSearchData.bind(this);
this.getSearchDataPre = getSearchDataPre.bind(this);
this.getSuggest = getSuggest.bind(this);
this.getListBrandsFilter = getListBrandsFilter.bind(this);
this.getBrands4Filter = getBrands4Filter.bind(this);
... ...