diff --git a/extensions/cache/deployment-spi/src/main/java/io/quarkus/cache/deployment/spi/CacheTypeBuildItem.java b/extensions/cache/deployment-spi/src/main/java/io/quarkus/cache/deployment/spi/CacheTypeBuildItem.java new file mode 100644 index 0000000000000..7d3742e941220 --- /dev/null +++ b/extensions/cache/deployment-spi/src/main/java/io/quarkus/cache/deployment/spi/CacheTypeBuildItem.java @@ -0,0 +1,25 @@ +package io.quarkus.cache.deployment.spi; + +import io.quarkus.builder.item.SimpleBuildItem; + +/** + * A build item that can be used by extensions to determine what kind of cache backend is configured. + * This is useful for cases where caching extensions specific data does not make sense for remote cache backends + */ +public final class CacheTypeBuildItem extends SimpleBuildItem { + + private final Type type; + + public CacheTypeBuildItem(Type type) { + this.type = type; + } + + public Type getType() { + return type; + } + + public enum Type { + LOCAL, + REMOTE + } +} diff --git a/extensions/cache/deployment/src/main/java/io/quarkus/cache/deployment/CacheProcessor.java b/extensions/cache/deployment/src/main/java/io/quarkus/cache/deployment/CacheProcessor.java index 53502e4976002..bf7c8e0a8f47b 100644 --- a/extensions/cache/deployment/src/main/java/io/quarkus/cache/deployment/CacheProcessor.java +++ b/extensions/cache/deployment/src/main/java/io/quarkus/cache/deployment/CacheProcessor.java @@ -13,6 +13,7 @@ import static io.quarkus.cache.deployment.CacheDeploymentConstants.INTERCEPTOR_BINDING_CONTAINERS; import static io.quarkus.cache.deployment.CacheDeploymentConstants.MULTI; import static io.quarkus.cache.deployment.CacheDeploymentConstants.REGISTER_REST_CLIENT; +import static io.quarkus.cache.runtime.CacheBuildConfig.CAFFEINE_CACHE_TYPE; import static io.quarkus.deployment.annotations.ExecutionTime.RUNTIME_INIT; import static io.quarkus.runtime.metrics.MetricsFactory.MICROMETER; import static java.util.stream.Collectors.toList; @@ -54,6 +55,8 @@ import io.quarkus.cache.deployment.exception.VoidReturnTypeTargetException; import io.quarkus.cache.deployment.spi.AdditionalCacheNameBuildItem; import io.quarkus.cache.deployment.spi.CacheManagerInfoBuildItem; +import io.quarkus.cache.deployment.spi.CacheTypeBuildItem; +import io.quarkus.cache.runtime.CacheBuildConfig; import io.quarkus.cache.runtime.CacheInvalidateAllInterceptor; import io.quarkus.cache.runtime.CacheInvalidateInterceptor; import io.quarkus.cache.runtime.CacheManagerRecorder; @@ -92,6 +95,12 @@ RestClientAnnotationsTransformerBuildItem restClientAnnotationsTransformer() { return new RestClientAnnotationsTransformerBuildItem(new RestClientCacheAnnotationsTransformer()); } + @BuildStep + CacheTypeBuildItem type(CacheBuildConfig config) { + return new CacheTypeBuildItem( + CAFFEINE_CACHE_TYPE.equals(config.type()) ? CacheTypeBuildItem.Type.LOCAL : CacheTypeBuildItem.Type.REMOTE); + } + @BuildStep void validateCacheAnnotationsAndProduceCacheNames(CombinedIndexBuildItem combinedIndex, List additionalCacheNames, diff --git a/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/CacheProcessor.java b/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/CacheProcessor.java index e03af658f6325..a9f940865591b 100644 --- a/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/CacheProcessor.java +++ b/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/CacheProcessor.java @@ -1,21 +1,40 @@ package io.quarkus.qute.deployment; +import java.util.Optional; + import io.quarkus.arc.deployment.AdditionalBeanBuildItem; import io.quarkus.cache.deployment.spi.AdditionalCacheNameBuildItem; -import io.quarkus.deployment.Capabilities; -import io.quarkus.deployment.Capability; +import io.quarkus.cache.deployment.spi.CacheTypeBuildItem; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; import io.quarkus.qute.cache.QuteCache; +import io.quarkus.qute.runtime.cache.CacheConfigurator; +import io.quarkus.qute.runtime.cache.MissingCacheConfigurator; +import io.quarkus.qute.runtime.cache.UnsupportedRemoteCacheConfigurator; public class CacheProcessor { @BuildStep - void initialize(Capabilities capabilities, BuildProducer beans, + void initialize(Optional cacheTypeBuildItem, + BuildProducer beans, BuildProducer cacheNames) { - if (capabilities.isPresent(Capability.CACHE)) { - beans.produce(new AdditionalBeanBuildItem("io.quarkus.qute.runtime.cache.CacheConfigurator")); - // We need to produce additional cache name because quarkus-cache only considers the CombinedIndexBuildItem and not the bean archive index + Class configuratorClass; + boolean supported = false; + if (cacheTypeBuildItem.isEmpty()) { // no caching enabled + configuratorClass = MissingCacheConfigurator.class; + } else { + CacheTypeBuildItem.Type type = cacheTypeBuildItem.get().getType(); + if (type != CacheTypeBuildItem.Type.LOCAL) { // it does not make sense to use a remote cache for Qute + configuratorClass = UnsupportedRemoteCacheConfigurator.class; + } else { + configuratorClass = CacheConfigurator.class; + supported = true; + } + } + + beans.produce(new AdditionalBeanBuildItem(configuratorClass.getName())); + // We need to produce additional cache name because quarkus-cache only considers the CombinedIndexBuildItem and not the bean archive index + if (supported) { cacheNames.produce(new AdditionalCacheNameBuildItem(QuteCache.NAME)); } } diff --git a/extensions/qute/runtime/src/main/java/io/quarkus/qute/runtime/cache/MissingCacheConfigurator.java b/extensions/qute/runtime/src/main/java/io/quarkus/qute/runtime/cache/MissingCacheConfigurator.java new file mode 100644 index 0000000000000..4899d01811192 --- /dev/null +++ b/extensions/qute/runtime/src/main/java/io/quarkus/qute/runtime/cache/MissingCacheConfigurator.java @@ -0,0 +1,24 @@ +package io.quarkus.qute.runtime.cache; + +import java.util.concurrent.CompletionStage; +import java.util.function.Function; + +import jakarta.enterprise.event.Observes; + +import io.quarkus.qute.CacheSectionHelper; +import io.quarkus.qute.EngineBuilder; +import io.quarkus.qute.ResultNode; + +public class MissingCacheConfigurator { + + void configureEngine(@Observes EngineBuilder builder) { + builder.addSectionHelper(new CacheSectionHelper.Factory(new CacheSectionHelper.Cache() { + + @Override + public CompletionStage getValue(String key, Function> loader) { + throw new IllegalStateException("#cache cannot be used without the 'quarkus-cache' extension"); + } + })); + } + +} diff --git a/extensions/qute/runtime/src/main/java/io/quarkus/qute/runtime/cache/UnsupportedRemoteCacheConfigurator.java b/extensions/qute/runtime/src/main/java/io/quarkus/qute/runtime/cache/UnsupportedRemoteCacheConfigurator.java new file mode 100644 index 0000000000000..df31ba7047186 --- /dev/null +++ b/extensions/qute/runtime/src/main/java/io/quarkus/qute/runtime/cache/UnsupportedRemoteCacheConfigurator.java @@ -0,0 +1,24 @@ +package io.quarkus.qute.runtime.cache; + +import java.util.concurrent.CompletionStage; +import java.util.function.Function; + +import jakarta.enterprise.event.Observes; + +import io.quarkus.qute.CacheSectionHelper; +import io.quarkus.qute.EngineBuilder; +import io.quarkus.qute.ResultNode; + +public class UnsupportedRemoteCacheConfigurator { + + void configureEngine(@Observes EngineBuilder builder) { + builder.addSectionHelper(new CacheSectionHelper.Factory(new CacheSectionHelper.Cache() { + + @Override + public CompletionStage getValue(String key, Function> loader) { + throw new IllegalStateException("#cache is not supported for remote caches"); + } + })); + } + +}