From 259c8c8fdb7a8557d7ecf77f9f75f67eb6020cb2 Mon Sep 17 00:00:00 2001 From: guqing Date: Thu, 27 Jun 2024 16:21:17 +0800 Subject: [PATCH] refactor: optimize the way to obtain plugin configurations --- .../comment/widget/DefaultCommentWidget.java | 26 +------ .../comment/widget/SettingConfigGetter.java | 33 +++++++++ .../widget/SettingConfigGetterImpl.java | 68 +++---------------- src/main/resources/plugin.yaml | 2 +- 4 files changed, 47 insertions(+), 82 deletions(-) diff --git a/src/main/java/run/halo/comment/widget/DefaultCommentWidget.java b/src/main/java/run/halo/comment/widget/DefaultCommentWidget.java index baeb9f5..d12d66e 100644 --- a/src/main/java/run/halo/comment/widget/DefaultCommentWidget.java +++ b/src/main/java/run/halo/comment/widget/DefaultCommentWidget.java @@ -1,7 +1,6 @@ package run.halo.comment.widget; import java.util.Properties; -import lombok.Data; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -29,7 +28,6 @@ public class DefaultCommentWidget implements CommentWidget { static final PropertyPlaceholderHelper PROPERTY_PLACEHOLDER_HELPER = new PropertyPlaceholderHelper("${", "}"); private final PluginWrapper pluginWrapper; - private final SettingFetcher settingFetcher; private final SettingConfigGetter settingConfigGetter; @Override @@ -65,15 +63,13 @@ private String commentHtml(IAttribute groupAttribute, IAttribute kindAttribute, properties.setProperty("name", nameAttribute.getValue()); properties.setProperty("domId", domIdFrom(group, kindAttribute.getValue(), nameAttribute.getValue())); - var basicConfig = settingFetcher.fetch(BasicConfig.GROUP, BasicConfig.class) - .orElse(new BasicConfig()); + var basicConfig = settingConfigGetter.getBasicConfig().blockOptional().orElseThrow(); properties.setProperty("size", String.valueOf(basicConfig.getSize())); properties.setProperty("replySize", String.valueOf(basicConfig.getReplySize())); properties.setProperty("withReplies", String.valueOf(basicConfig.isWithReplies())); properties.setProperty("withReplySize", String.valueOf(basicConfig.getWithReplySize())); - var avatarConfig = settingFetcher.fetch(AvatarConfig.GROUP, AvatarConfig.class) - .orElse(new AvatarConfig()); + var avatarConfig = settingConfigGetter.getAvatarConfig().blockOptional().orElseThrow(); properties.setProperty("useAvatarProvider", String.valueOf(avatarConfig.isEnable())); properties.setProperty("avatarProvider", String.valueOf(avatarConfig.getProvider())); properties.setProperty("avatarProviderMirror", String.valueOf(avatarConfig.getProviderMirror())); @@ -111,24 +107,6 @@ private String commentHtml(IAttribute groupAttribute, IAttribute kindAttribute, """, properties); } - @Data - private static class BasicConfig { - public static final String GROUP = "basic"; - private int size; - private int replySize; - private boolean withReplies; - private int withReplySize; - } - - @Data - private static class AvatarConfig { - public static final String GROUP = "avatar"; - private boolean enable; - private String provider; - private String providerMirror; - private String policy; - } - private String domIdFrom(String group, String kind, String name) { Assert.notNull(name, "The name must not be null."); Assert.notNull(kind, "The kind must not be null."); diff --git a/src/main/java/run/halo/comment/widget/SettingConfigGetter.java b/src/main/java/run/halo/comment/widget/SettingConfigGetter.java index 1b20d3f..eacd827 100644 --- a/src/main/java/run/halo/comment/widget/SettingConfigGetter.java +++ b/src/main/java/run/halo/comment/widget/SettingConfigGetter.java @@ -9,11 +9,26 @@ public interface SettingConfigGetter { + /** + * Never {@link Mono#empty()}. + */ + Mono getBasicConfig(); + + /** + * Never {@link Mono#empty()}. + */ + Mono getAvatarConfig(); + + /** + * Never {@link Mono#empty()}. + */ Mono getSecurityConfig(); @Data @Accessors(chain = true) class SecurityConfig { + public static final String GROUP = "security"; + @Getter(onMethod_ = @NonNull) private CaptchaConfig captcha; @@ -46,4 +61,22 @@ public static CaptchaConfig empty() { return new CaptchaConfig(); } } + + @Data + class BasicConfig { + public static final String GROUP = "basic"; + private int size; + private int replySize; + private boolean withReplies; + private int withReplySize; + } + + @Data + class AvatarConfig { + public static final String GROUP = "avatar"; + private boolean enable; + private String provider; + private String providerMirror; + private String policy; + } } diff --git a/src/main/java/run/halo/comment/widget/SettingConfigGetterImpl.java b/src/main/java/run/halo/comment/widget/SettingConfigGetterImpl.java index 07e8113..6b4fad3 100644 --- a/src/main/java/run/halo/comment/widget/SettingConfigGetterImpl.java +++ b/src/main/java/run/halo/comment/widget/SettingConfigGetterImpl.java @@ -1,76 +1,30 @@ package run.halo.comment.widget; -import static run.halo.app.extension.index.query.QueryFactory.equal; - -import java.util.function.Function; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; import reactor.core.publisher.Mono; -import run.halo.app.extension.ConfigMap; -import run.halo.app.extension.DefaultExtensionMatcher; -import run.halo.app.extension.ExtensionClient; -import run.halo.app.extension.controller.Controller; -import run.halo.app.extension.controller.ControllerBuilder; -import run.halo.app.extension.controller.Reconciler; -import run.halo.app.extension.router.selector.FieldSelector; import run.halo.app.plugin.ReactiveSettingFetcher; @Component @RequiredArgsConstructor public class SettingConfigGetterImpl implements SettingConfigGetter { private final ReactiveSettingFetcher settingFetcher; - private final SettingConfigCache settingConfigCache; @Override - public Mono getSecurityConfig() { - return settingConfigCache.get("security", - key -> settingFetcher.fetch("security", SecurityConfig.class) - .defaultIfEmpty(SecurityConfig.empty()) - ); + public Mono getBasicConfig() { + return settingFetcher.fetch(BasicConfig.GROUP, BasicConfig.class) + .defaultIfEmpty(new BasicConfig()); } - interface SettingConfigCache { - Mono get(String key, Function> loader); + @Override + public Mono getAvatarConfig() { + return settingFetcher.fetch(AvatarConfig.GROUP, AvatarConfig.class) + .defaultIfEmpty(new AvatarConfig()); } - @Component - @RequiredArgsConstructor - static class SettingConfigCacheImpl implements Reconciler, SettingConfigCache { - private static final String CONFIG_NAME = "plugin-comment-widget-configmap"; - - private final Cache cache = CacheBuilder.newBuilder() - .maximumSize(10) - .build(); - - private final ExtensionClient client; - - @SuppressWarnings("unchecked") - public Mono get(String key, Function> loader) { - return Mono.justOrEmpty(cache.getIfPresent(key)) - .switchIfEmpty(loader.apply(key).doOnNext(value -> cache.put(key, value))) - .map(object -> (T) object); - } - - @Override - public Result reconcile(Request request) { - cache.invalidateAll(); - return Result.doNotRetry(); - } - - @Override - public Controller setupWith(ControllerBuilder builder) { - var extension = new ConfigMap(); - var extensionMatcher = DefaultExtensionMatcher.builder(client, extension.groupVersionKind()) - .fieldSelector(FieldSelector.of(equal("metadata.name", CONFIG_NAME))) - .build(); - return builder - .extension(extension) - .syncAllOnStart(false) - .onAddMatcher(extensionMatcher) - .onUpdateMatcher(extensionMatcher) - .build(); - } + @Override + public Mono getSecurityConfig() { + return settingFetcher.fetch(SecurityConfig.GROUP, SecurityConfig.class) + .defaultIfEmpty(SecurityConfig.empty()); } } diff --git a/src/main/resources/plugin.yaml b/src/main/resources/plugin.yaml index 6de8594..ea86813 100644 --- a/src/main/resources/plugin.yaml +++ b/src/main/resources/plugin.yaml @@ -8,7 +8,7 @@ metadata: "store.halo.run/app-id": "app-YXyaD" spec: enabled: true - requires: ">=2.15.0" + requires: ">=2.17.0" author: name: Halo website: https://github.com/halo-dev