From 7892ec5a596e6d1fce07bef7661d802dc3fb6afb Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期五, 26 十一月 2021 16:32:56 +0800 Subject: [PATCH] fix 修复重复提交不生效问题 由于概念不同 使用 RedisUtils 重构 --- ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java | 11 ++++++----- ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisLockController.java | 10 ---------- README.md | 2 +- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b8f86f1..cf958eb 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ | Redis瀹㈡埛绔� | Redisson | [Redisson鏂囨。](https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95) | 鏀寔鍗曟満銆侀泦缇ら厤缃� | | 鍒嗗竷寮忛檺娴� | Redisson | [Redisson鏂囨。](https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95) | 鍏ㄥ眬銆佽姹侷P銆侀泦缇D 澶氱闄愭祦 | | 鍒嗗竷寮忛攣 | Lock4j | [Lock4j瀹樼綉](https://gitee.com/baomidou/lock4j) | 娉ㄨВ閿併�佸伐鍏烽攣 澶氱澶氭牱 | -| 鍒嗗竷寮忓箓绛� | Lock4j | [Lock4j鏂囨。](https://gitee.com/baomidou/lock4j) | 鍩轰簬鍒嗗竷寮忛攣瀹炵幇 | +| 鍒嗗竷寮忓箓绛� | Redisson | [Lock4j鏂囨。](https://gitee.com/baomidou/lock4j) | 鎷︽埅閲嶅鎻愪氦 | | 鍒嗗竷寮忔棩蹇� | TLog | [TLog鏂囨。](https://yomahub.com/tlog/docs) | 鏀寔璺熻釜閾捐矾鏃ュ織璁板綍銆佹�ц兘鍒嗘瀽銆侀摼璺帓鏌� | | 鍒嗗竷寮忎换鍔¤皟搴� | Xxl-Job | [Xxl-Job瀹樼綉](https://www.xuxueli.com/xxl-job/) | 楂樻�ц兘 楂樺彲闈� 鏄撴墿灞� | | 鏂囦欢瀛樺偍 | Minio | [Minio鏂囨。](https://docs.min.io/) | 鏈湴瀛樺偍 | diff --git a/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisLockController.java b/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisLockController.java index b2d66f5..94ad3c3 100644 --- a/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisLockController.java +++ b/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisLockController.java @@ -9,7 +9,6 @@ import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.Cacheable; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -74,13 +73,4 @@ return AjaxResult.success("鎿嶄綔鎴愬姛", value); } - /** - * 娴嬭瘯spring-cache娉ㄨВ - */ - @ApiOperation("娴嬭瘯spring-cache娉ㄨВ") - @Cacheable(value = "test", key = "#key") - @GetMapping("/testCache") - public AjaxResult<String> testCache(String key) { - return AjaxResult.success("鎿嶄綔鎴愬姛", key); - } } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java index 71d438c..f8f5c37 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java @@ -1,13 +1,12 @@ package com.ruoyi.framework.aspectj; import cn.hutool.crypto.SecureUtil; -import com.baomidou.lock.LockInfo; -import com.baomidou.lock.LockTemplate; import com.ruoyi.common.annotation.RepeatSubmit; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.properties.TokenProperties; import com.ruoyi.common.utils.JsonUtils; +import com.ruoyi.common.utils.RedisUtils; import com.ruoyi.common.utils.ServletUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.framework.config.properties.RepeatSubmitProperties; @@ -25,6 +24,7 @@ import javax.servlet.http.HttpServletResponse; import java.util.Collection; import java.util.Map; +import java.util.concurrent.TimeUnit; /** * 闃叉閲嶅鎻愪氦 @@ -39,7 +39,6 @@ private final TokenProperties tokenProperties; private final RepeatSubmitProperties repeatSubmitProperties; - private final LockTemplate lockTemplate; @Before("@annotation(repeatSubmit)") public void doBefore(JoinPoint point, RepeatSubmit repeatSubmit) throws Throwable { @@ -65,8 +64,10 @@ submitKey = SecureUtil.md5(submitKey + ":" + nowParams); // 鍞竴鏍囪瘑锛堟寚瀹歬ey + 娑堟伅澶达級 String cacheRepeatKey = Constants.REPEAT_SUBMIT_KEY + submitKey; - LockInfo lock = lockTemplate.lock(cacheRepeatKey, interval, interval / 2); - if (lock == null) { + String key = RedisUtils.getCacheObject(cacheRepeatKey); + if (key == null) { + RedisUtils.setCacheObject(cacheRepeatKey, "", interval, TimeUnit.MILLISECONDS); + } else { throw new ServiceException(repeatSubmit.message()); } } -- Gitblit v1.9.3