Polish Fix for Local Response Cache

See gh-2848
This commit is contained in:
spencergibb
2023-02-14 14:14:03 -05:00
parent 08167a2da5
commit bcdd8749b2
4 changed files with 22 additions and 14 deletions

View File

@@ -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 {
}

View File

@@ -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
*/

View File

@@ -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()));

View File

@@ -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);
});
}