cl
3 天以前 2fa1c824c63048b49e45d867191ab3645aeaf3a4
rsf-server/src/main/java/com/vincent/rsf/server/system/service/impl/ConfigServiceImpl.java
@@ -12,6 +12,7 @@
import com.vincent.rsf.server.system.enums.StatusType;
import com.vincent.rsf.server.system.mapper.ConfigMapper;
import com.vincent.rsf.server.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@@ -24,15 +25,21 @@
 * Created by vincent on 8/30/2024
 */
@Service("configService")
@Slf4j
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService {
    public static final Map<String, Config> CONFIG_CACHE = new ConcurrentHashMap<>();
    @PostConstruct
    public void init() {
        List<Config> list = this.list(new LambdaQueryWrapper<Config>().eq(Config::getStatus, StatusType.ENABLE.val));
        for (Config config : list) {
            CONFIG_CACHE.put(config.getFlag(), config);
        try {
            // List<Config> list = this.list(new LambdaQueryWrapper<Config>().eq(Config::getStatus, StatusType.ENABLE.val));
            List<Config> list = safeList(new LambdaQueryWrapper<Config>().eq(Config::getStatus, StatusType.ENABLE.val));
            for (Config config : list) {
                CONFIG_CACHE.put(config.getFlag(), config);
            }
        } catch (Exception e) {
            log.warn("配置缓存初始化失败,跳过配置表加载,后续按默认流程处理", e);
        }
    }
@@ -41,7 +48,8 @@
    public <T> T getVal(String key, Class<T> clazz) {
        Config config = CONFIG_CACHE.get(key);
        if (config == null) {
            List<Config> list = this.list(new LambdaQueryWrapper<Config>().eq(Config::getFlag, key));
            // List<Config> list = this.list(new LambdaQueryWrapper<Config>().eq(Config::getFlag, key));
            List<Config> list = safeList(new LambdaQueryWrapper<Config>().eq(Config::getFlag, key));
            config = list.stream().findFirst().orElse(null);
            if (null == config) {
                return null;
@@ -80,7 +88,8 @@
    public <T> boolean setVal(String key, T val) {
        Config config = CONFIG_CACHE.get(key);
        if (config == null) {
            List<Config> list = this.list(new LambdaQueryWrapper<Config>().eq(Config::getFlag, key));
            // List<Config> list = this.list(new LambdaQueryWrapper<Config>().eq(Config::getFlag, key));
            List<Config> list = safeList(new LambdaQueryWrapper<Config>().eq(Config::getFlag, key));
            config = list.stream().findFirst().orElse(null);
            if (null == config) {
                return false;
@@ -124,6 +133,15 @@
        return this.updateById(config);
    }
    private List<Config> safeList(LambdaQueryWrapper<Config> wrapper) {
        try {
            return this.list(wrapper);
        } catch (Exception e) {
            log.warn("配置查询失败,跳过配置表读取,按默认流程处理", e);
            return java.util.Collections.emptyList();
        }
    }
    /**
     * 修改配置
     * @param config