From 19da4f752279945122d703ffd95e77266feaf4d1 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期一, 26 六月 2023 23:37:37 +0800 Subject: [PATCH] update 优化 RepeatSubmitAspect 逻辑避免并发请求问题 --- ruoyi-common/ruoyi-common-idempotent/src/main/java/org/dromara/common/idempotent/aspectj/RepeatSubmitAspect.java | 40 +++++++++++++++++----------------------- 1 files changed, 17 insertions(+), 23 deletions(-) diff --git a/ruoyi-common/ruoyi-common-idempotent/src/main/java/org/dromara/common/idempotent/aspectj/RepeatSubmitAspect.java b/ruoyi-common/ruoyi-common-idempotent/src/main/java/org/dromara/common/idempotent/aspectj/RepeatSubmitAspect.java index 77f2646..016fe0f 100644 --- a/ruoyi-common/ruoyi-common-idempotent/src/main/java/org/dromara/common/idempotent/aspectj/RepeatSubmitAspect.java +++ b/ruoyi-common/ruoyi-common-idempotent/src/main/java/org/dromara/common/idempotent/aspectj/RepeatSubmitAspect.java @@ -1,6 +1,7 @@ package org.dromara.common.idempotent.aspectj; import cn.dev33.satoken.SaManager; +import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.crypto.SecureUtil; import org.dromara.common.core.constant.GlobalConstants; @@ -25,6 +26,7 @@ import java.time.Duration; import java.util.Collection; import java.util.Map; +import java.util.StringJoiner; /** * 闃叉閲嶅鎻愪氦(鍙傝�冪編鍥TIS闃查噸绯荤粺) @@ -39,10 +41,8 @@ @Before("@annotation(repeatSubmit)") public void doBefore(JoinPoint point, RepeatSubmit repeatSubmit) throws Throwable { // 濡傛灉娉ㄨВ涓嶄负0 鍒欎娇鐢ㄦ敞瑙f暟鍊� - long interval = 0; - if (repeatSubmit.interval() > 0) { - interval = repeatSubmit.timeUnit().toMillis(repeatSubmit.interval()); - } + long interval = repeatSubmit.timeUnit().toMillis(repeatSubmit.interval()); + if (interval < 1000) { throw new ServiceException("閲嶅鎻愪氦闂撮殧鏃堕棿涓嶈兘灏忎簬'1'绉�"); } @@ -58,9 +58,7 @@ submitKey = SecureUtil.md5(submitKey + ":" + nowParams); // 鍞竴鏍囪瘑锛堟寚瀹歬ey + url + 娑堟伅澶达級 String cacheRepeatKey = GlobalConstants.REPEAT_SUBMIT_KEY + url + submitKey; - String key = RedisUtils.getCacheObject(cacheRepeatKey); - if (key == null) { - RedisUtils.setCacheObject(cacheRepeatKey, "", Duration.ofMillis(interval)); + if (RedisUtils.setObjectIfAbsent(cacheRepeatKey, "", Duration.ofMillis(interval))) { KEY_CACHE.set(cacheRepeatKey); } else { String message = repeatSubmit.message(); @@ -78,7 +76,7 @@ */ @AfterReturning(pointcut = "@annotation(repeatSubmit)", returning = "jsonResult") public void doAfterReturning(JoinPoint joinPoint, RepeatSubmit repeatSubmit, Object jsonResult) { - if (jsonResult instanceof R r) { + if (jsonResult instanceof R<?> r) { try { // 鎴愬姛鍒欎笉鍒犻櫎redis鏁版嵁 淇濊瘉鍦ㄦ湁鏁堟椂闂村唴鏃犳硶閲嶅鎻愪氦 if (r.getCode() == R.SUCCESS) { @@ -107,19 +105,16 @@ * 鍙傛暟鎷艰 */ private String argsArrayToString(Object[] paramsArray) { - StringBuilder params = new StringBuilder(); - if (paramsArray != null && paramsArray.length > 0) { - for (Object o : paramsArray) { - if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) { - try { - params.append(JsonUtils.toJsonString(o)).append(" "); - } catch (Exception e) { - e.printStackTrace(); - } - } + StringJoiner params = new StringJoiner(" "); + if (ArrayUtil.isEmpty(paramsArray)) { + return params.toString(); + } + for (Object o : paramsArray) { + if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) { + params.add(JsonUtils.toJsonString(o)); } } - return params.toString().trim(); + return params.toString(); } /** @@ -140,13 +135,12 @@ } } 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; + || o instanceof BindingResult; } } -- Gitblit v1.9.3