...
|
...
|
@@ -11,11 +11,12 @@ const TABLE_ACT_PRIZE_PRODUCT = 'act_prize_product'; |
|
|
const TABLE_ACT_PRIZE_PRODUCT_CONTENT = 'act_prize_product_content';
|
|
|
const TABLE_ACT_PRIZE_PRODUCT_USER = 'act_prize_product_user';
|
|
|
|
|
|
const CACHE_TIMES = 60 * 5; // 缓存时间
|
|
|
const MINUTE_TIMES = 60;
|
|
|
const PRODUCT_CACHE_TIMES = MINUTE_TIMES * 5; // 商品(列表&详情)缓存时间
|
|
|
const RECENT_CODE_CACHE_TIME = MINUTE_TIMES; // 最近获取记录缓存时间
|
|
|
const MAX_JOIN_TIMES = 2; // 最大活动参与次数
|
|
|
|
|
|
const userTimesCache = new MemoryCache();
|
|
|
const productCache = new MemoryCache(300);
|
|
|
|
|
|
function handelResult(result) {
|
|
|
return {
|
...
|
...
|
@@ -57,16 +58,6 @@ module.exports = class extends global.yoho.BaseModel { |
|
|
status = parseInt(status, 10);
|
|
|
page = parseInt(page, 10) || 1;
|
|
|
|
|
|
const listCacheKey = `list_${actId}_${status}_${page}_${status}`;
|
|
|
|
|
|
if (!extra.noCache) {
|
|
|
let cacheList = productCache.get(listCacheKey);
|
|
|
|
|
|
if (!_.isUndefined(cacheList)) {
|
|
|
return handelResult(cacheList);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
status = _.isNaN(status) ? '> 0' : `= ${status}`;
|
|
|
|
|
|
let limit = `${(page - 1) * pageSize},${page * pageSize}`;
|
...
|
...
|
@@ -74,11 +65,9 @@ module.exports = class extends global.yoho.BaseModel { |
|
|
return mysqlCli.query(`select * from ${TABLE_ACT_PRIZE_PRODUCT}
|
|
|
where act_id = :actId and status ${status} limit ${limit}`, {
|
|
|
actId
|
|
|
}).then(result => {
|
|
|
productCache.set(listCacheKey, result, CACHE_TIMES);
|
|
|
|
|
|
return handelResult(result);
|
|
|
});
|
|
|
}, {
|
|
|
cache: extra.noCache ? 0 : PRODUCT_CACHE_TIMES
|
|
|
}).then(handelResult);
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -114,21 +103,15 @@ module.exports = class extends global.yoho.BaseModel { |
|
|
return Promise.resolve(handelResult(resData));
|
|
|
}
|
|
|
|
|
|
const contentCacheKey = `content_${actPrizeId}`;
|
|
|
|
|
|
if (!extra.noCache) {
|
|
|
let cacheContent = productCache.get(contentCacheKey);
|
|
|
|
|
|
if (!_.isUndefined(cacheContent)) {
|
|
|
return handelResult(cacheContent);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return Promise.all([
|
|
|
mysqlCli.query(`select * from ${TABLE_ACT_PRIZE_PRODUCT}
|
|
|
where id = :actPrizeId limit 1;`, {actPrizeId}),
|
|
|
where id = :actPrizeId limit 1;`, {actPrizeId}, {
|
|
|
cache: extra.noCache ? 0 : PRODUCT_CACHE_TIMES
|
|
|
}),
|
|
|
mysqlCli.query(`select * from ${TABLE_ACT_PRIZE_PRODUCT_CONTENT}
|
|
|
where act_prize_id = :actPrizeId;`, {actPrizeId})
|
|
|
where act_prize_id = :actPrizeId;`, {actPrizeId}, {
|
|
|
cache: extra.noCache ? 0 : PRODUCT_CACHE_TIMES
|
|
|
})
|
|
|
]).then(result => {
|
|
|
let [product, content] = result;
|
|
|
|
...
|
...
|
@@ -139,20 +122,27 @@ module.exports = class extends global.yoho.BaseModel { |
|
|
});
|
|
|
}
|
|
|
|
|
|
productCache.set(contentCacheKey, resData, CACHE_TIMES);
|
|
|
|
|
|
return resData;
|
|
|
}).then(handelResult);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 0元购抽奖码最近获取记录
|
|
|
* @param actId
|
|
|
* @returns {*}
|
|
|
*/
|
|
|
getCodeRecent() {
|
|
|
getCodeRecent(actId) {
|
|
|
actId = parseInt(actId, 10) || 0;
|
|
|
|
|
|
return mysqlCli.query(`select user_name, user_thumb, create_time
|
|
|
from ${TABLE_ACT_PRIZE_PRODUCT_USER}
|
|
|
order by u.create_time desc limit 10;`).then(handelResult);
|
|
|
from ${TABLE_ACT_PRIZE_PRODUCT_USER} where act_id = :actId
|
|
|
order by u.create_time desc limit 10;`, {
|
|
|
actId
|
|
|
}, {
|
|
|
cache: RECENT_CODE_CACHE_TIME
|
|
|
}).then(result => {
|
|
|
return handelResult(result);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -184,7 +174,7 @@ module.exports = class extends global.yoho.BaseModel { |
|
|
|
|
|
let info = await Promise.all([
|
|
|
mysqlCli.query(`select * from ${TABLE_ACT_PRIZE_PRODUCT}
|
|
|
where id = :actPrizeId limit 1;`, {actPrizeId}),
|
|
|
where id = :actPrizeId limit 1;`, {actPrizeId}, {cache: MINUTE_TIMES / 2}),
|
|
|
mysqlCli.query(`select count(distinct uid) as join_num from ${TABLE_ACT_PRIZE_PRODUCT_USER}
|
|
|
where act_prize_id = :actPrizeId;`, {actPrizeId})
|
|
|
]);
|
...
|
...
|
@@ -241,7 +231,7 @@ module.exports = class extends global.yoho.BaseModel { |
|
|
*/
|
|
|
async sendPrizeCode(uid, actPrizeId, extra = {}) {
|
|
|
// 查询用户参与次数
|
|
|
const TimesCacheKey = `${actPrizeId}_${uid}`;
|
|
|
const TimesCacheKey = `times_${actPrizeId}_${uid}`;
|
|
|
|
|
|
let userJoinTimes = userTimesCache.get(TimesCacheKey);
|
|
|
|
...
|
...
|
@@ -252,6 +242,7 @@ module.exports = class extends global.yoho.BaseModel { |
|
|
uid
|
|
|
});
|
|
|
|
|
|
// 用户参与次数超限(业务缓存)
|
|
|
if (userJoinTimes >= MAX_JOIN_TIMES) {
|
|
|
userTimesCache.set(TimesCacheKey, userJoinTimes);
|
|
|
}
|
...
|
...
|
|