Authored by htoooth

fix

const cache = global.yoho.cache;
const logger = global.yoho.logger;
const ONE_MINUTE = 60;
const FIVE_MINUTE = 5 * ONE_MINUTE;
function _cacheGet(key) {
return cache.get(key).then((data) => {
if (data) {
return JSON.parse(data);
}
return Promise.reject();
});
}
function _cacheSave(key, value, ttl) {
return cache.set(key, value, ttl)
.catch(err => logger.error(`save cache data fail:${err.toString()}`));
}
function defaultKey() {
const args = Array.prototype.slice.call(arguments);
return JSON.stringify(args || '[]');
}
function _cached(fn, keyFn = defaultKey, ctx = null, ttl = FIVE_MINUTE) {
const keyGen = function() {
const args = Array.prototype.slice.call(arguments);
return '_cache:' + (fn.name || '') + ':' + keyFn.apply(null, args);
};
return function() {
const args = Array.prototype.slice.call(arguments);
const key = keyGen.apply(null, args);
return _cacheGet(key).catch(() => {
return fn.apply(ctx, args).then(result => {
_cacheSave(key, result, ttl);
return result;
});
});
};
}
module.exports = _cached;
... ...
... ... @@ -17,6 +17,7 @@ const logger = global.yoho.logger;
const detailHelper = require('./detail-helper');
const _cached = require('./cache-utils');
const ProductModel = require('./detail-product-api');
const ConsultServiceModel = require('./detail-consult-service');
const CommentServiceModel = require('./detail-comment-service');
... ... @@ -1737,7 +1738,9 @@ module.exports = class extends global.yoho.BaseModel {
this.usefulAsync = this.consultService.usefulAsync.bind(this.consultService);
// 获取某一个商品详情主页面
this.showMainAsync = showMainAsync.bind(this);
this.showMainAsync = _cached(showMainAsync, function keyGen(req, {skn, channel, gender}) {
return skn + (channel || '') + (gender || '');
}, this);
// 获取某一个商品详情主页面
this.showMainBackAsync = showMainBackAsync.bind(this);
... ...