sxq
2021-06-08 f45b50a796e1db1238b1147f11c2f96c0d405e48
update redis锁工具类编写
已添加1个文件
132 ■■■■■ 文件已修改
ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisLockManager.java 132 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisLockManager.java
¶Ô±ÈÐÂÎļþ
@@ -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();
    }
}