!485 优化 OssFactory 获取实例锁性能

Merge pull request !485 from fanc/dev
This commit is contained in:
疯狂的狮子Li 2024-01-25 07:00:52 +00:00 committed by Gitee
commit 4273f2db34
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -25,6 +25,8 @@ public class OssFactory {
private static final Map<String, OssClient> CLIENT_CACHE = new ConcurrentHashMap<>(); private static final Map<String, OssClient> CLIENT_CACHE = new ConcurrentHashMap<>();
private static final ReentrantLock lock = new ReentrantLock();
/** /**
* 获取默认实例 * 获取默认实例
*/ */
@ -51,12 +53,14 @@ public class OssFactory {
OssClient client = CLIENT_CACHE.get(key); OssClient client = CLIENT_CACHE.get(key);
// 客户端不存在或配置不相同则重新构建 // 客户端不存在或配置不相同则重新构建
if (client == null || !client.checkPropertiesSame(properties)) { if (client == null || !client.checkPropertiesSame(properties)) {
ReentrantLock lock = new ReentrantLock();
lock.lock(); lock.lock();
try { try {
client = CLIENT_CACHE.get(key);
if (client == null || !client.checkPropertiesSame(properties)) {
CLIENT_CACHE.put(key, new OssClient(configKey, properties)); CLIENT_CACHE.put(key, new OssClient(configKey, properties));
log.info("创建OSS实例 key => {}", configKey); log.info("创建OSS实例 key => {}", configKey);
return CLIENT_CACHE.get(key); return CLIENT_CACHE.get(key);
}
} finally { } finally {
lock.unlock(); lock.unlock();
} }