From 7c5898ddf6de0f20e78d2667d56e90197960dd83 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期四, 25 一月 2024 10:14:22 +0800
Subject: [PATCH] update 优化 OssFactory 获取实例锁性能
---
ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/factory/OssFactory.java | 26 ++++++++++++++------------
1 files changed, 14 insertions(+), 12 deletions(-)
diff --git a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/factory/OssFactory.java b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/factory/OssFactory.java
index 763b090..cbda62a 100644
--- a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/factory/OssFactory.java
+++ b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/factory/OssFactory.java
@@ -1,5 +1,6 @@
package org.dromara.common.oss.factory;
+import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.constant.CacheNames;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.json.utils.JsonUtils;
@@ -9,10 +10,10 @@
import org.dromara.common.oss.properties.OssProperties;
import org.dromara.common.redis.utils.CacheUtils;
import org.dromara.common.redis.utils.RedisUtils;
-import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReentrantLock;
/**
* 鏂囦欢涓婁紶Factory
@@ -39,7 +40,7 @@
/**
* 鏍规嵁绫诲瀷鑾峰彇瀹炰緥
*/
- public static synchronized OssClient instance(String configKey) {
+ public static OssClient instance(String configKey) {
String json = CacheUtils.get(CacheNames.SYS_OSS_CONFIG, configKey);
if (json == null) {
throw new OssException("绯荤粺寮傚父, '" + configKey + "'閰嶇疆淇℃伅涓嶅瓨鍦�!");
@@ -48,16 +49,17 @@
// 浣跨敤绉熸埛鏍囪瘑閬垮厤澶氫釜绉熸埛鐩稿悓key瀹炰緥瑕嗙洊
String key = properties.getTenantId() + ":" + configKey;
OssClient client = CLIENT_CACHE.get(key);
- if (client == null) {
- CLIENT_CACHE.put(key, new OssClient(configKey, properties));
- log.info("鍒涘缓OSS瀹炰緥 key => {}", configKey);
- return CLIENT_CACHE.get(key);
- }
- // 閰嶇疆涓嶇浉鍚屽垯閲嶆柊鏋勫缓
- if (!client.checkPropertiesSame(properties)) {
- CLIENT_CACHE.put(key, new OssClient(configKey, properties));
- log.info("閲嶈浇OSS瀹炰緥 key => {}", configKey);
- return CLIENT_CACHE.get(key);
+ // 瀹㈡埛绔笉瀛樺湪鎴栭厤缃笉鐩稿悓鍒欓噸鏂版瀯寤�
+ if (client == null || !client.checkPropertiesSame(properties)) {
+ ReentrantLock lock = new ReentrantLock();
+ lock.lock();
+ try {
+ CLIENT_CACHE.put(key, new OssClient(configKey, properties));
+ log.info("鍒涘缓OSS瀹炰緥 key => {}", configKey);
+ return CLIENT_CACHE.get(key);
+ } finally {
+ lock.unlock();
+ }
}
return client;
}
--
Gitblit v1.9.3