cn.structure.starter.redisson.utils.DistributedLockerImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of structure-redisson-starter Show documentation
Show all versions of structure-redisson-starter Show documentation
封装 redisson 将封装成启动器,并且对分布式锁和缓存进行封装
The newest version!
package cn.structure.starter.redisson.utils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import java.util.concurrent.TimeUnit;
/**
*
* 分布式锁实现
*
*
* @author chuck
* @version 1.0.1
* @since 2020-12-23
*/
public class DistributedLockerImpl implements IDistributedLocker {
private RedissonClient redissonClient;
public DistributedLockerImpl(RedissonClient redissonClient) {
this.redissonClient = redissonClient;
}
@Override
public RLock lock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock();
return lock;
}
@Override
public RLock lock(String lockKey, int leaseTime) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock(leaseTime, TimeUnit.SECONDS);
return lock;
}
@Override
public RLock lock(String lockKey, TimeUnit unit, int timeout) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock(timeout, unit);
return lock;
}
@Override
public boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
RLock lock = redissonClient.getLock(lockKey);
try {
return lock.tryLock(waitTime, leaseTime, unit);
} catch (InterruptedException e) {
return false;
}
}
@Override
public void unlock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
lock.unlock();
}
@Override
public void unlock(RLock lock) {
lock.unlock();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy