update 优化 OssFactory 锁逻辑

2.X
疯狂的狮子Li 1 year ago
parent d27fc8cc93
commit d77fec1537

@ -24,6 +24,7 @@ import java.util.concurrent.locks.ReentrantLock;
public class OssFactory { 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,14 +52,16 @@ 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_CACHE.put(key, new OssClient(configKey, properties)); client = CLIENT_CACHE.get(key);
log.info("创建OSS实例 key => {}", configKey); if (client == null || !client.checkPropertiesSame(properties)) {
return CLIENT_CACHE.get(key); CLIENT_CACHE.put(key, new OssClient(configKey, properties));
log.info("创建OSS实例 key => {}", configKey);
return CLIENT_CACHE.get(key);
}
} finally { } finally {
lock.unlock(); LOCK.unlock();
} }
} }
return client; return client;

Loading…
Cancel
Save