public class CacheControl
A builder for creating "Cache-Control" HTTP response headers.
Adding Cache-Control directives to HTTP responses can significantly improve the client
experience when interacting with a web application. This builder creates opinionated
"Cache-Control" headers with response directives only, with several use cases in mind.
Caching HTTP responses with CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS)
will result in Cache-Control: "max-age=3600"
Preventing cache with CacheControl cc = CacheControl.noStore()
will result in Cache-Control: "no-store"
Advanced cases like CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS).noTransform().cachePublic()
will result in Cache-Control: "max-age=3600, no-transform, public"
Note that to be efficient, Cache-Control headers should be written along HTTP validators
such as "Last-Modified" or "ETag" headers.
This is well suited for using other optional directives without "max-age",
"no-cache" or "no-store".
this, to facilitate method chaining
public static CacheControl maxAge(long maxAge,
Add a "max-age=" directive.
This directive is well suited for publicly caching resources, knowing that
they won't change within the configured amount of time. Additional directives
can be also used, in case resources shouldn't be cached (cachePrivate())
or transformed (noTransform()) by shared caches.
In order to prevent caches to reuse the cached response even when it has
become stale (i.e. the "max-age" delay is passed), the "must-revalidate"
directive should be set (mustRevalidate()
maxAge - the maximum time the response should be cached
This directive is well suited for telling caches that the response
can be reused only if the client revalidates it with the server.
This directive won't disable cache altogether and may result with clients
sending conditional requests (with "ETag", "If-Modified-Since" headers)
and the server responding with "304 - Not Modified" status.
In order to disable caching and minimize requests/responses exchanges,
the noStore() directive should be used instead of #noCache().
This directive indicates that intermediaries (caches and others) should
not transform the response content. This can be useful to force caches and
CDNs not to automatically gzip or optimize the response content.
public CacheControl staleWhileRevalidate(long staleWhileRevalidate,
Add a "stale-while-revalidate" directive.
This directive indicates that caches MAY serve the response in which it
appears after it becomes stale, up to the indicated number of seconds.
If a cached response is served stale due to the presence of this extension,
the cache SHOULD attempt to revalidate it while still serving stale responses
(i.e. without blocking).
staleWhileRevalidate - the maximum time the response should be used while being revalidated
unit - the time unit of the staleWhileRevalidate argument