Skip to content

Commit 29ec072

Browse files
committed
Polish gh-3047 "Correct inconsistencies in the Cache-Control..."
1 parent 9fce110 commit 29ec072

12 files changed

+89
-93
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/GlobalLocalResponseCacheGatewayFilter.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,15 @@ public class GlobalLocalResponseCacheGatewayFilter implements GlobalFilter, Orde
4040

4141
private final ResponseCacheGatewayFilter responseCacheGatewayFilter;
4242

43+
@Deprecated
4344
public GlobalLocalResponseCacheGatewayFilter(ResponseCacheManagerFactory cacheManagerFactory, Cache globalCache,
44-
Duration configuredTimeToLive, LocalResponseCacheRequestOptions requestOptions) {
45+
Duration configuredTimeToLive) {
46+
responseCacheGatewayFilter = new ResponseCacheGatewayFilter(
47+
cacheManagerFactory.create(globalCache, configuredTimeToLive));
48+
}
49+
50+
public GlobalLocalResponseCacheGatewayFilter(ResponseCacheManagerFactory cacheManagerFactory, Cache globalCache,
51+
Duration configuredTimeToLive, LocalResponseCacheProperties.RequestOptions requestOptions) {
4552
responseCacheGatewayFilter = new ResponseCacheGatewayFilter(
4653
cacheManagerFactory.create(globalCache, configuredTimeToLive, requestOptions));
4754
}

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheGatewayFilterFactory.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.cloud.gateway.config.LocalResponseCacheAutoConfiguration;
2525
import org.springframework.cloud.gateway.filter.GatewayFilter;
2626
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
27+
import org.springframework.cloud.gateway.filter.factory.cache.LocalResponseCacheProperties.RequestOptions;
2728
import org.springframework.cloud.gateway.support.HasRouteId;
2829
import org.springframework.util.unit.DataSize;
2930
import org.springframework.validation.annotation.Validated;
@@ -53,10 +54,16 @@ public class LocalResponseCacheGatewayFilterFactory
5354

5455
private final DataSize defaultSize;
5556

56-
private final LocalResponseCacheRequestOptions requestOptions;
57+
private final RequestOptions requestOptions;
5758

