Skip to content

Commit 9b8dbf6

Browse files
committed
feat: Redis Lock 修改为基于 RedisTemplate<String, Object>
1 parent c8192ba commit 9b8dbf6

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

  • common/plugin/cache/src/main/java/top/cadecode/uniboot/common/plugin/cache/util

common/plugin/cache/src/main/java/top/cadecode/uniboot/common/plugin/cache/util/RedisLockKit.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import lombok.AllArgsConstructor;
44
import lombok.Data;
55
import lombok.RequiredArgsConstructor;
6-
import org.springframework.data.redis.core.StringRedisTemplate;
6+
import org.springframework.data.redis.core.RedisTemplate;
77
import org.springframework.stereotype.Component;
88
import top.cadecode.uniboot.common.plugin.cache.exception.RedisLockException;
99

@@ -23,7 +23,7 @@
2323
@RequiredArgsConstructor
2424
public class RedisLockKit {
2525

26-
private final StringRedisTemplate redisTemplate;
26+
private final RedisTemplate<String, Object> redisTemplate;
2727

2828
private final Map<String, LockContent> contentMap = new ConcurrentHashMap<>();
2929

@@ -39,7 +39,7 @@ public void lock(String name) {
3939
lock(name, "");
4040
}
4141

42-
public void lock(String name, String value) {
42+
public void lock(String name, Object value) {
4343
if (checkReentrant(name)) {
4444
storeLock(name, null, true);
4545
return;
@@ -62,7 +62,7 @@ public boolean tryLock(String name) {
6262
return tryLock(name, "");
6363
}
6464

65-
public boolean tryLock(String name, String value) {
65+
public boolean tryLock(String name, Object value) {
6666
if (checkReentrant(name)) {
6767
storeLock(name, null, true);
6868
return true;
@@ -82,7 +82,7 @@ public boolean tryLock(String name, long timeout, TimeUnit timeUnit) {
8282
return tryLock(name, "", timeout, timeUnit);
8383
}
8484

85-
public boolean tryLock(String name, String value, long timeout, TimeUnit timeUnit) {
85+
public boolean tryLock(String name, Object value, long timeout, TimeUnit timeUnit) {
8686
if (checkReentrant(name)) {
8787
storeLock(name, null, true);
8888
return true;
@@ -190,7 +190,7 @@ private void storeLock(String name, ScheduledFuture<?> future, boolean reentrant
190190
* @param name 锁名称
191191
* @return 是否设置成功
192192
*/
193-
private boolean tryLock0(String name, String value) {
193+
private boolean tryLock0(String name, Object value) {
194194
Boolean success = redisTemplate.opsForValue().setIfAbsent(name, value, 30, TimeUnit.SECONDS);
195195
if (Objects.equals(success, false)) {
196196
return false;
@@ -207,7 +207,7 @@ private boolean tryLock0(String name, String value) {
207207
* @param name 锁名称
208208
* @return ScheduledFuture
209209
*/
210-
private ScheduledFuture<?> renewLock(String name, String value) {
210+
private ScheduledFuture<?> renewLock(String name, Object value) {
211211
// 有效期设置为 30s,每 15 秒重置
212212
return executor.scheduleAtFixedRate(() -> {
213213
Boolean success = redisTemplate.opsForValue().setIfPresent(name, value, 30, TimeUnit.SECONDS);

0 commit comments

Comments
 (0)