From 620447287b200285891640b8ac8e47915a80d206 Mon Sep 17 00:00:00 2001 From: Sergei Tsvetkov Date: Wed, 29 May 2024 09:48:22 +0300 Subject: [PATCH] feat: caching optimization Add kind constants to Plugin and ExtensionPointDefinition classes --- .../run/halo/app/core/extension/Plugin.java | 4 +++- .../halo/app/cache/CacheConditionProvider.java | 17 ++++++++--------- .../extension/service/DefaultRoleService.java | 2 +- .../ExtensionPointDefinition.java | 4 +++- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/api/src/main/java/run/halo/app/core/extension/Plugin.java b/api/src/main/java/run/halo/app/core/extension/Plugin.java index 8d1db1b76..9e0412e64 100644 --- a/api/src/main/java/run/halo/app/core/extension/Plugin.java +++ b/api/src/main/java/run/halo/app/core/extension/Plugin.java @@ -1,6 +1,7 @@ package run.halo.app.core.extension; import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; +import static run.halo.app.core.extension.Plugin.KIND; import com.fasterxml.jackson.annotation.JsonIgnore; import io.swagger.v3.oas.annotations.media.Schema; @@ -27,10 +28,11 @@ import run.halo.app.infra.ConditionList; */ @Data @ToString(callSuper = true) -@GVK(group = "plugin.halo.run", version = "v1alpha1", kind = "Plugin", plural = "plugins", +@GVK(group = "plugin.halo.run", version = "v1alpha1", kind = KIND, plural = "plugins", singular = "plugin") @EqualsAndHashCode(callSuper = true) public class Plugin extends AbstractExtension { + public static final String KIND = "Plugin"; @Schema(requiredMode = REQUIRED) private PluginSpec spec; diff --git a/application/src/main/java/run/halo/app/cache/CacheConditionProvider.java b/application/src/main/java/run/halo/app/cache/CacheConditionProvider.java index fd51a2f27..b2e6d9f52 100644 --- a/application/src/main/java/run/halo/app/cache/CacheConditionProvider.java +++ b/application/src/main/java/run/halo/app/cache/CacheConditionProvider.java @@ -5,8 +5,11 @@ import lombok.RequiredArgsConstructor; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; +import run.halo.app.core.extension.Plugin; +import run.halo.app.core.extension.Role; import run.halo.app.extension.ReactiveExtensionClientImpl; import run.halo.app.infra.properties.HaloProperties; +import run.halo.app.plugin.extensionpoint.ExtensionPointDefinition; /** * Provides methods to determine if specific types of caches are evictable or enabled. @@ -22,10 +25,6 @@ import run.halo.app.infra.properties.HaloProperties; @Component @RequiredArgsConstructor public class CacheConditionProvider { - private static final String ROLE_KIND = "Role"; - private static final String PLUGIN_KIND = "Plugin"; - private static final String EXTENSION_POINT_DEFINITION_KIND = "ExtensionPointDefinition"; - private final HaloProperties properties; /** @@ -35,7 +34,7 @@ public class CacheConditionProvider { * @return {@code true} if the role dependencies cache is evictable, {@code false} otherwise. */ public boolean isRoleDependenciesCacheEvictableByKind(String kind) { - return isRoleCacheEnabled() && Objects.equals(kind, ROLE_KIND); + return isRoleDependenciesCacheEnabled() && Objects.equals(kind, Role.KIND); } /** @@ -45,7 +44,7 @@ public class CacheConditionProvider { * @return {@code true} if the plugin extension cache is evictable, {@code false} otherwise. */ public boolean isPluginExtensionCacheEvictableByKind(String kind) { - return isPluginExtensionCacheEnabled() && Objects.equals(kind, PLUGIN_KIND); + return isPluginExtensionCacheEnabled() && Objects.equals(kind, Plugin.KIND); } /** @@ -56,8 +55,8 @@ public class CacheConditionProvider { * otherwise. */ public boolean isExtensionPointDefinitionCacheEvictableByKind(String kind) { - return isExtensionPointDefinitionCacheEnabled() && Objects.equals(kind, - EXTENSION_POINT_DEFINITION_KIND); + return isExtensionPointDefinitionCacheEnabled() + && Objects.equals(kind, ExtensionPointDefinition.KIND); } /** @@ -65,7 +64,7 @@ public class CacheConditionProvider { * * @return {@code true} if the role dependencies cache is enabled, {@code false} otherwise */ - public boolean isRoleCacheEnabled() { + public boolean isRoleDependenciesCacheEnabled() { return isCacheEnabled("role-dependencies"); } diff --git a/application/src/main/java/run/halo/app/core/extension/service/DefaultRoleService.java b/application/src/main/java/run/halo/app/core/extension/service/DefaultRoleService.java index 949c89dc8..11ff64577 100644 --- a/application/src/main/java/run/halo/app/core/extension/service/DefaultRoleService.java +++ b/application/src/main/java/run/halo/app/core/extension/service/DefaultRoleService.java @@ -76,7 +76,7 @@ public class DefaultRoleService implements RoleService { @Override @Cacheable( value = CacheNames.ROLE_DEPENDENCIES, - condition = "@cacheConditionProvider.isRoleCacheEnabled()" + condition = "@cacheConditionProvider.isRoleDependenciesCacheEnabled()" ) public Flux listDependenciesFlux(Set names) { return listDependencies(names, shouldFilterHidden(false)); diff --git a/application/src/main/java/run/halo/app/plugin/extensionpoint/ExtensionPointDefinition.java b/application/src/main/java/run/halo/app/plugin/extensionpoint/ExtensionPointDefinition.java index 5b65032fa..57e398671 100644 --- a/application/src/main/java/run/halo/app/plugin/extensionpoint/ExtensionPointDefinition.java +++ b/application/src/main/java/run/halo/app/plugin/extensionpoint/ExtensionPointDefinition.java @@ -1,6 +1,7 @@ package run.halo.app.plugin.extensionpoint; import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; +import static run.halo.app.plugin.extensionpoint.ExtensionPointDefinition.KIND; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; @@ -22,9 +23,10 @@ import run.halo.app.extension.GVK; @ToString(callSuper = true) @EqualsAndHashCode(callSuper = true) @GVK(group = "plugin.halo.run", version = "v1alpha1", - kind = "ExtensionPointDefinition", singular = "extensionpointdefinition", + kind = KIND, singular = "extensionpointdefinition", plural = "extensionpointdefinitions") public class ExtensionPointDefinition extends AbstractExtension { + public static final String KIND = "ExtensionPointDefinition"; @Schema(requiredMode = REQUIRED) private ExtensionPointSpec spec;