MichelleChung
2023-12-29 4ceb79afa35b2be369b8c84219524d687451974b
ruoyi-common/ruoyi-common-idempotent/src/main/java/org/dromara/common/idempotent/aspectj/RepeatSubmitAspect.java
@@ -5,6 +5,7 @@
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import org.dromara.common.core.constant.GlobalConstants;
import org.dromara.common.core.context.ThreadLocalHolder;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MessageUtils;
@@ -36,7 +37,7 @@
@Aspect
public class RepeatSubmitAspect {
    private static final ThreadLocal<String> KEY_CACHE = new ThreadLocal<>();
    private static final String KEY_CACHE = "keyCache";
    @Before("@annotation(repeatSubmit)")
    public void doBefore(JoinPoint point, RepeatSubmit repeatSubmit) throws Throwable {
@@ -59,7 +60,7 @@
        // 唯一标识(指定key + url + 消息头)
        String cacheRepeatKey = GlobalConstants.REPEAT_SUBMIT_KEY + url + submitKey;
        if (RedisUtils.setObjectIfAbsent(cacheRepeatKey, "", Duration.ofMillis(interval))) {
            KEY_CACHE.set(cacheRepeatKey);
            ThreadLocalHolder.set(KEY_CACHE, cacheRepeatKey);
        } else {
            String message = repeatSubmit.message();
            if (StringUtils.startsWith(message, "{") && StringUtils.endsWith(message, "}")) {
@@ -82,9 +83,10 @@
                if (r.getCode() == R.SUCCESS) {
                    return;
                }
                RedisUtils.deleteObject(KEY_CACHE.get());
                String cacheKey = ThreadLocalHolder.get(KEY_CACHE);
                RedisUtils.deleteObject(cacheKey);
            } finally {
                KEY_CACHE.remove();
                ThreadLocalHolder.remove(KEY_CACHE);
            }
        }
    }
@@ -97,8 +99,9 @@
     */
    @AfterThrowing(value = "@annotation(repeatSubmit)", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, RepeatSubmit repeatSubmit, Exception e) {
        RedisUtils.deleteObject(KEY_CACHE.get());
        KEY_CACHE.remove();
        String cacheKey = ThreadLocalHolder.get(KEY_CACHE);
        RedisUtils.deleteObject(cacheKey);
        ThreadLocalHolder.remove(KEY_CACHE);
    }
    /**