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 ddeaafd27e..908e2883f8 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 @@ -16,8 +16,6 @@ package org.springframework.cloud.gateway.config; -import java.time.Duration; - import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.Weigher; import org.apache.commons.logging.Log; @@ -35,8 +33,8 @@ import org.springframework.cloud.gateway.filter.factory.cache.GlobalLocalResponseCacheGatewayFilter; import org.springframework.cloud.gateway.filter.factory.cache.LocalResponseCacheGatewayFilterFactory; import org.springframework.cloud.gateway.filter.factory.cache.LocalResponseCacheProperties; +import org.springframework.cloud.gateway.filter.factory.cache.LocalResponseCacheUtils; import org.springframework.cloud.gateway.filter.factory.cache.ResponseCacheManagerFactory; -import org.springframework.cloud.gateway.filter.factory.cache.ResponseCacheSizeWeigher; import org.springframework.cloud.gateway.filter.factory.cache.keygenerator.CacheKeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; @@ -71,7 +69,7 @@ public GlobalLocalResponseCacheGatewayFilter globalLocalResponseCacheGatewayFilt @Bean(name = RESPONSE_CACHE_MANAGER_NAME) @Conditional(LocalResponseCacheAutoConfiguration.OnGlobalLocalResponseCacheCondition.class) public CacheManager gatewayCacheManager(LocalResponseCacheProperties cacheProperties) { - return createGatewayCacheManager(cacheProperties); + return LocalResponseCacheUtils.createGatewayCacheManager(cacheProperties); } @Bean @@ -91,23 +89,13 @@ public CacheKeyGenerator cacheKeyGenerator() { return new CacheKeyGenerator(); } - @SuppressWarnings({ "unchecked", "rawtypes" }) + /** + * @deprecated since 4.1.2 for removal in 4.2.0 in favor of + * {@link LocalResponseCacheUtils#createGatewayCacheManager(LocalResponseCacheProperties)} + */ + @Deprecated(since = "4.1.2", forRemoval = true) public static CaffeineCacheManager createGatewayCacheManager(LocalResponseCacheProperties cacheProperties) { - Caffeine caffeine = Caffeine.newBuilder(); - LOGGER.info("Initializing Caffeine"); - Duration ttlSeconds = cacheProperties.getTimeToLive(); - caffeine.expireAfterWrite(ttlSeconds); - - if (cacheProperties.getSize() != null) { - caffeine.maximumWeight(cacheProperties.getSize().toBytes()).weigher(responseCacheSizeWeigher()); - } - CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(); - caffeineCacheManager.setCaffeine(caffeine); - return caffeineCacheManager; - } - - private static ResponseCacheSizeWeigher responseCacheSizeWeigher() { - return new ResponseCacheSizeWeigher(); + return LocalResponseCacheUtils.createGatewayCacheManager(cacheProperties); } Cache responseCache(CacheManager cacheManager) { 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 652de96f80..a173882727 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 @@ -19,9 +19,11 @@ import java.time.Duration; import java.util.List; +import com.github.benmanes.caffeine.cache.Caffeine; + import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.cache.Cache; -import org.springframework.cloud.gateway.config.LocalResponseCacheAutoConfiguration; +import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; import org.springframework.cloud.gateway.filter.factory.cache.LocalResponseCacheProperties.RequestOptions; @@ -56,6 +58,8 @@ public class LocalResponseCacheGatewayFilterFactory private final RequestOptions requestOptions; + private final CaffeineCacheManager caffeineCacheManager; + @Deprecated public LocalResponseCacheGatewayFilterFactory(ResponseCacheManagerFactory cacheManagerFactory, Duration defaultTimeToLive, DataSize defaultSize) { @@ -64,19 +68,29 @@ public LocalResponseCacheGatewayFilterFactory(ResponseCacheManagerFactory cacheM public LocalResponseCacheGatewayFilterFactory(ResponseCacheManagerFactory cacheManagerFactory, Duration defaultTimeToLive, DataSize defaultSize, RequestOptions requestOptions) { + this(cacheManagerFactory, defaultTimeToLive, defaultSize, requestOptions, new CaffeineCacheManager()); + } + + public LocalResponseCacheGatewayFilterFactory(ResponseCacheManagerFactory cacheManagerFactory, + Duration defaultTimeToLive, DataSize defaultSize, RequestOptions requestOptions, + CaffeineCacheManager caffeineCacheManager) { super(RouteCacheConfiguration.class); this.cacheManagerFactory = cacheManagerFactory; this.defaultTimeToLive = defaultTimeToLive; this.defaultSize = defaultSize; this.requestOptions = requestOptions; + this.caffeineCacheManager = caffeineCacheManager; } @Override + @SuppressWarnings({ "unchecked", "rawtypes" }) public GatewayFilter apply(RouteCacheConfiguration config) { LocalResponseCacheProperties cacheProperties = mapRouteCacheConfig(config); - Cache routeCache = LocalResponseCacheAutoConfiguration.createGatewayCacheManager(cacheProperties) - .getCache(config.getRouteId() + "-cache"); + Caffeine caffeine = LocalResponseCacheUtils.createCaffeine(cacheProperties); + String cacheName = config.getRouteId() + "-cache"; + caffeineCacheManager.registerCustomCache(cacheName, caffeine.build()); + Cache routeCache = caffeineCacheManager.getCache(cacheName); return new ResponseCacheGatewayFilter( cacheManagerFactory.create(routeCache, cacheProperties.getTimeToLive(), requestOptions)); diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheUtils.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheUtils.java index 6de54ac5d1..71349db043 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheUtils.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheUtils.java @@ -16,11 +16,20 @@ package org.springframework.cloud.gateway.filter.factory.cache; +import java.time.Duration; + +import com.github.benmanes.caffeine.cache.Caffeine; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.util.StringUtils; public final class LocalResponseCacheUtils { + private static final Log LOGGER = LogFactory.getLog(LocalResponseCacheUtils.class); + private LocalResponseCacheUtils() { } @@ -29,4 +38,24 @@ public static boolean isNoCacheRequest(ServerHttpRequest request) { return StringUtils.hasText(cacheControl) && cacheControl.matches(".*(\s|,|^)no-cache(\\s|,|$).*"); } + public static CaffeineCacheManager createGatewayCacheManager(LocalResponseCacheProperties cacheProperties) { + Caffeine caffeine = createCaffeine(cacheProperties); + CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(); + caffeineCacheManager.setCaffeine(caffeine); + return caffeineCacheManager; + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + public static Caffeine createCaffeine(LocalResponseCacheProperties cacheProperties) { + Caffeine caffeine = Caffeine.newBuilder(); + LOGGER.info("Initializing Caffeine"); + Duration ttlSeconds = cacheProperties.getTimeToLive(); + caffeine.expireAfterWrite(ttlSeconds); + + if (cacheProperties.getSize() != null) { + caffeine.maximumWeight(cacheProperties.getSize().toBytes()).weigher(new ResponseCacheSizeWeigher()); + } + return caffeine; + } + }