16
16
17
17
package org .springframework .http ;
18
18
19
+ import java .time .Duration ;
19
20
import java .util .concurrent .TimeUnit ;
20
21
21
22
import org .springframework .lang .Nullable ;
50
51
*/
51
52
public class CacheControl {
52
53
53
- private long maxAge = -1 ;
54
+ @ Nullable
55
+ private Duration maxAge = null ;
54
56
55
57
private boolean noCache = false ;
56
58
@@ -66,12 +68,14 @@ public class CacheControl {
66
68
67
69
private boolean proxyRevalidate = false ;
68
70
69
- private long staleWhileRevalidate = -1 ;
70
-
71
- private long staleIfError = -1 ;
71
+ @ Nullable
72
+ private Duration staleWhileRevalidate = null ;
72
73
73
- private long sMaxAge = -1 ;
74
+ @ Nullable
75
+ private Duration staleIfError = null ;
74
76
77
+ @ Nullable
78
+ private Duration sMaxAge = null ;
75
79
76
80
/**
77
81
* Create an empty CacheControl instance.
@@ -106,8 +110,25 @@ public static CacheControl empty() {
106
110
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.8">rfc7234 section 5.2.2.8</a>
107
111
*/
108
112
public static CacheControl maxAge (long maxAge , TimeUnit unit ) {
113
+ return maxAge (Duration .ofSeconds (unit .toSeconds (maxAge )));
114
+ }
115
+
116
+ /**
117
+ * Add a "max-age=" directive.
118
+ * <p>This directive is well suited for publicly caching resources, knowing that
119
+ * they won't change within the configured amount of time. Additional directives
120
+ * can be also used, in case resources shouldn't be cached ({@link #cachePrivate()})
121
+ * or transformed ({@link #noTransform()}) by shared caches.
122
+ * <p>In order to prevent caches to reuse the cached response even when it has
123
+ * become stale (i.e. the "max-age" delay is passed), the "must-revalidate"
124
+ * directive should be set ({@link #mustRevalidate()}
125
+ * @param maxAge the maximum time the response should be cached
126
+ * @return {@code this}, to facilitate method chaining
127
+ * @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.8">rfc7234 section 5.2.2.8</a>
128
+ */
129
+ public static CacheControl maxAge (Duration maxAge ) {
109
130
CacheControl cc = new CacheControl ();
110
- cc .maxAge = unit . toSeconds ( maxAge ) ;
131
+ cc .maxAge = maxAge ;
111
132
return cc ;
112
133
}
113
134
@@ -216,7 +237,19 @@ public CacheControl proxyRevalidate() {
216
237
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.9">rfc7234 section 5.2.2.9</a>
217
238
*/
218
239
public CacheControl sMaxAge (long sMaxAge , TimeUnit unit ) {
219
- this .sMaxAge = unit .toSeconds (sMaxAge );
240
+ return sMaxAge (Duration .ofSeconds (unit .toSeconds (sMaxAge )));
241
+ }
242
+
243
+ /**
244
+ * Add an "s-maxage" directive.
245
+ * <p>This directive indicates that, in shared caches, the maximum age specified
246
+ * by this directive overrides the maximum age specified by other directives.
247
+ * @param sMaxAge the maximum time the response should be cached
248
+ * @return {@code this}, to facilitate method chaining
249
+ * @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.9">rfc7234 section 5.2.2.9</a>
250
+ */
251
+ public CacheControl sMaxAge (Duration sMaxAge ) {
252
+ this .sMaxAge = sMaxAge ;
220
253
return this ;
221
254
}
222
255
@@ -233,7 +266,22 @@ public CacheControl sMaxAge(long sMaxAge, TimeUnit unit) {
233
266
* @see <a href="https://tools.ietf.org/html/rfc5861#section-3">rfc5861 section 3</a>
234
267
*/
235
268
public CacheControl staleWhileRevalidate (long staleWhileRevalidate , TimeUnit unit ) {
236
- this .staleWhileRevalidate = unit .toSeconds (staleWhileRevalidate );
269
+ return staleWhileRevalidate (Duration .ofSeconds (unit .toSeconds (staleWhileRevalidate )));
270
+ }
271
+
272
+ /**
273
+ * Add a "stale-while-revalidate" directive.
274
+ * <p>This directive indicates that caches MAY serve the response in which it
275
+ * appears after it becomes stale, up to the indicated number of seconds.
276
+ * If a cached response is served stale due to the presence of this extension,
277
+ * the cache SHOULD attempt to revalidate it while still serving stale responses
278
+ * (i.e. without blocking).
279
+ * @param staleWhileRevalidate the maximum time the response should be used while being revalidated
280
+ * @return {@code this}, to facilitate method chaining
281
+ * @see <a href="https://tools.ietf.org/html/rfc5861#section-3">rfc5861 section 3</a>
282
+ */
283
+ public CacheControl staleWhileRevalidate (Duration staleWhileRevalidate ) {
284
+ this .staleWhileRevalidate = staleWhileRevalidate ;
237
285
return this ;
238
286
}
239
287
@@ -247,10 +295,21 @@ public CacheControl staleWhileRevalidate(long staleWhileRevalidate, TimeUnit uni
247
295
* @see <a href="https://tools.ietf.org/html/rfc5861#section-4">rfc5861 section 4</a>
248
296
*/
249
297
public CacheControl staleIfError (long staleIfError , TimeUnit unit ) {
250
- this .staleIfError = unit .toSeconds (staleIfError );
251
- return this ;
298
+ return staleIfError (Duration .ofSeconds (unit .toSeconds (staleIfError )));
252
299
}
253
300
301
+ /**
302
+ * Add a "stale-if-error" directive.
303
+ * <p>This directive indicates that when an error is encountered, a cached stale response
304
+ * MAY be used to satisfy the request, regardless of other freshness information.
305
+ * @param staleIfError the maximum time the response should be used when errors are encountered
306
+ * @return {@code this}, to facilitate method chaining
307
+ * @see <a href="https://tools.ietf.org/html/rfc5861#section-4">rfc5861 section 4</a>
308
+ */
309
+ public CacheControl staleIfError (Duration staleIfError ) {
310
+ this .staleIfError = staleIfError ;
311
+ return this ;
312
+ }
254
313
255
314
/**
256
315
* Return the "Cache-Control" header value, if any.
@@ -268,8 +327,8 @@ public String getHeaderValue() {
268
327
*/
269
328
private String toHeaderValue () {
270
329
StringBuilder headerValue = new StringBuilder ();
271
- if (this .maxAge != - 1 ) {
272
- appendDirective (headerValue , "max-age=" + this .maxAge );
330
+ if (this .maxAge != null ) {
331
+ appendDirective (headerValue , "max-age=" + this .maxAge . getSeconds () );
273
332
}
274
333
if (this .noCache ) {
275
334
appendDirective (headerValue , "no-cache" );
@@ -292,14 +351,14 @@ private String toHeaderValue() {
292
351
if (this .proxyRevalidate ) {
293
352
appendDirective (headerValue , "proxy-revalidate" );
294
353
}
295
- if (this .sMaxAge != - 1 ) {
296
- appendDirective (headerValue , "s-maxage=" + this .sMaxAge );
354
+ if (this .sMaxAge != null ) {
355
+ appendDirective (headerValue , "s-maxage=" + this .sMaxAge . getSeconds () );
297
356
}
298
- if (this .staleIfError != - 1 ) {
299
- appendDirective (headerValue , "stale-if-error=" + this .staleIfError );
357
+ if (this .staleIfError != null ) {
358
+ appendDirective (headerValue , "stale-if-error=" + this .staleIfError . getSeconds () );
300
359
}
301
- if (this .staleWhileRevalidate != - 1 ) {
302
- appendDirective (headerValue , "stale-while-revalidate=" + this .staleWhileRevalidate );
360
+ if (this .staleWhileRevalidate != null ) {
361
+ appendDirective (headerValue , "stale-while-revalidate=" + this .staleWhileRevalidate . getSeconds () );
303
362
}
304
363
return headerValue .toString ();
305
364
}
0 commit comments