Skip to content

Commit

Permalink
Introduce build time flag property to disable the cache extension
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenneg committed Jul 31, 2020
1 parent 327e48a commit c4d5226
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
@ConfigRoot
public class CacheConfig {

/**
* Whether or not the cache extension is enabled.
*/
@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 = IsEnabled.class)
AnnotationsTransformerBuildItem annotationsTransformer() {
return new AnnotationsTransformerBuildItem(new CacheAnnotationsTransformer());
}

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

@BuildStep
@BuildStep(onlyIf = IsEnabled.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 = IsEnabled.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 IsEnabled 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();
}
}
}

0 comments on commit c4d5226

Please sign in to comment.