update redis锁工具类编写

master
sxq 5 years ago committed by 疯狂的狮子li
parent 00d024ff86
commit f45b50a796

@ -0,0 +1,132 @@
package com.ruoyi.common.core.redis;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import org.redisson.api.RCountDownLatch;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* redis
*
* @author shenxinquan
*/
@Component
public class RedisLockManager {
@Autowired
private RedissonClient redissonClient;
private final ThreadLocal<RLock> threadLocal = new ThreadLocal<>();
/**
*
*/
public boolean getLock(String key) {
Assert.isTrue(StrUtil.isNotBlank(key), "key不能为空");
RLock lock = redissonClient.getLock(key);
threadLocal.set(lock);
return lock.tryLock();
}
/**
*
*
* @param key
* @param time
* @param expireUnit
*/
public boolean getLock(String key, long time, TimeUnit expireUnit) throws InterruptedException {
Assert.isTrue(StrUtil.isNotBlank(key), "key不能为空");
Assert.isTrue(time > 0, "过期时间必须大于0");
Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空");
RLock lock = redissonClient.getLock(key);
threadLocal.set(lock);
return lock.tryLock(time, expireUnit);
}
/**
*
*
* @param key
* @param waitTime
* @param leaseTime
* @param expireUnit
* @throws InterruptedException
*/
public boolean getLock(String key, long waitTime, long leaseTime, TimeUnit expireUnit) throws InterruptedException {
Assert.isTrue(StrUtil.isNotBlank(key), "key不能为空");
Assert.isTrue(waitTime > 0, "获取锁等待时间必须大于0");
Assert.isTrue(leaseTime > 0, "保留锁的时间必须大于0");
Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空");
RLock lock = redissonClient.getLock(key);
threadLocal.set(lock);
return lock.tryLock(waitTime, leaseTime, expireUnit);
}
/**
*
*
* @param key
* @param count countDownLatch
*/
public RCountDownLatch countDownLatch(String key, long count) {
Assert.isTrue(StrUtil.isNotBlank(key), "key不能为空");
Assert.isTrue(count >= 0, "count数量必须大于等于0");
RCountDownLatch rCountDownLatch = redissonClient.getCountDownLatch(key);
rCountDownLatch.trySetCount(count);
return rCountDownLatch;
}
/**
*
*
* @param key
* @param waitTime
* @param leaseTime
* @param expireUnit
* @return
* @throws InterruptedException
*/
public boolean getFairLock(String key, long waitTime, long leaseTime, TimeUnit expireUnit) throws InterruptedException {
Assert.isTrue(StrUtil.isNotBlank(key), "key不能为空");
Assert.isTrue(waitTime > 0, "获取锁等待时间必须大于0");
Assert.isTrue(leaseTime > 0, "保留锁的时间必须大于0");
Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空");
RLock lock = redissonClient.getFairLock(key);
threadLocal.set(lock);
return lock.tryLock(waitTime, leaseTime, expireUnit);
}
/**
*
*
* @param key
* @param leaseTime
* @param expireUnit
* @return
* @throws InterruptedException
*/
public boolean getFairLock(String key, long leaseTime, TimeUnit expireUnit) throws InterruptedException {
Assert.isTrue(StrUtil.isNotBlank(key), "key不能为空");
Assert.isTrue(leaseTime > 0, "保留锁的时间必须大于0");
Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空");
RLock lock = redissonClient.getFairLock(key);
threadLocal.set(lock);
return lock.tryLock(leaseTime, expireUnit);
}
/**
* ()
*/
public void unLock() {
RLock lock = threadLocal.get();
lock.unlock();
threadLocal.remove();
}
}
Loading…
Cancel
Save