Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce build time flag property to disable the cache extension #11139

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
@ConfigRoot
public class CacheConfig {

/**
* Whether or not the cache extension is enabled. If the extension is disabled, the caching annotations will have no effect
* at run time.
*/
@ConfigItem(defaultValue = "true")
public boolean enabled;

/**
* Cache type.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BooleanSupplier;

import javax.enterprise.inject.spi.DeploymentException;

Expand Down Expand Up @@ -45,20 +46,20 @@ FeatureBuildItem feature() {
return new FeatureBuildItem(Feature.CACHE);
}

@BuildStep
@BuildStep(onlyIf = CacheEnabled.class)
AnnotationsTransformerBuildItem annotationsTransformer() {
return new AnnotationsTransformerBuildItem(new CacheAnnotationsTransformer());
}

@BuildStep
@BuildStep(onlyIf = CacheEnabled.class)
List<AdditionalBeanBuildItem> additionalBeans() {
return Arrays.asList(
new AdditionalBeanBuildItem(CacheInvalidateAllInterceptor.class),
new AdditionalBeanBuildItem(CacheInvalidateInterceptor.class),
new AdditionalBeanBuildItem(CacheResultInterceptor.class));
}

@BuildStep
@BuildStep(onlyIf = CacheEnabled.class)
ValidationErrorBuildItem validateBeanDeployment(ValidationPhaseBuildItem validationPhase) {
AnnotationStore annotationStore = validationPhase.getContext().get(Key.ANNOTATION_STORE);
List<Throwable> throwables = new ArrayList<>();
Expand All @@ -74,7 +75,7 @@ ValidationErrorBuildItem validateBeanDeployment(ValidationPhaseBuildItem validat
return new ValidationErrorBuildItem(throwables.toArray(new Throwable[0]));
}

@BuildStep
@BuildStep(onlyIf = CacheEnabled.class)
@Record(RUNTIME_INIT)
void recordCachesBuild(CombinedIndexBuildItem combinedIndex, BeanContainerBuildItem beanContainer, CacheConfig config,
CaffeineCacheBuildRecorder caffeineRecorder,
Expand Down Expand Up @@ -113,4 +114,13 @@ private Set<String> getCacheNames(IndexView index) {
}
return cacheNames;
}

private static class CacheEnabled implements BooleanSupplier {

CacheConfig config;

public boolean getAsBoolean() {
return config.enabled;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.quarkus.cache.test.deployment;

import static org.junit.jupiter.api.Assertions.assertNotEquals;

import javax.enterprise.context.Dependent;
import javax.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.cache.CacheResult;
import io.quarkus.test.QuarkusUnitTest;

public class DisabledCacheTest {

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addAsResource(new StringAsset("quarkus.cache.enabled=false"), "application.properties")
.addClass(CachedService.class));

private static final String KEY = "key";

@Inject
CachedService cachedService;

@Test
public void testEnabledFlagProperty() {
assertNotEquals(cachedService.cachedMethod(KEY), cachedService.cachedMethod(KEY));
}

@Dependent
static class CachedService {

@CacheResult(cacheName = "test-cache")
public Object cachedMethod(String key) {
return new Object();
}
}
}