From a0bed51d966ab5d161d3fdd5423ba84f59fb60ff Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期四, 13 一月 2022 09:58:16 +0800 Subject: [PATCH] update 格式化代码 统一间隔符 --- ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java | 83 +++++++++++++++++++++++++++++++---------- 1 files changed, 62 insertions(+), 21 deletions(-) diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java index b549d21..3652f64 100644 --- a/ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java +++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java @@ -1,10 +1,16 @@ package com.ruoyi.oss.factory; -import cn.hutool.core.lang.Assert; -import com.ruoyi.common.utils.spring.SpringUtils; -import com.ruoyi.oss.constant.CloudConstant; -import com.ruoyi.oss.service.ICloudStorageService; -import com.ruoyi.system.service.ISysConfigService; +import com.ruoyi.common.utils.JsonUtils; +import com.ruoyi.common.utils.redis.RedisUtils; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.reflect.ReflectUtils; +import com.ruoyi.oss.constant.OssConstant; +import com.ruoyi.oss.enumd.OssEnumd; +import com.ruoyi.oss.exception.OssException; +import com.ruoyi.oss.properties.OssProperties; +import com.ruoyi.oss.service.IOssStrategy; +import com.ruoyi.oss.service.abstractd.AbstractOssStrategy; +import lombok.extern.slf4j.Slf4j; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -14,27 +20,62 @@ * * @author Lion Li */ +@Slf4j public class OssFactory { - private static ISysConfigService sysConfigService; + /** + * 鏈嶅姟瀹炰緥缂撳瓨 + */ + private static final Map<String, IOssStrategy> SERVICES = new ConcurrentHashMap<>(); - static { - OssFactory.sysConfigService = SpringUtils.getBean(ISysConfigService.class); - } + /** + * 鍒濆鍖栧伐鍘� + */ + public static void init() { + log.info("鍒濆鍖朞SS宸ュ巶"); + RedisUtils.subscribe(OssConstant.CACHE_CONFIG_KEY, String.class, type -> { + // 娌℃湁鐨勫疄渚嬩笉澶勭悊 + if (SERVICES.containsKey(type)) { + refreshService(type); + log.info("璁㈤槄鍒锋柊OSS閰嶇疆 => " + type); + } + }); + } - private static final Map<String, ICloudStorageService> SERVICES = new ConcurrentHashMap<>(); + /** + * 鑾峰彇榛樿瀹炰緥 + */ + public static IOssStrategy instance() { + // 鑾峰彇redis 榛樿绫诲瀷 + String type = RedisUtils.getCacheObject(OssConstant.CACHE_CONFIG_KEY); + if (StringUtils.isEmpty(type)) { + throw new OssException("鏂囦欢瀛樺偍鏈嶅姟绫诲瀷鏃犳硶鎵惧埌!"); + } + return instance(type); + } - public static ICloudStorageService instance() { - String type = sysConfigService.selectConfigByKey(CloudConstant.CLOUD_STORAGE_CONFIG_KEY); - return SERVICES.get(type); - } + /** + * 鏍规嵁绫诲瀷鑾峰彇瀹炰緥 + */ + public static IOssStrategy instance(String type) { + IOssStrategy service = SERVICES.get(type); + if (service == null) { + refreshService(type); + service = SERVICES.get(type); + } + return service; + } - public static ICloudStorageService instance(String type) { - return SERVICES.get(type); - } + private static void refreshService(String type) { + Object json = RedisUtils.getCacheObject(OssConstant.SYS_OSS_KEY + type); + OssProperties properties = JsonUtils.parseObject(json.toString(), OssProperties.class); + if (properties == null) { + throw new OssException("绯荤粺寮傚父, '" + type + "'閰嶇疆淇℃伅涓嶅瓨鍦�!"); + } + // 鑾峰彇redis閰嶇疆淇℃伅 鍒涘缓瀵硅薄 骞剁紦瀛� + IOssStrategy service = (IOssStrategy) ReflectUtils.newInstance(OssEnumd.getServiceClass(type)); + ((AbstractOssStrategy)service).init(properties); + SERVICES.put(type, service); + } - public static void register(String type, ICloudStorageService iCloudStorageService) { - Assert.notNull(type, "type can't be null"); - SERVICES.put(type, iCloudStorageService); - } } -- Gitblit v1.9.3