From f4495f8677e645e167714740f1dcb660edd4e44a Mon Sep 17 00:00:00 2001 From: Admin <5695480+rui2450@user.noreply.gitee.com> Date: Mon, 26 Jun 2023 02:39:20 +0000 Subject: [PATCH] =?UTF-8?q?1.=E4=BC=98=E5=8C=96getCacheObject=E4=B8=BAsetI?= =?UTF-8?q?fAbsent=20=E4=BF=AE=E5=A4=8D=E5=9B=A0=E4=B8=BA=E7=BD=91?= =?UTF-8?q?=E7=BB=9C=E5=BB=B6=E8=BF=9F=E5=AF=BC=E8=87=B4=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E5=8F=91=E9=80=81=20=E7=BB=95=E8=BF=87?= =?UTF-8?q?=E9=99=90=E5=88=B6=E7=9A=84=E9=97=AE=E9=A2=98=202.StringBuilder?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BAStringJoiner=203.map.entrySet()?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BAmap.values()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Admin <5695480+rui2450@user.noreply.gitee.com> --- .../aspectj/RepeatSubmitAspect.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/ruoyi-common/ruoyi-common-idempotent/src/main/java/com/ruoyi/common/idempotent/aspectj/RepeatSubmitAspect.java b/ruoyi-common/ruoyi-common-idempotent/src/main/java/com/ruoyi/common/idempotent/aspectj/RepeatSubmitAspect.java index 964f0b0df..2f3c03d64 100644 --- a/ruoyi-common/ruoyi-common-idempotent/src/main/java/com/ruoyi/common/idempotent/aspectj/RepeatSubmitAspect.java +++ b/ruoyi-common/ruoyi-common-idempotent/src/main/java/com/ruoyi/common/idempotent/aspectj/RepeatSubmitAspect.java @@ -17,6 +17,8 @@ import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; +import org.redisson.api.RBucket; +import org.redisson.api.RedissonClient; import org.springframework.validation.BindingResult; import org.springframework.web.multipart.MultipartFile; @@ -25,6 +27,7 @@ import javax.servlet.http.HttpServletResponse; import java.time.Duration; import java.util.Collection; import java.util.Map; +import java.util.StringJoiner; /** * 防止重复提交(参考美团GTIS防重系统) @@ -56,9 +59,9 @@ public class RepeatSubmitAspect { submitKey = SecureUtil.md5(submitKey + ":" + nowParams); // 唯一标识(指定key + url + 消息头) String cacheRepeatKey = Constants.REPEAT_SUBMIT_KEY + url + submitKey; - String key = RedisUtils.getCacheObject(cacheRepeatKey); - if (key == null) { - RedisUtils.setCacheObject(cacheRepeatKey, "", Duration.ofMillis(interval)); + RedissonClient client = RedisUtils.getClient(); + RBucket bucket = client.getBucket(cacheRepeatKey); + if (bucket.setIfAbsent(cacheRepeatKey,Duration.ofMillis(interval))) { KEY_CACHE.set(cacheRepeatKey); } else { String message = repeatSubmit.message(); @@ -106,19 +109,19 @@ public class RepeatSubmitAspect { * 参数拼装 */ private String argsArrayToString(Object[] paramsArray) { - StringBuilder params = new StringBuilder(); + StringJoiner params = new StringJoiner( " "); if (paramsArray != null && paramsArray.length > 0) { for (Object o : paramsArray) { if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) { try { - params.append(JsonUtils.toJsonString(o)).append(" "); + params.add(JsonUtils.toJsonString(o)); } catch (Exception e) { e.printStackTrace(); } } } } - return params.toString().trim(); + return params.toString(); } /** @@ -139,13 +142,12 @@ public class RepeatSubmitAspect { } } else if (Map.class.isAssignableFrom(clazz)) { Map map = (Map) o; - for (Object value : map.entrySet()) { - Map.Entry entry = (Map.Entry) value; - return entry.getValue() instanceof MultipartFile; + for (Object value : map.values()) { + return value instanceof MultipartFile; } } return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse || o instanceof BindingResult; } -} +} \ No newline at end of file