fix:创建订单增加节流操作
This commit is contained in:
@@ -3,6 +3,7 @@ package cn.ysk.cashier.controller.product;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.ysk.cashier.annotation.AnonymousAccess;
|
||||
import cn.ysk.cashier.annotation.Limit;
|
||||
import cn.ysk.cashier.annotation.Log;
|
||||
import cn.ysk.cashier.config.security.security.TokenProvider;
|
||||
import cn.ysk.cashier.dto.shoptable.*;
|
||||
@@ -18,6 +19,7 @@ import cn.ysk.cashier.service.product.TbProductService;
|
||||
import cn.ysk.cashier.service.shop.TbShopTableService;
|
||||
import cn.ysk.cashier.utils.RabbitMsgUtils;
|
||||
import cn.ysk.cashier.utils.RedisUtils;
|
||||
import cn.ysk.cashier.utils.Utils;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
@@ -156,7 +158,8 @@ public class TbPlaceController {
|
||||
|
||||
@PostMapping("/order")
|
||||
@ApiOperation("代客下单 查询购物车 /shop/table")
|
||||
public ResponseEntity<Object> createOrder(@RequestBody CreateOrderDTO createOrderDTO) {
|
||||
public ResponseEntity<Object> createOrder(HttpServletRequest request, @RequestBody CreateOrderDTO createOrderDTO) {
|
||||
Utils.checkLimit(tokenProvider.getToken(request), 1, 400);
|
||||
return ResponseEntity.ok(tbShopTableService.createOrder(createOrderDTO, false));
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -12,10 +13,16 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Component
|
||||
public class Utils {
|
||||
public static int retryCount = 5;
|
||||
private static StringRedisTemplate redisTemplate;
|
||||
private static final Logger log = LoggerFactory.getLogger(Utils.class);
|
||||
|
||||
public Utils(StringRedisTemplate redisTemplate) {
|
||||
Utils.redisTemplate = redisTemplate;
|
||||
}
|
||||
|
||||
public static <T> void catchErrNoReturn(Supplier<T> supplier) {
|
||||
try {
|
||||
supplier.get();
|
||||
@@ -116,4 +123,42 @@ public class Utils {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void checkLimit(String key, int maxRequests, long timeWindow) {
|
||||
// 获取当前时间戳
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
// 获取之前的时间戳和计数从 Redis
|
||||
String prevTimestamp = redisTemplate.opsForValue().get(key + ":timestamp");
|
||||
String prevCount = redisTemplate.opsForValue().get(key + ":count");
|
||||
|
||||
// 如果这是第一次请求,设置初始值
|
||||
if (prevTimestamp == null || prevCount == null) {
|
||||
redisTemplate.opsForValue().set(key + ":timestamp", String.valueOf(now));
|
||||
redisTemplate.opsForValue().set(key + ":count", "1");
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算自上一次请求以来经过的时间
|
||||
long elapsed = now - Long.parseLong(prevTimestamp);
|
||||
|
||||
// 如果时间窗口已经过去,重置计数
|
||||
if (elapsed > timeWindow) {
|
||||
redisTemplate.opsForValue().set(key + ":timestamp", String.valueOf(now));
|
||||
redisTemplate.opsForValue().set(key + ":count", "1");
|
||||
return;
|
||||
}
|
||||
|
||||
// 增加计数
|
||||
int count = Integer.parseInt(prevCount) + 1;
|
||||
|
||||
// 如果计数超过最大值,返回 false
|
||||
if (count > maxRequests) {
|
||||
throw new BadRequestException("操作太快啦,请稍后再试");
|
||||
}
|
||||
|
||||
// 更新计数和时间戳
|
||||
redisTemplate.opsForValue().set(key + ":count", String.valueOf(count));
|
||||
redisTemplate.opsForValue().set(key + ":timestamp", String.valueOf(now));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user