...
|
...
|
@@ -3,6 +3,7 @@ |
|
|
*/
|
|
|
'use strict';
|
|
|
const _ = require('lodash');
|
|
|
const Fn = require('lodash/fp');
|
|
|
const md5 = require('md5');
|
|
|
|
|
|
const api = global.yoho.API;
|
...
|
...
|
@@ -26,16 +27,15 @@ const isFavoriteBrandUrl = '/shops/service/v1/favorite/getUidBrandFav'; |
|
|
// 根据品牌查询相关文章
|
|
|
const relateArticleUrl = 'guang/service/v2/article/getArticleByBrand';
|
|
|
|
|
|
const getSearchCackeKey = params => {
|
|
|
let ks = [];
|
|
|
// 缓存生效时间
|
|
|
const CACHE_TIME_S = 60;
|
|
|
|
|
|
_.forEach(params, (val, key) => {
|
|
|
if (params.hasOwnProperty(key) && !_.includes(['page', 'limit', 'need_filter', 'order'], key)) {
|
|
|
ks.push(val);
|
|
|
}
|
|
|
});
|
|
|
const getSearchCacheKey = params => {
|
|
|
let removeUnusedKey = Fn.omit(['page', 'limit', 'need_filter', 'order']);
|
|
|
let sortByKey = Fn.pipe(Fn.toPairs, Fn.sortBy(0), Fn.flatten);
|
|
|
let genKey = Fn.pipe(Fn.cloneDeep, removeUnusedKey, sortByKey, Fn.join('_'));
|
|
|
|
|
|
return 'search_custom_' + md5(ks.join('_'));
|
|
|
return 'search_custom_' + md5(genKey(params));
|
|
|
};
|
|
|
|
|
|
const saveCache = (key, kv, cacheTime) => {
|
...
|
...
|
@@ -79,7 +79,7 @@ const getProductList = (params, from) => { |
|
|
if (!config.useCache) {
|
|
|
return getProductListOrig(finalParams);
|
|
|
} else {
|
|
|
let cKey = getSearchCackeKey(finalParams);
|
|
|
let cKey = getSearchCacheKey(finalParams);
|
|
|
|
|
|
return cache.get(cKey)
|
|
|
.catch(err => logger.debug(`product query save cache data fail:${err.toString()}`))
|
...
|
...
|
@@ -108,7 +108,7 @@ const getProductList = (params, from) => { |
|
|
saveCache(cKey, Object.assign({}, {
|
|
|
filter: result.data.filter,
|
|
|
standard: result.data.standard
|
|
|
}), 3600);
|
|
|
}), CACHE_TIME_S);
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
@@ -137,7 +137,7 @@ const getSortList = (params) => { |
|
|
if (!config.useCache) {
|
|
|
return getSortListOrig(finalParams);
|
|
|
} else {
|
|
|
let cKey = getSearchCackeKey(finalParams);
|
|
|
let cKey = getSearchCacheKey(finalParams);
|
|
|
|
|
|
return cache.get(cKey)
|
|
|
.catch(err => logger.debug(`product query save cache data fail:${err.toString()}`))
|
...
|
...
|
@@ -157,7 +157,7 @@ const getSortList = (params) => { |
|
|
} else {
|
|
|
return getSortListOrig(finalParams).then(ret => {
|
|
|
if (ret && ret.code === 200) {
|
|
|
saveCache(cKey, ret, 3600);
|
|
|
saveCache(cKey, ret, CACHE_TIME_S);
|
|
|
}
|
|
|
return ret;
|
|
|
});
|
...
|
...
|
@@ -262,7 +262,7 @@ const getBrandShop = (query) => { |
|
|
|
|
|
Object.assign(finalParams, {keyword: query});
|
|
|
|
|
|
let cKey = getSearchCackeKey(finalParams);
|
|
|
let cKey = getSearchCacheKey(finalParams);
|
|
|
|
|
|
return cache.get(cKey)
|
|
|
.catch(err => logger.debug(`product query save cache data fail:${err.toString()}`))
|
...
|
...
|
@@ -283,7 +283,7 @@ const getBrandShop = (query) => { |
|
|
return api.get('', finalParams).then(ret => {
|
|
|
if (ret && ret.code === 200) {
|
|
|
|
|
|
saveCache(cKey, ret.data, 3600);
|
|
|
saveCache(cKey, ret.data, CACHE_TIME_S);
|
|
|
return ret.data;
|
|
|
}
|
|
|
return null;
|
...
|
...
|
@@ -302,7 +302,7 @@ const getShopsByBrandId = bid => { |
|
|
brand_id: bid
|
|
|
};
|
|
|
|
|
|
let cKey = getSearchCackeKey(finalParams);
|
|
|
let cKey = getSearchCacheKey(finalParams);
|
|
|
|
|
|
return cache.get(cKey)
|
|
|
.catch(err => logger.debug(`product query save cache data fail:${err.toString()}`))
|
...
|
...
|
@@ -323,7 +323,7 @@ const getShopsByBrandId = bid => { |
|
|
return api.get('', finalParams).then(ret => {
|
|
|
if (ret && ret.code === 200) {
|
|
|
|
|
|
saveCache(cKey, ret.data, 3600);
|
|
|
saveCache(cKey, ret.data, CACHE_TIME_S);
|
|
|
return ret.data;
|
|
|
}
|
|
|
return null;
|
...
|
...
|
@@ -555,5 +555,5 @@ module.exports = { |
|
|
getShopList,
|
|
|
getBrands4Filter,
|
|
|
getProductListOrig,
|
|
|
getSearchCackeKey
|
|
|
getSearchCacheKey
|
|
|
}; |
...
|
...
|
|