From 62c8e1c877ebbfe242eac94d313b04513b96d626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=B0=8F=E7=8E=8B=E5=AD=90?= Date: Fri, 1 Mar 2024 15:29:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20OssFactory=EF=BC=8C?= =?UTF-8?q?=E9=87=87=E7=94=A8=E5=8F=8C=E9=87=8D=E6=A0=A1=E9=AA=8C=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/common/oss/factory/OssFactory.java | 27 +++++++++++-------- .../common/tenant/handle/MyTenantFactory.java | 2 ++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/ruoyi-common/ruoyi-common-oss/src/main/java/com/ruoyi/common/oss/factory/OssFactory.java b/ruoyi-common/ruoyi-common-oss/src/main/java/com/ruoyi/common/oss/factory/OssFactory.java index 4f94e1f..a986466 100644 --- a/ruoyi-common/ruoyi-common-oss/src/main/java/com/ruoyi/common/oss/factory/OssFactory.java +++ b/ruoyi-common/ruoyi-common-oss/src/main/java/com/ruoyi/common/oss/factory/OssFactory.java @@ -13,6 +13,7 @@ import lombok.extern.slf4j.Slf4j; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantLock; /** * 文件上传Factory @@ -23,6 +24,7 @@ import java.util.concurrent.ConcurrentHashMap; public class OssFactory { private static final Map CLIENT_CACHE = new ConcurrentHashMap<>(); + private static final ReentrantLock LOCK = new ReentrantLock(); /** * 获取默认实例 @@ -39,7 +41,7 @@ public class OssFactory { /** * 根据类型获取实例 */ - 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 +50,19 @@ public class OssFactory { // 使用租户标识避免多个租户相同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)) { + LOCK.lock(); + try { + client = CLIENT_CACHE.get(key); + if (client == null || !client.checkPropertiesSame(properties)) { + CLIENT_CACHE.put(key, new OssClient(configKey, properties)); + log.info("创建OSS实例 key => {}", configKey); + return CLIENT_CACHE.get(key); + } + } finally { + LOCK.unlock(); + } } return client; } diff --git a/ruoyi-common/ruoyi-common-tenant/src/main/java/com/ruoyi/common/tenant/handle/MyTenantFactory.java b/ruoyi-common/ruoyi-common-tenant/src/main/java/com/ruoyi/common/tenant/handle/MyTenantFactory.java index 101a0fb..6762cd2 100644 --- a/ruoyi-common/ruoyi-common-tenant/src/main/java/com/ruoyi/common/tenant/handle/MyTenantFactory.java +++ b/ruoyi-common/ruoyi-common-tenant/src/main/java/com/ruoyi/common/tenant/handle/MyTenantFactory.java @@ -5,12 +5,14 @@ import com.mybatisflex.core.tenant.TenantFactory; import com.ruoyi.common.security.utils.LoginHelper; import com.ruoyi.common.tenant.helper.TenantHelper; import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; /** * 自定义租户工厂 * * @author 数据小王子 */ +@Slf4j @AllArgsConstructor public class MyTenantFactory implements TenantFactory {