diff --git a/src/main/java/com/kwan/springbootkwan/aop/RedisLockAspect.java b/src/main/java/com/kwan/springbootkwan/aop/RedisLockAspect.java index 13c9017f2de02299f25e6f5da073cb4ecd226a32..ac1e27507991b067cadab39bb7b8e8f9d9c3f89e 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 77773aa6e9472b59329707d96d9bf66906f739e8..cb851c63cfad73d92f8a9103728b1c5e5c02d453 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 0000000000000000000000000000000000000000..fe45713e123422d2dc64f646e628f771ffc4d70d --- /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