From fcaa2e390a3f901b2d99de40da1cbba181181991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Tue, 12 Sep 2023 15:43:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E7=BB=AD=E6=9C=9F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springbootkwan/aop/RedisLockAspect.java | 6 ++++ .../controller/DepartmentController.java | 2 +- .../schedule/RedisLockRenewal.java | 36 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/kwan/springbootkwan/schedule/RedisLockRenewal.java diff --git a/src/main/java/com/kwan/springbootkwan/aop/RedisLockAspect.java b/src/main/java/com/kwan/springbootkwan/aop/RedisLockAspect.java index 13c9017..ac1e275 100644 --- a/src/main/java/com/kwan/springbootkwan/aop/RedisLockAspect.java +++ b/src/main/java/com/kwan/springbootkwan/aop/RedisLockAspect.java @@ -1,9 +1,11 @@ package com.kwan.springbootkwan.aop; import com.kwan.springbootkwan.annotation.RedisLock; +import com.kwan.springbootkwan.schedule.RedisLockRenewal; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.annotation.Order; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; @@ -21,6 +23,9 @@ public class RedisLockAspect { private final StringRedisTemplate redisTemplate; + @Autowired + private RedisLockRenewal redisLockRenewal; + public RedisLockAspect(StringRedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } @@ -36,6 +41,7 @@ public class RedisLockAspect { if (locked) { if (StringUtils.hasText(lockKey)) { redisTemplate.opsForValue().set(lockKey, "locked", redisLock.expire(), TimeUnit.MILLISECONDS); + redisLockRenewal.scheduleRenewal(lockKey, 20000, redisLock.expire()); } return joinPoint.proceed(); } else { diff --git a/src/main/java/com/kwan/springbootkwan/controller/DepartmentController.java b/src/main/java/com/kwan/springbootkwan/controller/DepartmentController.java index 77773aa..cb851c6 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/DepartmentController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/DepartmentController.java @@ -70,7 +70,7 @@ public class DepartmentController { @GetMapping("/all") - @RedisLock(key = "myLock_queryTreeAll", expire = 5000, timeout = 3000) + @RedisLock(key = "myLock_queryTreeAll", expire = 30000, timeout = 3000) // @RedisLock(key = "myLock_queryTreeAll", timeout = 10000) public ResponseEntity> queryTreeAll() { return ResponseEntity.ok(this.departmentService.queryTreeAll()); diff --git a/src/main/java/com/kwan/springbootkwan/schedule/RedisLockRenewal.java b/src/main/java/com/kwan/springbootkwan/schedule/RedisLockRenewal.java new file mode 100644 index 0000000..fe45713 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/schedule/RedisLockRenewal.java @@ -0,0 +1,36 @@ +package com.kwan.springbootkwan.schedule; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Component; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +@Component +public class RedisLockRenewal { + + private final StringRedisTemplate redisTemplate; + /** + * 定时任务线程池 + */ + private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); + + @Autowired + public RedisLockRenewal(StringRedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + public void scheduleRenewal(String lockKey, long renewalIntervalMillis, long lockExpireMillis) { + scheduler.scheduleAtFixedRate(() -> { + try { + // 续期操作:设置新的过期时间 + redisTemplate.expire(lockKey, lockExpireMillis, TimeUnit.MILLISECONDS); + } catch (Exception e) { + // 处理续期失败的情况,可以记录日志或采取其他操作 + e.printStackTrace(); + } + }, renewalIntervalMillis, renewalIntervalMillis, TimeUnit.MILLISECONDS); + } +} \ No newline at end of file -- GitLab