From 3fb78b0cb1d250a06d8468f6f61a89768ae2e390 Mon Sep 17 00:00:00 2001 From: zhanghl <253316343@qq.com> Date: 星期五, 16 五月 2025 13:57:04 +0800 Subject: [PATCH] 增加Redis锁组件 --- aps-common/aps-common-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports | 1 + aps-common/aps-common-redis/src/main/java/com/aps/common/redis/service/RedisLockUtils.java | 45 +++++++++++++++++++++++++++++++++++++++++++++ aps-common/aps-common-redis/pom.xml | 12 ++++++++++-- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/aps-common/aps-common-redis/pom.xml b/aps-common/aps-common-redis/pom.xml index 226b45c..80d2edc 100644 --- a/aps-common/aps-common-redis/pom.xml +++ b/aps-common/aps-common-redis/pom.xml @@ -22,12 +22,20 @@ <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> - + <dependency> + <groupId>org.redisson</groupId> + <artifactId>redisson-spring-boot-starter</artifactId> + <version>3.39.0</version> + </dependency> <!-- RuoYi Common Core--> <dependency> <groupId>com.aps</groupId> <artifactId>aps-common-core</artifactId> </dependency> - + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <scope>provided</scope> + </dependency> </dependencies> </project> \ No newline at end of file diff --git a/aps-common/aps-common-redis/src/main/java/com/aps/common/redis/service/RedisLockUtils.java b/aps-common/aps-common-redis/src/main/java/com/aps/common/redis/service/RedisLockUtils.java new file mode 100644 index 0000000..659c1fe --- /dev/null +++ b/aps-common/aps-common-redis/src/main/java/com/aps/common/redis/service/RedisLockUtils.java @@ -0,0 +1,45 @@ +package com.aps.common.redis.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.script.DefaultRedisScript; +import org.springframework.data.redis.core.script.RedisScript; +import org.springframework.stereotype.Component; + +import java.time.Duration; +import java.util.Collections; + +/** + * Redis 鍒嗗竷寮忛攣 + * + **/ +@Component +public class RedisLockUtils { + + @Autowired + private RedisTemplate redisTemplate; + + //鍒嗗竷寮忛攣杩囨湡鏃堕棿 s 鍙互鏍规嵁涓氬姟鑷繁璋冭妭 + private static final Long LOCK_REDIS_TIMEOUT = 10L; + //鍒嗗竷寮忛攣浼戠湢 鑷� 鍐嶆灏濊瘯鑾峰彇 鐨勭瓑寰呮椂闂� ms 鍙互鏍规嵁涓氬姟鑷繁璋冭妭 + public static final Long LOCK_REDIS_WAIT = 500L; + + + /** + * 鍔犻攣 + **/ + public Boolean getLock(String key,String value,Long timeoutSeconds){ + Boolean lockStatus = this.redisTemplate.opsForValue().setIfAbsent(key,value, Duration.ofSeconds(timeoutSeconds)); + return lockStatus; + } + + /** + * 閲婃斁閿� + **/ + public Long releaseLock(String key,String value){ + String luaScript = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"; + RedisScript<Long> redisScript = new DefaultRedisScript<>(luaScript,Long.class); + Long releaseStatus = (Long)this.redisTemplate.execute(redisScript, Collections.singletonList(key),value); + return releaseStatus; + } +} \ No newline at end of file diff --git a/aps-common/aps-common-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/aps-common/aps-common-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index f8ab30d..c529ae9 100644 --- a/aps-common/aps-common-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/aps-common/aps-common-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,2 +1,3 @@ com.aps.common.redis.configure.RedisConfig com.aps.common.redis.service.RedisService +com.aps.common.redis.service.RedisLockUtils \ No newline at end of file -- Gitblit v1.9.3