Class CacheControl.Builder
- Enclosing class:
- CacheControl
Composes the directives defined by
RFC 9111 §5.2 into the
comma-separated wire format expected by the Cache-Control response header. Mutually exclusive
cacheability directives (e.g. publicCache() vs privateCache()) overwrite one another;
boolean directives (e.g. noStore()) are idempotent.
Example:
String
See Also:
- Since:
- 10.0.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds the comma-separatedCache-Control header value.Adds an arbitrary cache-control extension directive (e.g.Adds theimmutable directive.maxAge(long seconds) Adds themax-age=N directive (seconds).Adds themax-age=N directive from aDuration.Adds themust-revalidate directive.noCache()Adds theno-cache directive.noStore()Adds theno-store directive.Adds theno-transform directive.Adds theprivate directive (responses may only be cached by the originating client).Adds theproxy-revalidate directive (must-revalidate for shared caches only).Adds thepublic directive (responses may be cached by any cache).sMaxAge(long seconds) Adds thes-maxage=N directive (shared-cache max-age in seconds).Adds thes-maxage=N directive from aDuration.staleIfError(long seconds) Adds thestale-if-error=N directive (seconds).staleWhileRevalidate(long seconds) Adds thestale-while-revalidate=N directive (seconds).toHeader()Builds aCacheControlheader bean directly from this builder.toString()
-
Constructor Details
-
Builder
public Builder()
-
-
Method Details
-
publicCache
Adds thepublic directive (responses may be cached by any cache).Clears any previously set
private directive.- Returns:
- This object.
-
privateCache
Adds theprivate directive (responses may only be cached by the originating client).Clears any previously set
public directive.- Returns:
- This object.
-
noCache
Adds theno-cache directive.Forces caches to revalidate with the origin server before reusing a stored response.
- Returns:
- This object.
-
noStore
Adds theno-store directive.Forbids caches from storing any part of the request or response.
- Returns:
- This object.
-
noTransform
Adds theno-transform directive.Forbids intermediaries from transforming the response payload.
- Returns:
- This object.
-
mustRevalidate
Adds themust-revalidate directive.Forces caches to revalidate stale responses with the origin before serving them.
- Returns:
- This object.
-
proxyRevalidate
Adds theproxy-revalidate directive (must-revalidate for shared caches only).- Returns:
- This object.
-
immutable
Adds theimmutable directive.Signals that the response body will not change for the duration of its freshness lifetime.
- Returns:
- This object.
-
maxAge
Adds themax-age=N directive (seconds).- Parameters:
seconds- Maximum freshness lifetime in seconds. Must be non-negative.- Returns:
- This object.
- Throws:
IllegalArgumentException- Ifsecondsis negative.
-
maxAge
Adds themax-age=N directive from aDuration.- Parameters:
value- Maximum freshness lifetime. Must not benull or negative.- Returns:
- This object.
- Throws:
IllegalArgumentException- Ifvalueisnull or negative.
-
sMaxAge
Adds thes-maxage=N directive (shared-cache max-age in seconds).- Parameters:
seconds- Maximum freshness lifetime in seconds for shared caches. Must be non-negative.- Returns:
- This object.
- Throws:
IllegalArgumentException- Ifsecondsis negative.
-
sMaxAge
Adds thes-maxage=N directive from aDuration.- Parameters:
value- Maximum freshness lifetime for shared caches. Must not benull or negative.- Returns:
- This object.
- Throws:
IllegalArgumentException- Ifvalueisnull or negative.
-
staleWhileRevalidate
Adds thestale-while-revalidate=N directive (seconds).- Parameters:
seconds- Number of seconds a cache may serve a stale response while asynchronously revalidating.- Returns:
- This object.
- Throws:
IllegalArgumentException- Ifsecondsis negative.
-
staleIfError
Adds thestale-if-error=N directive (seconds).- Parameters:
seconds- Number of seconds a cache may serve a stale response when the origin is unreachable.- Returns:
- This object.
- Throws:
IllegalArgumentException- Ifsecondsis negative.
-
extension
Adds an arbitrary cache-control extension directive (e.g."community=\"UCI\"" ).Extensions are appended in registration order at the end of the directive list. The caller is responsible for supplying a token that conforms to the
cache-directive grammar of RFC 9111 §5.2.3.- Parameters:
value- The extension token. Must not benull or blank.- Returns:
- This object.
- Throws:
IllegalArgumentException- Ifvalueisnull or blank.
-
build
Builds the comma-separatedCache-Control header value.Returns an empty string when no directives have been registered.
- Returns:
- The header value. Never
null .
-
toHeader
Builds aCacheControlheader bean directly from this builder.- Returns:
- A
CacheControlcarryingbuild(). Nevernull .
-
toString
-