疯狂的狮子li
2021-06-12 5311937b6987a10087269ad0c82bc92f9d1f2a44
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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 static Integer BASE_LOCK = 1;
 
    /**
     * 公平锁
     */
    private final static Integer FAIR_LOCK = 2;
 
    /**
     * 存放当前线程获取锁的类型
     */
    private final ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
 
    /**
     * 获取锁
     */
    private RLock getLock(String key, Integer lockType) {
        Assert.isTrue(StrUtil.isNotBlank(key), "key不能为空");
        threadLocal.set(lockType);
        RLock lock;
        if (BASE_LOCK.equals(lockType)) {
            lock = redissonClient.getLock(key);
        } else if (FAIR_LOCK.equals(lockType)) {
            lock = redissonClient.getFairLock(key);
        } else {
            throw new RuntimeException("锁不存在!");
        }
        return lock;
    }
 
    /**
     * 获取锁(不用设置超时时间,一直等待)
     */
    public boolean getLock(String key) {
        RLock lock = getLock(key, BASE_LOCK);
        return lock.tryLock();
    }
 
    /**
     * 设置过期时间
     *
     * @param key
     * @param time       过期时间
     * @param expireUnit 时间单位
     */
    public boolean getLock(String key, long time, TimeUnit expireUnit) {
        Assert.isTrue(time > 0, "过期时间必须大于0");
        Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空");
        RLock lock = getLock(key, BASE_LOCK);
        try {
            return lock.tryLock(time, expireUnit);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    }
 
    /**
     * 设置过期时间
     *
     * @param key
     * @param waitTime   获取锁等待时间
     * @param leaseTime  保留锁的时间
     * @param expireUnit 时间单位
     */
    public boolean getLock(String key, long waitTime, long leaseTime, TimeUnit expireUnit) {
        Assert.isTrue(waitTime > 0, "获取锁等待时间必须大于0");
        Assert.isTrue(leaseTime > 0, "保留锁的时间必须大于0");
        Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空");
        RLock lock = getLock(key, BASE_LOCK);
        try {
            return lock.tryLock(waitTime, leaseTime, expireUnit);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    }
 
 
    /**
     * 获取计数器锁
     *
     * @param key
     * @param count countDownLatch 的数量
     */
    public RCountDownLatch getCountDownLatch(String key, long count) {
        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) {
        Assert.isTrue(waitTime > 0, "获取锁等待时间必须大于0");
        Assert.isTrue(leaseTime > 0, "保留锁的时间必须大于0");
        Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空");
        RLock lock = getLock(key, FAIR_LOCK);
        try {
            return lock.tryLock(waitTime, leaseTime, expireUnit);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    }
 
    /**
     * 获取公平锁
     *
     * @param key
     * @param leaseTime  持有锁的时间
     * @param expireUnit 时间单位
     */
    public boolean getFairLock(String key, long leaseTime, TimeUnit expireUnit) {
        Assert.isTrue(leaseTime > 0, "保留锁的时间必须大于0");
        Assert.isTrue(Validator.isNotEmpty(expireUnit), "时间单位不能为空");
        RLock lock = getLock(key, FAIR_LOCK);
        try {
            return lock.tryLock(leaseTime, expireUnit);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    }
 
    /**
     * 释放锁(统一释放)
     */
    public void unLock(String key) {
        Integer lockType = threadLocal.get();
        RLock lock = getLock(key, lockType);
        lock.unlock();
        threadLocal.remove();
    }
}