抽奖接口加锁
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
package com.sqx.modules.discSpinning.controller;
|
package com.sqx.modules.discSpinning.controller;
|
||||||
|
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageHelper;
|
||||||
import com.github.pagehelper.PageInfo;
|
import com.github.pagehelper.PageInfo;
|
||||||
import com.sqx.common.annotation.Debounce;
|
import com.sqx.common.annotation.Debounce;
|
||||||
import com.sqx.common.exception.CzgException;
|
import com.sqx.common.exception.CzgException;
|
||||||
|
import com.sqx.common.exception.SqxException;
|
||||||
import com.sqx.common.utils.*;
|
import com.sqx.common.utils.*;
|
||||||
import com.sqx.modules.app.annotation.Login;
|
import com.sqx.modules.app.annotation.Login;
|
||||||
import com.sqx.modules.app.service.UserInfoService;
|
import com.sqx.modules.app.service.UserInfoService;
|
||||||
@@ -175,6 +177,17 @@ public class DiscSpinningController {
|
|||||||
@ApiOperation("抽取大转盘")
|
@ApiOperation("抽取大转盘")
|
||||||
@Debounce(value = "#userId")
|
@Debounce(value = "#userId")
|
||||||
public Result draw(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestParam(required = false, defaultValue = "1") Integer source) throws CzgException {
|
public Result draw(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestParam(required = false, defaultValue = "1") Integer source) throws CzgException {
|
||||||
|
String lockKey = StrUtil.format("user:disc-spinning:limit:user:lock:{}", userId);
|
||||||
|
if (redisUtils.hasKey(lockKey)) {
|
||||||
|
return Result.error("抽奖进行中,请等待!");
|
||||||
|
}
|
||||||
|
String intervalKey = StrUtil.format("user:disc-spinning:interval:limit:user:{}", userId);
|
||||||
|
if (redisUtils.hasKey(intervalKey)) {
|
||||||
|
return Result.error("抽奖请求过于频繁,请稍后再试!");
|
||||||
|
}
|
||||||
|
redisUtils.set(lockKey, true, 30);
|
||||||
|
Result ret = Result.error();
|
||||||
|
try {
|
||||||
double amount = 0;
|
double amount = 0;
|
||||||
Long sourceId = null;
|
Long sourceId = null;
|
||||||
Integer i = recordService.countDraw(userId);
|
Integer i = recordService.countDraw(userId);
|
||||||
@@ -241,10 +254,22 @@ public class DiscSpinningController {
|
|||||||
if (DataLimitUtil.isAllowed(RedisKeys.getDateKey("spinning:draw:") + sourceId, 120)) {
|
if (DataLimitUtil.isAllowed(RedisKeys.getDateKey("spinning:draw:") + sourceId, 120)) {
|
||||||
DiscSpinningRecord draws = discSpinningService.draws(i == null ? 1 : i + 1, amount, sourceId, userId, source);
|
DiscSpinningRecord draws = discSpinningService.draws(i == null ? 1 : i + 1, amount, sourceId, userId, source);
|
||||||
executor.schedule(() -> discSpinningService.receive1(draws), 4, TimeUnit.SECONDS);
|
executor.schedule(() -> discSpinningService.receive1(draws), 4, TimeUnit.SECONDS);
|
||||||
|
redisUtils.set(intervalKey, true, 5);
|
||||||
return new Result().put("data", draws);
|
return new Result().put("data", draws);
|
||||||
} else {
|
} else {
|
||||||
throw new CzgException("异常请求");
|
throw new CzgException("异常请求");
|
||||||
}
|
}
|
||||||
|
} catch (CzgException e) {
|
||||||
|
throw e;
|
||||||
|
} catch (SqxException e) {
|
||||||
|
ret.put("msg", e.getMsg());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("抽奖异常", e);
|
||||||
|
ret.put("msg", "请求失败,请联系管理员");
|
||||||
|
} finally {
|
||||||
|
redisUtils.delete(lockKey);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation("大转盘奖项领取")
|
@ApiOperation("大转盘奖项领取")
|
||||||
|
|||||||
Reference in New Issue
Block a user