Skip to content

Commit

Permalink
Uses a single CaffeineCacheManager in LocalResponseCacheGatewayFilter…
Browse files Browse the repository at this point in the history
…Factory

Moves static methods from LocalResponseCacheAutoConfiguration to LocalResponseCacheUtils

Fixes gh-3025
  • Loading branch information
spencergibb committed Mar 19, 2024
1 parent 7e4bb5c commit 8c9fcdd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,6 +58,8 @@ public class LocalResponseCacheGatewayFilterFactory

private final RequestOptions requestOptions;

private final CaffeineCacheManager caffeineCacheManager;

@Deprecated
public LocalResponseCacheGatewayFilterFactory(ResponseCacheManagerFactory cacheManagerFactory,
Duration defaultTimeToLive, DataSize defaultSize) {
Expand All @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

Expand All @@ -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;
}

}

0 comments on commit 8c9fcdd

Please sign in to comment.