package com.qianwen.core.redis.lock;
|
|
import java.util.concurrent.TimeUnit;
|
import org.redisson.api.RLock;
|
import org.redisson.api.RedissonClient;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import com.qianwen.core.tool.function.CheckedSupplier;
|
import com.qianwen.core.tool.utils.Exceptions;
|
/* loaded from: blade-starter-redis-9.3.0.0-SNAPSHOT.jar:org/springblade/core/redis/lock/RedisLockClientImpl.class */
|
public class RedisLockClientImpl implements RedisLockClient {
|
private static final Logger log = LoggerFactory.getLogger(RedisLockClientImpl.class);
|
private final RedissonClient redissonClient;
|
|
public RedisLockClientImpl(final RedissonClient redissonClient) {
|
this.redissonClient = redissonClient;
|
}
|
|
@Override // org.springblade.core.redis.lock.RedisLockClient
|
public boolean tryLock(String lockName, LockType lockType, long waitTime, long leaseTime, TimeUnit timeUnit) throws InterruptedException {
|
RLock lock = getLock(lockName, lockType);
|
return lock.tryLock(waitTime, leaseTime, timeUnit);
|
}
|
|
@Override // org.springblade.core.redis.lock.RedisLockClient
|
public void unLock(String lockName, LockType lockType) {
|
RLock lock = getLock(lockName, lockType);
|
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
|
lock.unlock();
|
}
|
}
|
|
private RLock getLock(String lockName, LockType lockType) {
|
RLock lock;
|
if (LockType.REENTRANT == lockType) {
|
lock = this.redissonClient.getLock(lockName);
|
} else {
|
lock = this.redissonClient.getFairLock(lockName);
|
}
|
return lock;
|
}
|
|
@Override // org.springblade.core.redis.lock.RedisLockClient
|
public <T> T lock(String lockName, LockType lockType, long waitTime, long leaseTime, TimeUnit timeUnit, CheckedSupplier<T> supplier) {
|
|
try {
|
boolean result = tryLock(lockName, lockType, waitTime, leaseTime, timeUnit);
|
if (result)
|
return (T) supplier.get();
|
} catch (Throwable e) {
|
throw Exceptions.unchecked(e);
|
} finally {
|
unLock(lockName, lockType);
|
}
|
return null;
|
}
|
}
|