sxq
2021-06-08 f45b50a796e1db1238b1147f11c2f96c0d405e48
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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();
    }
}