¶Ô±ÈÐÂÎļþ |
| | |
| | | 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(); |
| | | } |
| | | } |