forked from halo-dev/plugin-comment-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: optimize the way to obtain plugin configurations (halo-dev#135
) ### What this PR does? /kind improvement 优化插件配置的获取方式 2.17.0 已经对配置获取增加了缓存因此不必在需要插件手动处理 ```release-note None ```
- Loading branch information
Showing
4 changed files
with
47 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 11 additions & 57 deletions
68
src/main/java/run/halo/comment/widget/SettingConfigGetterImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SecurityConfig> getSecurityConfig() { | ||
return settingConfigCache.get("security", | ||
key -> settingFetcher.fetch("security", SecurityConfig.class) | ||
.defaultIfEmpty(SecurityConfig.empty()) | ||
); | ||
public Mono<BasicConfig> getBasicConfig() { | ||
return settingFetcher.fetch(BasicConfig.GROUP, BasicConfig.class) | ||
.defaultIfEmpty(new BasicConfig()); | ||
} | ||
|
||
interface SettingConfigCache { | ||
<T> Mono<T> get(String key, Function<String, Mono<T>> loader); | ||
@Override | ||
public Mono<AvatarConfig> getAvatarConfig() { | ||
return settingFetcher.fetch(AvatarConfig.GROUP, AvatarConfig.class) | ||
.defaultIfEmpty(new AvatarConfig()); | ||
} | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
static class SettingConfigCacheImpl implements Reconciler<Reconciler.Request>, SettingConfigCache { | ||
private static final String CONFIG_NAME = "plugin-comment-widget-configmap"; | ||
|
||
private final Cache<String, Object> cache = CacheBuilder.newBuilder() | ||
.maximumSize(10) | ||
.build(); | ||
|
||
private final ExtensionClient client; | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T> Mono<T> get(String key, Function<String, Mono<T>> 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<SecurityConfig> getSecurityConfig() { | ||
return settingFetcher.fetch(SecurityConfig.GROUP, SecurityConfig.class) | ||
.defaultIfEmpty(SecurityConfig.empty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters