From a256bc0ff4ed7a02f9c7c4ba018d840345e174b2 Mon Sep 17 00:00:00 2001 From: chuhang <891580081@qq.com> Date: Thu, 30 Sep 2021 09:44:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(uboot-common):=20=E5=B9=82=E7=AD=89?= =?UTF-8?q?=E6=80=A7=E4=BF=AE=E6=94=B9redis=20key=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../idempotent/aspect/IdempotentAspect.java | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/idempotent/aspect/IdempotentAspect.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/idempotent/aspect/IdempotentAspect.java index ddfc551..f87ba9f 100644 --- a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/idempotent/aspect/IdempotentAspect.java +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/idempotent/aspect/IdempotentAspect.java @@ -5,6 +5,7 @@ import com.chinaunicom.mall.ebtp.common.exception.common.CommonExceptionEnum; import com.chinaunicom.mall.ebtp.common.idempotent.annotation.Idempotent; import com.chinaunicom.mall.ebtp.common.util.HttpContextUtils; import com.chinaunicom.mall.ebtp.common.util.JsonUtils; +import io.netty.util.concurrent.FastThreadLocal; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; @@ -20,6 +21,7 @@ import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.time.LocalDateTime; import java.util.Arrays; +import java.util.Optional; import java.util.concurrent.TimeUnit; /** @@ -35,6 +37,9 @@ public class IdempotentAspect { @Autowired private RedisTemplate redisTemplate; + /**redis键值*/ + public static final FastThreadLocal REDIS_KEY = new FastThreadLocal<>(); + @Pointcut("@annotation(com.chinaunicom.mall.ebtp.common.idempotent.annotation.Idempotent)") public void pointCut() { } @@ -52,6 +57,10 @@ public class IdempotentAspect { Idempotent idempotent = method.getAnnotation(Idempotent.class); String key = getKey(joinPoint, request); + /*清理线程*/ + this.removeThreadLocal(); + /*启用线程*/ + REDIS_KEY.set(key); long expireTime = idempotent.expireTime(); String info = idempotent.info(); @@ -87,21 +96,23 @@ public class IdempotentAspect { } @After("pointCut()") - public void afterPointCut(JoinPoint joinPoint) { - HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); - String key = getKey(joinPoint, request); + public void afterPointCut() { + String key = REDIS_KEY.get(); + Object object = redisTemplate.opsForValue().get(key); + Optional.ofNullable(object).ifPresent(o-> { + boolean delKey = (boolean) o; + if (delKey) { + redisTemplate.delete(key); + log.info("[idempotent]:has removed key={}", key); + } + }); + /*清理线程*/ + this.removeThreadLocal(); - Object o = redisTemplate.opsForValue().get(key); - if (o == null) { - return; - } + } - boolean delKey = (boolean) o; - - if (delKey) { - redisTemplate.delete(key); - log.info("[idempotent]:has removed key={}", key); - } + private void removeThreadLocal() { + REDIS_KEY.remove(); } }