Showing
10 changed files
with
115 additions
and
13 deletions
@@ -16,7 +16,8 @@ public enum CacheKeyEnum { | @@ -16,7 +16,8 @@ public enum CacheKeyEnum { | ||
16 | DRAWLINE_ACTIVITYBO("yh:act_activitybo:", "drawline.activitybo.redisSwitch", "drawline.activitybo.redisExpire", "限购排队活动信息(BO)"), | 16 | DRAWLINE_ACTIVITYBO("yh:act_activitybo:", "drawline.activitybo.redisSwitch", "drawline.activitybo.redisExpire", "限购排队活动信息(BO)"), |
17 | DRAWLINE_QUEUELIST("yh:act_queuelist:", "drawline.queuelist.redisSwitch", "drawline.queuelist.redisExpire", "活动排队列表"), | 17 | DRAWLINE_QUEUELIST("yh:act_queuelist:", "drawline.queuelist.redisSwitch", "drawline.queuelist.redisExpire", "活动排队列表"), |
18 | DRAWLINE_USERACTIVITY("yh:act_useractivity:", "drawline.useractivity.redisSwitch", "drawline.useractivity.redisExpire", "活动排队列表"), | 18 | DRAWLINE_USERACTIVITY("yh:act_useractivity:", "drawline.useractivity.redisSwitch", "drawline.useractivity.redisExpire", "活动排队列表"), |
19 | - DRAWLINE_LUCKYUSERLIST("yh:act_luckyuser:", "drawline.luckyuserlist.redisSwitch", "drawline.luckyuserlist.redisExpire", "中奖用户信息列表"); | 19 | + DRAWLINE_LUCKYUSERLIST("yh:act_luckyuser:", "drawline.luckyuserlist.redisSwitch", "drawline.luckyuserlist.redisExpire", "中奖用户信息列表"), |
20 | + USERDAY_DRAWPRIZENUM("yh:act_userday:drawPrizeNum", "userday.drawPrizeNum.redisSwitch", "userday.drawPrizeNum.redisExpire", "会员日虚拟参与抽奖人数"); | ||
20 | 21 | ||
21 | // 缓存的key | 22 | // 缓存的key |
22 | private String cacheKey; | 23 | private String cacheKey; |
@@ -280,6 +280,32 @@ public class RedisValueCache { | @@ -280,6 +280,32 @@ public class RedisValueCache { | ||
280 | } | 280 | } |
281 | } | 281 | } |
282 | 282 | ||
283 | + /** | ||
284 | + * 清除 | ||
285 | + * | ||
286 | + * @param cacheEnum | ||
287 | + * 缓存开关的枚举 | ||
288 | + * @param obj | ||
289 | + * key的后缀 | ||
290 | + * | ||
291 | + */ | ||
292 | + public Long increment(CacheKeyEnum cacheEnum, Object obj, long delta, long timeout, TimeUnit unit) { | ||
293 | + logger.debug("Enter increment valueOperation redis value. cacheEnum is {}, obj is {}, delta is {}, timeout is {}, unit is {}", cacheEnum, obj, delta, timeout, unit); | ||
294 | + try { | ||
295 | + String cacheKey = CacheKeyHelper.getCacheKeyAndCheck(cacheEnum, obj); | ||
296 | + // 如果获取的key为空,则说明缓存开关是关闭的 | ||
297 | + if (StringUtils.isEmpty(cacheKey)) { | ||
298 | + return null; | ||
299 | + } | ||
300 | + Long result = yhValueOperations.increment(cacheKey, delta); | ||
301 | + yHRedisTemplate.longExpire(cacheKey, timeout, unit); | ||
302 | + return result; | ||
303 | + } catch (Exception e) { | ||
304 | + logger.warn("Redis exception. value redis increment . cacheEnum is {}, obj is {}, exception msg is {}", cacheEnum, obj, e.getMessage()); | ||
305 | + } | ||
306 | + return null; | ||
307 | + } | ||
308 | + | ||
283 | public static void main(String[] args) { | 309 | public static void main(String[] args) { |
284 | String str = "12345"; | 310 | String str = "12345"; |
285 | String jsonStr = JSON.toJSONString(str); | 311 | String jsonStr = JSON.toJSONString(str); |
@@ -74,4 +74,18 @@ public class UserdayPrizeLogController { | @@ -74,4 +74,18 @@ public class UserdayPrizeLogController { | ||
74 | return new ApiResponse(list); | 74 | return new ApiResponse(list); |
75 | } | 75 | } |
76 | 76 | ||
77 | + /** | ||
78 | + * 查询参与抽奖人数 | ||
79 | + * @param prize_type | ||
80 | + * @return | ||
81 | + * @throws Exception | ||
82 | + */ | ||
83 | + @RequestMapping("/queryPrizeLogNum") | ||
84 | + @ResponseBody | ||
85 | + public ApiResponse queryPrizeLogNum(int prize_type) throws Exception { | ||
86 | + log.info("enter queryPrizeLogNum. param prize_type={}", prize_type); | ||
87 | + int num = userdayPrizeLogService.queryPrizeLogNum(prize_type); | ||
88 | + return new ApiResponse(num); | ||
89 | + } | ||
90 | + | ||
77 | } | 91 | } |
@@ -23,4 +23,6 @@ public interface IUserdayPrizeLogDAO { | @@ -23,4 +23,6 @@ public interface IUserdayPrizeLogDAO { | ||
23 | int insertLog(UserdayPrizeLog userdayPrizeLog); | 23 | int insertLog(UserdayPrizeLog userdayPrizeLog); |
24 | 24 | ||
25 | List<UserdayPrizeLog> selectByUidAndPrizeType(@Param("uid") int uid, @Param("prizeType") int prizeType); | 25 | List<UserdayPrizeLog> selectByUidAndPrizeType(@Param("uid") int uid, @Param("prizeType") int prizeType); |
26 | + | ||
27 | + int selectNumByPrizeType(@Param("prizeType") int prize_type); | ||
26 | } | 28 | } |
@@ -116,6 +116,9 @@ | @@ -116,6 +116,9 @@ | ||
116 | from userday_prize_log | 116 | from userday_prize_log |
117 | where uid = #{uid,jdbcType=INTEGER} and prize_type = #{prizeType,jdbcType=INTEGER} and winning_date = UNIX_TIMESTAMP(CURRENT_DATE()) | 117 | where uid = #{uid,jdbcType=INTEGER} and prize_type = #{prizeType,jdbcType=INTEGER} and winning_date = UNIX_TIMESTAMP(CURRENT_DATE()) |
118 | </select> | 118 | </select> |
119 | + <select id="selectNumByPrizeType" resultType="java.lang.Integer"> | ||
120 | + select IFNULL(COUNT(DISTINCT uid), 0) from userday_prize_log where prize_type = #{prizeType,jdbcType=INTEGER} | ||
121 | + </select> | ||
119 | 122 | ||
120 | <insert id="insertLog" parameterType="com.yoho.userday.dal.model.UserdayPrizeLog"> | 123 | <insert id="insertLog" parameterType="com.yoho.userday.dal.model.UserdayPrizeLog"> |
121 | insert into userday_prize_log (uid, prize_type, | 124 | insert into userday_prize_log (uid, prize_type, |
@@ -24,4 +24,6 @@ public interface IUserdayPrizeLogService { | @@ -24,4 +24,6 @@ public interface IUserdayPrizeLogService { | ||
24 | * @throws Exception | 24 | * @throws Exception |
25 | */ | 25 | */ |
26 | List<UserdayPrizeLogResponseBO> queryPrizeLog(int uid, int prize_type) throws Exception; | 26 | List<UserdayPrizeLogResponseBO> queryPrizeLog(int uid, int prize_type) throws Exception; |
27 | + | ||
28 | + int queryPrizeLogNum(int prize_type); | ||
27 | } | 29 | } |
@@ -6,6 +6,8 @@ import java.util.Map; | @@ -6,6 +6,8 @@ import java.util.Map; | ||
6 | 6 | ||
7 | import javax.annotation.Resource; | 7 | import javax.annotation.Resource; |
8 | 8 | ||
9 | +import com.yoho.activity.common.redis.CacheKeyEnum; | ||
10 | +import com.yoho.activity.common.redis.RedisValueCache; | ||
9 | import org.apache.commons.collections.CollectionUtils; | 11 | import org.apache.commons.collections.CollectionUtils; |
10 | import org.slf4j.Logger; | 12 | import org.slf4j.Logger; |
11 | import org.slf4j.LoggerFactory; | 13 | import org.slf4j.LoggerFactory; |
@@ -35,7 +37,9 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService { | @@ -35,7 +37,9 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService { | ||
35 | private IUserdayPrizeLogDAO userdayPrizeLogDAO; | 37 | private IUserdayPrizeLogDAO userdayPrizeLogDAO; |
36 | @Resource(name = "userdayDrawPrizeService") | 38 | @Resource(name = "userdayDrawPrizeService") |
37 | Map<Integer, IUserdayDrawPrizeService> userdayDrawPrizeServiceMap; | 39 | Map<Integer, IUserdayDrawPrizeService> userdayDrawPrizeServiceMap; |
38 | - | 40 | + @Resource |
41 | + RedisValueCache redisValueCache; | ||
42 | + | ||
39 | @Value("${userday.open.day}") | 43 | @Value("${userday.open.day}") |
40 | String userdayOpenDay; | 44 | String userdayOpenDay; |
41 | 45 | ||
@@ -108,9 +112,9 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService { | @@ -108,9 +112,9 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService { | ||
108 | return userdayDrawPrizeService.existsPrizeLog(userdayPrizeLogList); | 112 | return userdayDrawPrizeService.existsPrizeLog(userdayPrizeLogList); |
109 | } | 113 | } |
110 | 114 | ||
111 | - @Override | ||
112 | - public List<UserdayPrizeLogResponseBO> queryPrizeLog(int uid, int prize_type) throws Exception { | ||
113 | - log.info("enter queryPrizeLog. param uid={}, prize_type={}", uid, prize_type); | 115 | + @Override |
116 | + public List<UserdayPrizeLogResponseBO> queryPrizeLog(int uid, int prize_type) throws Exception { | ||
117 | + log.info("enter queryPrizeLog. param uid={}, prize_type={}", uid, prize_type); | ||
114 | 118 | ||
115 | // (1)校验uid, prize_type | 119 | // (1)校验uid, prize_type |
116 | if (1 > uid || 1 > prize_type) { | 120 | if (1 > uid || 1 > prize_type) { |
@@ -120,19 +124,27 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService { | @@ -120,19 +124,27 @@ public class UserdayPrizeLogServiceImpl implements IUserdayPrizeLogService { | ||
120 | // (2)查询当天中奖日志 | 124 | // (2)查询当天中奖日志 |
121 | List<UserdayPrizeLog> userdayPrizeLogList = userdayPrizeLogDAO.selectByUidAndPrizeType(uid, prize_type); | 125 | List<UserdayPrizeLog> userdayPrizeLogList = userdayPrizeLogDAO.selectByUidAndPrizeType(uid, prize_type); |
122 | if (CollectionUtils.isEmpty(userdayPrizeLogList)) { | 126 | if (CollectionUtils.isEmpty(userdayPrizeLogList)) { |
123 | - return null; | 127 | + return null; |
124 | } | 128 | } |
125 | List<UserdayPrizeLogResponseBO> list = new ArrayList<>(userdayPrizeLogList.size()); | 129 | List<UserdayPrizeLogResponseBO> list = new ArrayList<>(userdayPrizeLogList.size()); |
126 | UserdayPrizeLogResponseBO bo = null; | 130 | UserdayPrizeLogResponseBO bo = null; |
127 | for (UserdayPrizeLog dal : userdayPrizeLogList) { | 131 | for (UserdayPrizeLog dal : userdayPrizeLogList) { |
128 | - bo = new UserdayPrizeLogResponseBO(); | ||
129 | - BeanUtils.copyProperties(dal, bo); | ||
130 | - bo.setWinningTime(DateUtils.long2DateStr(((long)bo.getCreateTime()) * 1000, "HH:mm")); | ||
131 | - list.add(bo); | 132 | + bo = new UserdayPrizeLogResponseBO(); |
133 | + BeanUtils.copyProperties(dal, bo); | ||
134 | + bo.setWinningTime(DateUtils.long2DateStr(((long) bo.getCreateTime()) * 1000, "HH:mm")); | ||
135 | + list.add(bo); | ||
132 | } | 136 | } |
133 | 137 | ||
134 | // (3)返回 | 138 | // (3)返回 |
135 | return list; | 139 | return list; |
136 | - } | 140 | + } |
141 | + | ||
142 | + @Override | ||
143 | + public int queryPrizeLogNum(int prize_type) { | ||
144 | + log.info("enter queryPrizeLogNum. param prize_type={}", prize_type); | ||
145 | + int realNum = userdayPrizeLogDAO.selectNumByPrizeType(prize_type); | ||
146 | + Integer virtualNum = redisValueCache.get(CacheKeyEnum.USERDAY_DRAWPRIZENUM, "", Integer.class); | ||
147 | + return realNum + (null == virtualNum ? 0 : virtualNum); | ||
148 | + } | ||
137 | 149 | ||
138 | } | 150 | } |
1 | +package com.yoho.activity.service.timer; | ||
2 | + | ||
3 | +import com.yoho.activity.common.redis.CacheKeyEnum; | ||
4 | +import com.yoho.activity.common.redis.RedisValueCache; | ||
5 | +import com.yoho.core.common.utils.DateUtil; | ||
6 | +import org.slf4j.Logger; | ||
7 | +import org.slf4j.LoggerFactory; | ||
8 | +import org.springframework.beans.factory.annotation.Value; | ||
9 | +import org.springframework.scheduling.annotation.Scheduled; | ||
10 | +import org.springframework.stereotype.Component; | ||
11 | + | ||
12 | +import javax.annotation.Resource; | ||
13 | +import java.util.concurrent.TimeUnit; | ||
14 | + | ||
15 | +/** | ||
16 | + * 会员日:每分钟增加抽奖人数定时任务 | ||
17 | + */ | ||
18 | +@Component | ||
19 | +public class UserdayDrawPrizeNumTimer { | ||
20 | + | ||
21 | + private static final Logger log = LoggerFactory.getLogger(UserdayDrawPrizeNumTimer.class); | ||
22 | + | ||
23 | + @Resource | ||
24 | + RedisValueCache redisValueCache; | ||
25 | + @Value("${userday.open.day}") | ||
26 | + String userdayOpenDay; | ||
27 | + | ||
28 | + @Scheduled(fixedDelay = 60 * 1000) | ||
29 | + public void run() { | ||
30 | + log.info("UserdayDrawPrizeNumTimer run."); | ||
31 | + | ||
32 | + // (1)校验当天是否是会员日 | ||
33 | + String dateStr = DateUtil.getcurrentDate(); | ||
34 | + if (!userdayOpenDay.equals(dateStr.substring(8, 10))) { | ||
35 | + return; | ||
36 | + } | ||
37 | + redisValueCache.increment(CacheKeyEnum.USERDAY_DRAWPRIZENUM, "", 10, 2, TimeUnit.MINUTES); | ||
38 | + } | ||
39 | + | ||
40 | +} |
@@ -66,4 +66,5 @@ rabbit_user=${rabbit_user} | @@ -66,4 +66,5 @@ rabbit_user=${rabbit_user} | ||
66 | rabbit_password =${rabbit_password} | 66 | rabbit_password =${rabbit_password} |
67 | 67 | ||
68 | userday.coupon=${userday.coupon} | 68 | userday.coupon=${userday.coupon} |
69 | -userday.open.day=${userday.open.day} | ||
69 | +userday.open.day=${userday.open.day} | ||
70 | +userday.drawPrizeNum.redisSwitch=true |
-
Please register or login to post a comment