From 94797a2d2b815fe7a4e8d884059a34e1d9bc74f3 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期一, 27 五月 2024 15:14:16 +0800
Subject: [PATCH] fix 修复 oss未使用租户 拼接租户id null问题

---
 ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/factory/OssFactory.java |   32 +++++++++++++++++++++-----------
 1 files changed, 21 insertions(+), 11 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 084d89a..3da1ba5 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
@@ -23,6 +24,7 @@
 public class OssFactory {
 
     private static final Map<String, OssClient> CLIENT_CACHE = new ConcurrentHashMap<>();
+    private static final ReentrantLock LOCK = new ReentrantLock();
 
     /**
      * 鑾峰彇榛樿瀹炰緥
@@ -45,17 +47,25 @@
             throw new OssException("绯荤粺寮傚父, '" + configKey + "'閰嶇疆淇℃伅涓嶅瓨鍦�!");
         }
         OssProperties properties = JsonUtils.parseObject(json, OssProperties.class);
-        OssClient client = CLIENT_CACHE.get(configKey);
-        if (client == null) {
-            CLIENT_CACHE.put(configKey, new OssClient(configKey, properties));
-            log.info("鍒涘缓OSS瀹炰緥 key => {}", configKey);
-            return CLIENT_CACHE.get(configKey);
+        // 浣跨敤绉熸埛鏍囪瘑閬垮厤澶氫釜绉熸埛鐩稿悓key瀹炰緥瑕嗙洊
+        String key = configKey;
+        if (StringUtils.isNotBlank(properties.getTenantId())) {
+            key = properties.getTenantId() + ":" + configKey;
         }
-        // 閰嶇疆涓嶇浉鍚屽垯閲嶆柊鏋勫缓
-        if (!client.checkPropertiesSame(properties)) {
-            CLIENT_CACHE.put(configKey, new OssClient(configKey, properties));
-            log.info("閲嶈浇OSS瀹炰緥 key => {}", configKey);
-            return CLIENT_CACHE.get(configKey);
+        OssClient client = 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;
     }

--
Gitblit v1.9.3