diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/LocalResponseCacheAutoConfiguration.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/LocalResponseCacheAutoConfiguration.java index 6b2bdf35..74647718 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/LocalResponseCacheAutoConfiguration.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/LocalResponseCacheAutoConfiguration.java @@ -24,7 +24,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; +import org.springframework.boot.autoconfigure.condition.AllNestedConditions; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -86,13 +86,12 @@ public class LocalResponseCacheAutoConfiguration { } @Bean(name = RESPONSE_CACHE_MANAGER_NAME) - public static CacheManager gatewayCacheManager(LocalResponseCacheProperties cacheProperties) { - CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(); - caffeineCacheManager.setCaffeine(caffeine(cacheProperties)); - return caffeineCacheManager; + public CacheManager gatewayCacheManager(LocalResponseCacheProperties cacheProperties) { + return createGatewayCacheManager(cacheProperties); } - private static Caffeine caffeine(LocalResponseCacheProperties cacheProperties) { + @SuppressWarnings({ "unchecked", "rawtypes" }) + public static CaffeineCacheManager createGatewayCacheManager(LocalResponseCacheProperties cacheProperties) { Caffeine caffeine = Caffeine.newBuilder(); LOGGER.info("Initializing Caffeine"); Duration ttlSeconds = cacheProperties.getTimeToLive(); @@ -101,7 +100,9 @@ public class LocalResponseCacheAutoConfiguration { if (cacheProperties.getSize() != null) { caffeine.maximumWeight(cacheProperties.getSize().toBytes()).weigher(responseCacheSizeWeigher()); } - return caffeine; + CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(); + caffeineCacheManager.setCaffeine(caffeine); + return caffeineCacheManager; } private static ResponseCacheSizeWeigher responseCacheSizeWeigher() { @@ -112,13 +113,13 @@ public class LocalResponseCacheAutoConfiguration { return cacheManager.getCache(RESPONSE_CACHE_NAME); } - public static class OnGlobalLocalResponseCacheCondition extends AnyNestedCondition { + public static class OnGlobalLocalResponseCacheCondition extends AllNestedConditions { OnGlobalLocalResponseCacheCondition() { super(ConfigurationPhase.REGISTER_BEAN); } - @ConditionalOnProperty(value = "spring.cloud.gateway.enabled", havingValue = "true") + @ConditionalOnProperty(value = "spring.cloud.gateway.enabled", havingValue = "true", matchIfMissing = true) static class OnGatewayPropertyEnabled { } diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/GlobalLocalResponseCacheGatewayFilter.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/GlobalLocalResponseCacheGatewayFilter.java index 78d0ff1e..56715195 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/GlobalLocalResponseCacheGatewayFilter.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/GlobalLocalResponseCacheGatewayFilter.java @@ -30,6 +30,7 @@ import org.springframework.web.server.ServerWebExchange; import static org.springframework.cloud.gateway.filter.factory.cache.LocalResponseCacheGatewayFilterFactory.LOCAL_RESPONSE_CACHE_FILTER_APPLIED; /** + * Caches responses for routes that don't have the {@link LocalResponseCacheGatewayFilterFactory} configured. * @author Ignacio Lozano * @author Marta Medio */ diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheGatewayFilterFactory.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheGatewayFilterFactory.java index a73be1a0..39e9f842 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheGatewayFilterFactory.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheGatewayFilterFactory.java @@ -72,7 +72,7 @@ public class LocalResponseCacheGatewayFilterFactory return new ResponseCacheGatewayFilter(cacheManagerFactory.create(globalCache, configuredTimeToLive)); } else { - Cache routeCache = LocalResponseCacheAutoConfiguration.gatewayCacheManager(cacheProperties) + Cache routeCache = LocalResponseCacheAutoConfiguration.createGatewayCacheManager(cacheProperties) .getCache(config.getRouteId() + "-cache"); return new ResponseCacheGatewayFilter( cacheManagerFactory.create(routeCache, cacheProperties.getTimeToLive())); diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/LocalResponseCacheAutoConfigurationTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/LocalResponseCacheAutoConfigurationTests.java index fd25d939..f0ca6a3a 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/LocalResponseCacheAutoConfigurationTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/LocalResponseCacheAutoConfigurationTests.java @@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.cache.CacheManager; import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.cloud.gateway.filter.factory.cache.GlobalLocalResponseCacheGatewayFilter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; @@ -32,16 +33,21 @@ public class LocalResponseCacheAutoConfigurationTests { void onlyOneCacheManagerBeanCreated() { new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(LocalResponseCacheAutoConfiguration.class)) - .run(context -> context.containsBean(LocalResponseCacheAutoConfiguration.RESPONSE_CACHE_MANAGER_NAME)); + .withPropertyValues("spring.cloud.gateway.filter.local-response-cache.enabled=true").run(context -> { + context.containsBean(LocalResponseCacheAutoConfiguration.RESPONSE_CACHE_MANAGER_NAME); + context.assertThat().hasSingleBean(GlobalLocalResponseCacheGatewayFilter.class); + }); } @Test void twoCacheManagerBeans() { - new ApplicationContextRunner().withConfiguration( - AutoConfigurations.of(CustomCacheManagerConfig.class, LocalResponseCacheAutoConfiguration.class)) - .run(context -> { + new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(CustomCacheManagerConfig.class, + LocalResponseCacheAutoConfiguration.class)) + .withPropertyValues("spring.cloud.gateway.filter.local-response-cache.enabled=true").run(context -> { context.containsBean(LocalResponseCacheAutoConfiguration.RESPONSE_CACHE_MANAGER_NAME); context.containsBean("myCacheManager"); + context.assertThat().hasSingleBean(GlobalLocalResponseCacheGatewayFilter.class); }); }