Authored by htoooth

add cached

... ... @@ -26,15 +26,16 @@ function _cacheGet(key) {
});
}
// 半个月
const HALF_MONTH = 60 * 60 * 24 * 15;
const ONE_HOUR = 60 * 60; // 一小时
const ONE_DAY = ONE_HOUR * 24; // 一天
const HALF_MONTH = ONE_DAY * 15; // 半个月
function _cacheSave(key, value) {
return cache.set(key, value, HALF_MONTH)
function _cacheSave(key, value, ttl) {
return cache.set(key, value, ttl)
.catch(err => logger.error(`save cache data fail:${err.toString()}`));
}
function _cached(fn, ctx) {
function _cached(fn, ctx, ttl = HALF_MONTH) {
const pre = 'recommend-cache:' + (fn.name || 'random:' + uuid.v4()) + ':';
return function() {
... ... @@ -43,7 +44,7 @@ function _cached(fn, ctx) {
return _cacheGet(key).catch(() => {
return fn.apply(ctx, args).then(result => {
_cacheSave(key, result);
_cacheSave(key, result, ttl);
return result;
});
});
... ... @@ -61,6 +62,7 @@ module.exports = class extends global.yoho.BaseModel {
this.getNewProduct = _cached(this._getNewProduct, this);
this.getGuangArticles = _cached(this._getGuangArticles, this);
this.getRecommendKeywords = _cached(this._getRecommendKeywords, this);
this.getShopRecommendAsync = _cached(this._getShopRecommendAsync, this, ONE_DAY);
}
/**
... ... @@ -212,13 +214,13 @@ module.exports = class extends global.yoho.BaseModel {
/**
* 店铺推荐
*/
getShopRecommendAsync(skn, page, limit) {
_getShopRecommendAsync(skn) {
return this.get({
data: {
method: 'web.product.shopRecommend',
product_skn: skn,
page: page || 1,
limit: limit || 20
page: 1,
limit: 20
}
});
}
... ...