59+
@Deprecated
5860
public LocalResponseCacheGatewayFilterFactory(ResponseCacheManagerFactory cacheManagerFactory,
59-
Duration defaultTimeToLive, DataSize defaultSize, LocalResponseCacheRequestOptions requestOptions) {
61+
Duration defaultTimeToLive, DataSize defaultSize) {
62+
this(cacheManagerFactory, defaultTimeToLive, defaultSize, new RequestOptions());
63+
}
64+
65+
public LocalResponseCacheGatewayFilterFactory(ResponseCacheManagerFactory cacheManagerFactory,
66+
Duration defaultTimeToLive, DataSize defaultSize, RequestOptions requestOptions) {
6067
super(RouteCacheConfiguration.class);
6168
this.cacheManagerFactory = cacheManagerFactory;
6269
this.defaultTimeToLive = defaultTimeToLive;

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheProperties.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class LocalResponseCacheProperties {
4040

4141
private Duration timeToLive;
4242

43-
private LocalResponseCacheRequestOptions request = new LocalResponseCacheRequestOptions();
43+
private RequestOptions request = new RequestOptions();
4444

4545
public DataSize getSize() {
4646
return size;
@@ -66,11 +66,11 @@ public void setTimeToLive(Duration timeToLive) {
6666
this.timeToLive = timeToLive;
6767
}
6868

69-
public LocalResponseCacheRequestOptions getRequest() {
69+
public RequestOptions getRequest() {
7070
return request;
7171
}
7272

73-
public void setRequest(LocalResponseCacheRequestOptions request) {
73+
public void setRequest(RequestOptions request) {
7474
this.request = request;
7575
}
7676

@@ -80,4 +80,43 @@ public String toString() {
8080
+ '}';
8181
}
8282

83+
public static class RequestOptions {
84+
85+
private NoCacheStrategy noCacheStrategy = NoCacheStrategy.SKIP_UPDATE_CACHE_ENTRY;
86+
87+
public NoCacheStrategy getNoCacheStrategy() {
88+
return noCacheStrategy;
89+
}
90+
91+
public void setNoCacheStrategy(NoCacheStrategy noCacheStrategy) {
92+
this.noCacheStrategy = noCacheStrategy;
93+
}
94+
95+
@Override
96+
public String toString() {
97+
return "RequestOptions{" + "noCacheStrategy=" + noCacheStrategy + '}';
98+
}
99+
100+
}
101+
102+
/**
103+
* When client sends "no-cache" directive in "Cache-Control" header, the response
104+
* should be re-validated from upstream. There are several strategies that indicates
105+
* what to do with the new fresh response.
106+
*/
107+
public enum NoCacheStrategy {
108+
109+
/**
110+
* Update the cache entry by the fresh response coming from upstream with a new
111+
* time to live.
112+
*/
113+
UPDATE_CACHE_ENTRY,
114+
/**
115+
* Skip the update. The client will receive the fresh response, other clients will
116+
* receive the old entry in cache.
117+
*/
118+
SKIP_UPDATE_CACHE_ENTRY
119+
120+
}
121+
83122
}

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheRequestOptions.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheUtils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@
1616

1717
package org.springframework.cloud.gateway.filter.factory.cache;
1818

19-
import java.util.Optional;
20-
2119
import org.springframework.http.server.reactive.ServerHttpRequest;
20+
import org.springframework.util.StringUtils;
2221

2322
public final class LocalResponseCacheUtils {
2423

2524
private LocalResponseCacheUtils() {
2625
}
2726

2827
public static boolean isNoCacheRequest(ServerHttpRequest request) {
29-
return Optional.ofNullable(request.getHeaders().getCacheControl())
30-
.filter(cc -> cc.matches(".*(\s|,|^)no-cache(\\s|,|$).*")).isPresent();
28+
String cacheControl = request.getHeaders().getCacheControl();
29+
return StringUtils.hasText(cacheControl) && cacheControl.matches(".*(\s|,|^)no-cache(\\s|,|$).*");
3130
}
3231

3332
}

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/RequestNoCacheDirectiveStrategy.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/ResponseCacheManager.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import reactor.core.publisher.Mono;
3131

3232
import org.springframework.cache.Cache;
33+
import org.springframework.cloud.gateway.filter.factory.cache.LocalResponseCacheProperties.NoCacheStrategy;
34+
import org.springframework.cloud.gateway.filter.factory.cache.LocalResponseCacheProperties.RequestOptions;
3335
import org.springframework.cloud.gateway.filter.factory.cache.keygenerator.CacheKeyGenerator;
3436
import org.springframework.cloud.gateway.filter.factory.cache.postprocessor.AfterCacheExchangeMutator;
3537
import org.springframework.cloud.gateway.filter.factory.cache.postprocessor.SetCacheDirectivesByMaxAgeAfterCacheExchangeMutator;
@@ -66,8 +68,13 @@ public class ResponseCacheManager {
6668

6769
private final boolean ignoreNoCacheUpdate;
6870

71+
@Deprecated
72+
public ResponseCacheManager(CacheKeyGenerator cacheKeyGenerator, Cache cache, Duration configuredTimeToLive) {
73+
this(cacheKeyGenerator, cache, configuredTimeToLive, new RequestOptions());
74+
}
75+
6976
public ResponseCacheManager(CacheKeyGenerator cacheKeyGenerator, Cache cache, Duration configuredTimeToLive,
70-
LocalResponseCacheRequestOptions requestOptions) {
77+
RequestOptions requestOptions) {
7178
this.cacheKeyGenerator = cacheKeyGenerator;
7279
this.cache = cache;
7380
this.ignoreNoCacheUpdate = isSkipNoCacheUpdateActive(requestOptions);
@@ -78,9 +85,9 @@ public ResponseCacheManager(CacheKeyGenerator cacheKeyGenerator, Cache cache, Du
7885
new SetCacheDirectivesByMaxAgeAfterCacheExchangeMutator());
7986
}
8087

81-
private static boolean isSkipNoCacheUpdateActive(LocalResponseCacheRequestOptions requestOptions) {
82-
return Optional.ofNullable(requestOptions).map(LocalResponseCacheRequestOptions::getNoCache)
83-
.filter(RequestNoCacheDirectiveStrategy.SKIP_UPDATE_CACHE_ENTRY::equals).isPresent();
88+
private static boolean isSkipNoCacheUpdateActive(RequestOptions requestOptions) {
89+
return requestOptions != null
90+
&& requestOptions.getNoCacheStrategy().equals(NoCacheStrategy.SKIP_UPDATE_CACHE_ENTRY);
8491
}
8592

8693
private static final List<HttpStatusCode> statusesToCache = Arrays.asList(HttpStatus.OK, HttpStatus.PARTIAL_CONTENT,

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/ResponseCacheManagerFactory.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ public ResponseCacheManagerFactory(CacheKeyGenerator cacheKeyGenerator) {
3333
this.cacheKeyGenerator = cacheKeyGenerator;
3434
}
3535

36+
@Deprecated
37+
public ResponseCacheManager create(Cache cache, Duration timeToLive) {
38+
return new ResponseCacheManager(cacheKeyGenerator, cache, timeToLive);
39+
}
40+
3641
public ResponseCacheManager create(Cache cache, Duration timeToLive,
37-
LocalResponseCacheRequestOptions requestOptions) {
42+
LocalResponseCacheProperties.RequestOptions requestOptions) {
3843
return new ResponseCacheManager(cacheKeyGenerator, cache, timeToLive, requestOptions);
3944
}
4045

spring-cloud-gateway-server/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@
4545
{
4646
"name": "spring.cloud.gateway.filter.local-response-cache.size",
4747
"type": "org.springframework.util.unit.DataSize",
48-
"description": "Maximum size of the cache to evict entries for this route (in KB, MB and GB).",
49-
"defaultValue": "null"
48+
"description": "Maximum size of the cache to evict entries for this route (in KB, MB and GB)."
5049
},
5150
{
5251
"name": "spring.cloud.gateway.filter.local-response-cache.time-to-live",

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheGatewayFilterFactoryTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,9 @@ public RouteLocator testRouteLocator(RouteLocatorBuilder builder) {
348348
}
349349

350350
@Nested
351-
@SpringBootTest(
352-
properties = { "spring.cloud.gateway.filter.local-response-cache.enabled=true",
353-
"spring.cloud.gateway.filter.local-response-cache.time-to-live=2m",
354-
"spring.cloud.gateway.filter.local-response-cache.request.no-cache=skip-update-cache-entry" },
351+
@SpringBootTest(properties = { "spring.cloud.gateway.filter.local-response-cache.enabled=true",
352+
"spring.cloud.gateway.filter.local-response-cache.time-to-live=2m",
353+
"spring.cloud.gateway.filter.local-response-cache.request.no-cache-strategy=skip-update-cache-entry" },
355354
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
356355
public class DirectiveNoCacheSkippingUpdate extends BaseWebClientTests {
357356

@@ -425,7 +424,7 @@ public RouteLocator testRouteLocator(RouteLocatorBuilder builder) {
425424
@SpringBootTest(
426425
properties = { "spring.cloud.gateway.filter.local-response-cache.enabled=true",
427426
"spring.cloud.gateway.filter.local-response-cache.time-to-live=2m",
428-
"spring.cloud.gateway.filter.local-response-cache.request.no-cache=update-cache-entry" },
427+
"spring.cloud.gateway.filter.local-response-cache.request.no-cache-strategy=update-cache-entry" },
429428
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
430429
public class DirectiveNoCacheWithUpdate extends BaseWebClientTests {
431430

0 commit comments

Comments
 (0)