Skip to content

Commit

Permalink
feat(cache/redisson): RedisUtils 新增上锁、释放锁方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Nov 7, 2024
1 parent bd60411 commit 04498ff
Showing 1 changed file with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
* Redis 工具类
Expand Down Expand Up @@ -207,6 +209,80 @@ public static boolean rateLimit(String key, RateType rateType, int rate, int rat
return rateLimiter.tryAcquire(1);
}

/**
* 尝试获取锁
*
* @param key 键
* @param expireTime 锁过期时间(单位:毫秒)
* @param timeout 获取锁超时时间(单位:毫秒)
* @return true:成功;false:失败
* @since 2.7.2
*/
public static boolean tryLock(String key, long expireTime, long timeout) {
return tryLock(key, expireTime, timeout, TimeUnit.MILLISECONDS);
}

/**
* 释放锁
*
* @param key 键
* @return true:释放成功;false:释放失败
* @since 2.7.2
*/
public static boolean unlock(String key) {
RLock lock = getLock(key);
return unlock(lock);
}

/**
* 尝试获取锁
*
* @param key 键
* @param expireTime 锁过期时间
* @param timeout 获取锁超时时间
* @param unit 时间单位
* @return true:成功;false:失败
* @since 2.7.2
*/
public static boolean tryLock(String key, long expireTime, long timeout, TimeUnit unit) {
RLock lock = getLock(key);
try {
return lock.tryLock(timeout, expireTime, unit);
} catch (InterruptedException e) {
return false;
}
}

/**
* 释放锁
*
* @param lock 锁实例
* @return true:释放成功;false:释放失败
* @since 2.7.2
*/
public static boolean unlock(RLock lock) {
if (lock.isHeldByCurrentThread()) {
try {
lock.unlockAsync().get();
return true;
} catch (ExecutionException | InterruptedException e) {
return false;
}
}
return false;
}

/**
* 获取锁实例
*
* @param key 键
* @return 锁实例
* @since 2.7.2
*/
public static RLock getLock(String key) {
return CLIENT.getLock(key);
}

/**
* 格式化键,将各子键用 : 拼接起来
*
Expand Down

0 comments on commit 04498ff

Please sign in to comment.