From 3d722235c00039d4a551551320a1a6ad34540fb9 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Mon, 27 May 2024 17:27:11 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E9=85=8D=E7=BD=AE=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../community/config/SimpleConfigManager.java | 44 +++++++++++++------ .../configuration/CommonConfiguration.java | 6 ++- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/jetlinks-components/common-component/src/main/java/org/jetlinks/community/config/SimpleConfigManager.java b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/config/SimpleConfigManager.java index 64d79c6f..51510575 100644 --- a/jetlinks-components/common-component/src/main/java/org/jetlinks/community/config/SimpleConfigManager.java +++ b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/config/SimpleConfigManager.java @@ -3,6 +3,8 @@ package org.jetlinks.community.config; import lombok.AllArgsConstructor; import org.apache.commons.collections4.MapUtils; import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository; +import org.hswebframework.web.cache.ReactiveCache; +import org.hswebframework.web.cache.ReactiveCacheManager; import org.jetlinks.community.ValueObject; import org.jetlinks.community.config.entity.ConfigEntity; import reactor.core.publisher.Flux; @@ -18,6 +20,13 @@ public class SimpleConfigManager implements ConfigManager, ConfigScopeManager { private final ReactiveRepository repository; + private final ReactiveCache> cache; + + public SimpleConfigManager(ReactiveRepository repository, ReactiveCacheManager cacheManager) { + this.repository = repository; + this.cache = cacheManager.getCache("system-config"); + } + @Override public void addScope(ConfigScope scope, List properties) { @@ -55,20 +64,27 @@ public class SimpleConfigManager implements ConfigManager, ConfigScopeManager { .filter(def -> null != def.getDefaultValue()) .collectMap(ConfigPropertyDef::getKey, ConfigPropertyDef::getDefaultValue), //数据库配置的值 - repository - .createQuery() - .where(ConfigEntity::getScope, scope) - .fetch() - .filter(val -> MapUtils.isNotEmpty(val.getProperties())) - .>reduce(new LinkedHashMap<>(), (l, r) -> { - l.putAll(r.getProperties()); - return l; - }), + cache + .getMono(scope, () -> getPropertiesNow(scope)), (defaults, values) -> { - defaults.forEach(values::putIfAbsent); - return values; + Map properties = new HashMap<>(values); + defaults.forEach(properties::putIfAbsent); + return properties; } - ).map(ValueObject::of); + ) + .map(ValueObject::of); + } + + private Mono> getPropertiesNow(String scope) { + return repository + .createQuery() + .where(ConfigEntity::getScope, scope) + .fetch() + .filter(val -> MapUtils.isNotEmpty(val.getProperties())) + .reduce(new LinkedHashMap<>(), (l, r) -> { + l.putAll(r.getProperties()); + return l; + }); } @Override @@ -77,7 +93,9 @@ public class SimpleConfigManager implements ConfigManager, ConfigScopeManager { entity.setProperties(values); entity.setScope(scope); entity.getId(); - return repository.save(entity).then(); + return repository + .save(entity) + .then(cache.evict(scope)); } } diff --git a/jetlinks-components/common-component/src/main/java/org/jetlinks/community/configuration/CommonConfiguration.java b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/configuration/CommonConfiguration.java index 561bdfc5..adf69afe 100644 --- a/jetlinks-components/common-component/src/main/java/org/jetlinks/community/configuration/CommonConfiguration.java +++ b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/configuration/CommonConfiguration.java @@ -7,6 +7,7 @@ import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.beanutils.Converter; import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository; import org.hswebframework.web.api.crud.entity.EntityFactory; +import org.hswebframework.web.cache.ReactiveCacheManager; import org.jetlinks.community.Interval; import org.jetlinks.community.JvmErrorException; import org.jetlinks.community.config.ConfigManager; @@ -159,9 +160,10 @@ public class CommonConfiguration { @Bean public ConfigManager configManager(ObjectProvider configScopeCustomizers, - ReactiveRepository repository) { + ReactiveRepository repository, + ReactiveCacheManager cacheManager) { - SimpleConfigManager configManager = new SimpleConfigManager(repository); + SimpleConfigManager configManager = new SimpleConfigManager(repository,cacheManager); for (ConfigScopeCustomizer customizer : configScopeCustomizers) { customizer.custom(configManager); }