Authored by 周少峰

Merge branch 'hotfix/add-cache' into gray

... ... @@ -7,6 +7,43 @@
const api = global.yoho.API;
const config = global.yoho.config;
const redis = global.yoho.redis;
const cache = global.yoho.cache;
const logger = global.yoho.logger;
const uuid = require('uuid');
function _cacheGet(key) {
return cache.get(key).then((data) => {
if (data) {
return JSON.parse(data);
}
return Promise.reject();
});
}
// 半个月
const HALF_MONTH = 60 * 60 * 24 * 15;
function _cacheSave(key, value) {
return cache.set(key, value, HALF_MONTH)
.catch(err => logger.error(`save cache data fail:${err.toString()}`));
}
function _cached(fn, ctx) {
const pre = 'recommend-cache:' + (fn.name || 'random:' + uuid.v4()) + ':';
return function() {
const args = Array.prototype.slice.call(arguments);
const key = pre + JSON.stringify(args || '[]');
return _cacheGet(key).catch(() => {
return fn.apply(ctx, args).then(result => {
_cacheSave(key, result);
return result;
});
});
};
}
/**
* 商品的 banner
... ... @@ -159,11 +196,11 @@ const getLikeAsync = (skn, limit) => {
};
// 根据small_sort从redis获取分类下的关键词
const getRecommendKeywords = (smallSort) => {
function _getRecommendKeywords(smallSort) {
return redis.all([['get', `global:yoho:seo:keywords:sortId:${smallSort}:page:1`]]).then(res => {
return res[0];
});
};
}
module.exports = {
getProductBannerAsync,
... ... @@ -177,5 +214,5 @@ module.exports = {
getShopRecommendAsync,
getBundleAsync,
getLikeAsync,
getRecommendKeywords
getRecommendKeywords: _cached(_getRecommendKeywords)
};
... ...