Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
kyouji committed May 30, 2024
2 parents 63ed9d7 + 3d72223 commit 3a6824f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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;
Expand All @@ -18,6 +20,13 @@ public class SimpleConfigManager implements ConfigManager, ConfigScopeManager {

private final ReactiveRepository<ConfigEntity, String> repository;

private final ReactiveCache<Map<String, Object>> cache;

public SimpleConfigManager(ReactiveRepository<ConfigEntity, String> repository, ReactiveCacheManager cacheManager) {
this.repository = repository;
this.cache = cacheManager.getCache("system-config");
}

@Override
public void addScope(ConfigScope scope,
List<ConfigPropertyDef> properties) {
Expand Down Expand Up @@ -55,20 +64,27 @@ public Mono<ValueObject> getProperties(String scope) {
.filter(def -> null != def.getDefaultValue())
.collectMap(ConfigPropertyDef::getKey, ConfigPropertyDef::getDefaultValue),
//数据库配置的值
repository
.createQuery()
.where(ConfigEntity::getScope, scope)
.fetch()
.filter(val -> MapUtils.isNotEmpty(val.getProperties()))
.<Map<String, Object>>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<String, Object> properties = new HashMap<>(values);
defaults.forEach(properties::putIfAbsent);
return properties;
}
).map(ValueObject::of);
)
.map(ValueObject::of);
}

private Mono<Map<String, Object>> 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
Expand All @@ -77,7 +93,9 @@ public Mono<Void> setProperties(String scope, Map<String, Object> values) {
entity.setProperties(values);
entity.setScope(scope);
entity.getId();
return repository.save(entity).then();
return repository
.save(entity)
.then(cache.evict(scope));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand Down Expand Up @@ -159,9 +160,10 @@ public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomiz

@Bean
public ConfigManager configManager(ObjectProvider<ConfigScopeCustomizer> configScopeCustomizers,
ReactiveRepository<ConfigEntity, String> repository) {
ReactiveRepository<ConfigEntity, String> repository,
ReactiveCacheManager cacheManager) {

SimpleConfigManager configManager = new SimpleConfigManager(repository);
SimpleConfigManager configManager = new SimpleConfigManager(repository,cacheManager);
for (ConfigScopeCustomizer customizer : configScopeCustomizers) {
customizer.custom(configManager);
}
Expand Down

0 comments on commit 3a6824f

Please sign in to comment.