feat: caching optimization

Add kind constants to Plugin and ExtensionPointDefinition classes
pull/6009/head
Sergei Tsvetkov 2024-05-29 09:48:22 +03:00
parent ce44f57300
commit 620447287b
4 changed files with 15 additions and 12 deletions

View File

@ -1,6 +1,7 @@
package run.halo.app.core.extension; package run.halo.app.core.extension;
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; 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 com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
@ -27,10 +28,11 @@ import run.halo.app.infra.ConditionList;
*/ */
@Data @Data
@ToString(callSuper = true) @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") singular = "plugin")
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
public class Plugin extends AbstractExtension { public class Plugin extends AbstractExtension {
public static final String KIND = "Plugin";
@Schema(requiredMode = REQUIRED) @Schema(requiredMode = REQUIRED)
private PluginSpec spec; private PluginSpec spec;

View File

@ -5,8 +5,11 @@ import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component; 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.extension.ReactiveExtensionClientImpl;
import run.halo.app.infra.properties.HaloProperties; 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. * 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 @Component
@RequiredArgsConstructor @RequiredArgsConstructor
public class CacheConditionProvider { 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; private final HaloProperties properties;
/** /**
@ -35,7 +34,7 @@ public class CacheConditionProvider {
* @return {@code true} if the role dependencies cache is evictable, {@code false} otherwise. * @return {@code true} if the role dependencies cache is evictable, {@code false} otherwise.
*/ */
public boolean isRoleDependenciesCacheEvictableByKind(String kind) { 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. * @return {@code true} if the plugin extension cache is evictable, {@code false} otherwise.
*/ */
public boolean isPluginExtensionCacheEvictableByKind(String kind) { 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. * otherwise.
*/ */
public boolean isExtensionPointDefinitionCacheEvictableByKind(String kind) { public boolean isExtensionPointDefinitionCacheEvictableByKind(String kind) {
return isExtensionPointDefinitionCacheEnabled() && Objects.equals(kind, return isExtensionPointDefinitionCacheEnabled()
EXTENSION_POINT_DEFINITION_KIND); && 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 * @return {@code true} if the role dependencies cache is enabled, {@code false} otherwise
*/ */
public boolean isRoleCacheEnabled() { public boolean isRoleDependenciesCacheEnabled() {
return isCacheEnabled("role-dependencies"); return isCacheEnabled("role-dependencies");
} }

View File

@ -76,7 +76,7 @@ public class DefaultRoleService implements RoleService {
@Override @Override
@Cacheable( @Cacheable(
value = CacheNames.ROLE_DEPENDENCIES, value = CacheNames.ROLE_DEPENDENCIES,
condition = "@cacheConditionProvider.isRoleCacheEnabled()" condition = "@cacheConditionProvider.isRoleDependenciesCacheEnabled()"
) )
public Flux<Role> listDependenciesFlux(Set<String> names) { public Flux<Role> listDependenciesFlux(Set<String> names) {
return listDependencies(names, shouldFilterHidden(false)); return listDependencies(names, shouldFilterHidden(false));

View File

@ -1,6 +1,7 @@
package run.halo.app.plugin.extensionpoint; package run.halo.app.plugin.extensionpoint;
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; 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 io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
@ -22,9 +23,10 @@ import run.halo.app.extension.GVK;
@ToString(callSuper = true) @ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@GVK(group = "plugin.halo.run", version = "v1alpha1", @GVK(group = "plugin.halo.run", version = "v1alpha1",
kind = "ExtensionPointDefinition", singular = "extensionpointdefinition", kind = KIND, singular = "extensionpointdefinition",
plural = "extensionpointdefinitions") plural = "extensionpointdefinitions")
public class ExtensionPointDefinition extends AbstractExtension { public class ExtensionPointDefinition extends AbstractExtension {
public static final String KIND = "ExtensionPointDefinition";
@Schema(requiredMode = REQUIRED) @Schema(requiredMode = REQUIRED)
private ExtensionPointSpec spec; private ExtensionPointSpec spec;