From 1bf9dd1217ad2d62cb6e23ea40e2a56395a9f2b6 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期四, 10 六月 2021 13:32:50 +0800 Subject: [PATCH] update 优化redis锁工具代码结构 --- ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisLockManager.java | 109 ++++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 77 insertions(+), 32 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisLockManager.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisLockManager.java index fb665a9..e00f800 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisLockManager.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisLockManager.java @@ -22,15 +22,50 @@ @Autowired private RedissonClient redissonClient; - private final ThreadLocal<RLock> threadLocal = new ThreadLocal<>(); + /** + * 閫氱敤閿� + */ + private final static Integer BASE_LOCK = 1; + + /** + * 鍏钩閿� + */ + private final static Integer FAIR_LOCK = 2; + + /** + * 璁℃暟閿� + */ + private final static Integer COUNT_LOCK = 3; + + /** + * 瀛樻斁褰撳墠绾跨▼鑾峰彇閿佺殑绫诲瀷 + */ + private final ThreadLocal<Integer> threadLocal = new ThreadLocal<>(); + + /** + * 鑾峰彇閿� + */ + private <T> T getLock(String key, Integer lockType) { + Assert.isTrue(StrUtil.isNotBlank(key), "key涓嶈兘涓虹┖"); + threadLocal.set(lockType); + Object lock; + if (BASE_LOCK.equals(lockType)) { + lock = redissonClient.getLock(key); + } else if (FAIR_LOCK.equals(lockType)) { + lock = redissonClient.getFairLock(key); + } else if (COUNT_LOCK.equals(lockType)) { + lock = redissonClient.getCountDownLatch(key); + } else { + throw new RuntimeException("閿佷笉瀛樺湪!"); + } + return (T)lock; + } /** * 鑾峰彇閿侊紙涓嶇敤璁剧疆瓒呮椂鏃堕棿锛屼竴鐩寸瓑寰咃級 */ public boolean getLock(String key) { - Assert.isTrue(StrUtil.isNotBlank(key), "key涓嶈兘涓虹┖"); - RLock lock = redissonClient.getLock(key); - threadLocal.set(lock); + RLock lock = getLock(key, BASE_LOCK); return lock.tryLock(); } @@ -41,13 +76,16 @@ * @param time 杩囨湡鏃堕棿 * @param expireUnit 鏃堕棿鍗曚綅 */ - public boolean getLock(String key, long time, TimeUnit expireUnit) throws InterruptedException { - Assert.isTrue(StrUtil.isNotBlank(key), "key涓嶈兘涓虹┖"); + public boolean getLock(String key, long time, TimeUnit expireUnit) { Assert.isTrue(time > 0, "杩囨湡鏃堕棿蹇呴』澶т簬0"); Assert.isTrue(Validator.isNotEmpty(expireUnit), "鏃堕棿鍗曚綅涓嶈兘涓虹┖"); - RLock lock = redissonClient.getLock(key); - threadLocal.set(lock); - return lock.tryLock(time, expireUnit); + RLock lock = getLock(key, BASE_LOCK); + try { + return lock.tryLock(time, expireUnit); + } catch (InterruptedException e) { + e.printStackTrace(); + return false; + } } /** @@ -57,17 +95,20 @@ * @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涓嶈兘涓虹┖"); + 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 = redissonClient.getLock(key); - threadLocal.set(lock); - return lock.tryLock(waitTime, leaseTime, expireUnit); + RLock lock = getLock(key, BASE_LOCK); + try { + return lock.tryLock(waitTime, leaseTime, expireUnit); + } catch (InterruptedException e) { + e.printStackTrace(); + return false; + } } + /** * 鑾峰彇璁℃暟鍣ㄩ攣 @@ -75,10 +116,9 @@ * @param key * @param count countDownLatch 鐨勬暟閲� */ - public RCountDownLatch countDownLatch(String key, long count) { - Assert.isTrue(StrUtil.isNotBlank(key), "key涓嶈兘涓虹┖"); + public RCountDownLatch getCountDownLatch(String key, long count) { Assert.isTrue(count >= 0, "count鏁伴噺蹇呴』澶т簬绛変簬0"); - RCountDownLatch rCountDownLatch = redissonClient.getCountDownLatch(key); + RCountDownLatch rCountDownLatch = getLock(key, COUNT_LOCK); rCountDownLatch.trySetCount(count); return rCountDownLatch; } @@ -93,14 +133,17 @@ * @return * @throws InterruptedException */ - public boolean getFairLock(String key, long waitTime, long leaseTime, TimeUnit expireUnit) throws InterruptedException { - Assert.isTrue(StrUtil.isNotBlank(key), "key涓嶈兘涓虹┖"); + 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 = redissonClient.getFairLock(key); - threadLocal.set(lock); - return lock.tryLock(waitTime, leaseTime, expireUnit); + RLock lock = getLock(key, FAIR_LOCK); + try { + return lock.tryLock(waitTime, leaseTime, expireUnit); + } catch (InterruptedException e) { + e.printStackTrace(); + return false; + } } /** @@ -109,23 +152,25 @@ * @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涓嶈兘涓虹┖"); + public boolean getFairLock(String key, long leaseTime, TimeUnit expireUnit) { Assert.isTrue(leaseTime > 0, "淇濈暀閿佺殑鏃堕棿蹇呴』澶т簬0"); Assert.isTrue(Validator.isNotEmpty(expireUnit), "鏃堕棿鍗曚綅涓嶈兘涓虹┖"); - RLock lock = redissonClient.getFairLock(key); - threadLocal.set(lock); - return lock.tryLock(leaseTime, expireUnit); + RLock lock = getLock(key, FAIR_LOCK); + try { + return lock.tryLock(leaseTime, expireUnit); + } catch (InterruptedException e) { + e.printStackTrace(); + return false; + } } /** * 閲婃斁閿�(缁熶竴閲婃斁) */ - public void unLock() { - RLock lock = threadLocal.get(); + public void unLock(String key) { + Integer lockType = threadLocal.get(); + RLock lock = getLock(key, lockType); lock.unlock(); threadLocal.remove(); } -- Gitblit v1.9.3