Top Description Fields Constructors Methods
org.springframework.http

public Class CacheControl

extends Object
Class Inheritance
Imports
java.time.Duration, java.util.concurrent.TimeUnit, org.springframework.lang.Nullable, org.springframework.util.StringUtils

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.

Note that to be efficient, Cache-Control headers should be written along HTTP validators such as "Last-Modified" or "ETag" headers.

Authors
Brian Clozel, Juergen Hoeller
Since
4.2
See Also
rfc7234 section 5.2.2, HTTP caching - Google developers reference, Mark Nottingham's cache documentation

Field Summary

Modifier and TypeField and Description
private boolean
private boolean
private boolean
private Duration
private boolean
private boolean
private boolean
private boolean
private boolean
private Duration
private Duration
private Duration

Constructor Summary

AccessConstructor and Description
protected
CacheControl()

Create an empty CacheControl instance.

Method Summary

Modifier and TypeMethod and Description
private void
public CacheControl

Returns:

this, to facilitate method chaining
cachePrivate
()

Add a "private" directive.

public CacheControl

Returns:

this, to facilitate method chaining
cachePublic
()

Add a "public" directive.

public static CacheControl

Returns:

this, to facilitate method chaining
empty
()

Return an empty directive.

public String

Returns:

the header value, or null if no directive was added
getHeaderValue
()

Return the "Cache-Control" header value, if any.

public CacheControl

Returns:

this, to facilitate method chaining
immutable
()

Add an "immutable" directive.

public static CacheControl

Returns:

this, to facilitate method chaining
maxAge
(long
the maximum time the response should be cached
maxAge
,
TimeUnit
the time unit of the maxAge argument
unit
)

Add a "max-age=" directive.

public static CacheControl

Returns:

this, to facilitate method chaining
maxAge
(Duration
the maximum time the response should be cached
maxAge
)

Add a "max-age=" directive.

public CacheControl

Returns:

this, to facilitate method chaining
mustRevalidate
()

Add a "must-revalidate" directive.

public static CacheControl

Returns:

this, to facilitate method chaining
noCache
()

Add a "no-cache" directive.

public static CacheControl

Returns:

this, to facilitate method chaining
noStore
()

Add a "no-store" directive.

public CacheControl

Returns:

this, to facilitate method chaining
noTransform
()

Add a "no-transform" directive.

public CacheControl

Returns:

this, to facilitate method chaining
proxyRevalidate
()

Add a "proxy-revalidate" directive.

public CacheControl

Returns:

this, to facilitate method chaining
sMaxAge
(long
the maximum time the response should be cached
sMaxAge
,
TimeUnit
the time unit of the sMaxAge argument
unit
)

Add an "s-maxage" directive.

public CacheControl

Returns:

this, to facilitate method chaining
sMaxAge
(Duration
the maximum time the response should be cached
sMaxAge
)

Add an "s-maxage" directive.

public CacheControl

Returns:

this, to facilitate method chaining
staleIfError
(long
the maximum time the response should be used when errors are encountered
staleIfError
,
TimeUnit
the time unit of the staleIfError argument
unit
)

Add a "stale-if-error" directive.

public CacheControl

Returns:

this, to facilitate method chaining
staleIfError
(Duration
the maximum time the response should be used when errors are encountered
staleIfError
)

Add a "stale-if-error" directive.

public CacheControl

Returns:

this, to facilitate method chaining
staleWhileRevalidate
(long
the maximum time the response should be used while being revalidated
staleWhileRevalidate
,
TimeUnit
the time unit of the staleWhileRevalidate argument
unit
)

Add a "stale-while-revalidate" directive.

public CacheControl

Returns:

this, to facilitate method chaining
staleWhileRevalidate
(Duration
the maximum time the response should be used while being revalidated
staleWhileRevalidate
)

Add a "stale-while-revalidate" directive.

private String

Returns:

the header value (potentially empty)
toHeaderValue
()

Return the "Cache-Control" header value.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

cachePrivateback to summary
private boolean cachePrivate
cachePublicback to summary
private boolean cachePublic
immutableback to summary
private boolean immutable
maxAgeback to summary
private Duration maxAge
Annotations
@Nullable
mustRevalidateback to summary
private boolean mustRevalidate
noCacheback to summary
private boolean noCache
noStoreback to summary
private boolean noStore
noTransformback to summary
private boolean noTransform
proxyRevalidateback to summary
private boolean proxyRevalidate
sMaxAgeback to summary
private Duration sMaxAge
Annotations
@Nullable
staleIfErrorback to summary
private Duration staleIfError
Annotations
@Nullable
staleWhileRevalidateback to summary
private Duration staleWhileRevalidate
Annotations
@Nullable

Constructor Detail

CacheControlback to summary
protected CacheControl()

Create an empty CacheControl instance.

See Also
empty()

Method Detail

appendDirectiveback to summary
private void appendDirective(StringBuilder builder, String value)
cachePrivateback to summary
public CacheControl cachePrivate()

Add a "private" directive.

This directive indicates that the response message is intended for a single user and MUST NOT be stored by a shared cache.

Returns:CacheControl

this, to facilitate method chaining

See Also
rfc7234 section 5.2.2.6
cachePublicback to summary
public CacheControl cachePublic()

Add a "public" directive.

This directive indicates that any cache MAY store the response, even if the response would normally be non-cacheable or cacheable only within a private cache.

Returns:CacheControl

this, to facilitate method chaining

See Also
rfc7234 section 5.2.2.5
emptyback to summary
public static CacheControl empty()

Return an empty directive.

This is well suited for using other optional directives without "max-age", "no-cache" or "no-store".

Returns:CacheControl

this, to facilitate method chaining

getHeaderValueback to summary
public String getHeaderValue()

Return the "Cache-Control" header value, if any.

Returns:String

the header value, or null if no directive was added

Annotations
@Nullable
immutableback to summary
public CacheControl immutable()

Add an "immutable" directive.

This directive indicates that the origin server will not update the representation of that resource during the freshness lifetime of the response. Adding a max-age directive is strongly advised to enforce the actual freshness lifetime.

Returns:CacheControl

this, to facilitate method chaining

Since
6.0.5
See Also
rfc8246
maxAgeback to summary
public static CacheControl maxAge(long maxAge, TimeUnit unit)

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()

Parameters
maxAge:long

the maximum time the response should be cached

unit:TimeUnit

the time unit of the maxAge argument

Returns:CacheControl

this, to facilitate method chaining

See Also
maxAge(Duration), rfc7234 section 5.2.2.8
maxAgeback to summary
public static CacheControl maxAge(Duration 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()

Parameters
maxAge:Duration

the maximum time the response should be cached

Returns:CacheControl

this, to facilitate method chaining

Since
5.2
See Also
rfc7234 section 5.2.2.8
mustRevalidateback to summary
public CacheControl mustRevalidate()

Add a "must-revalidate" directive.

This directive indicates that once it has become stale, a cache MUST NOT use the response to satisfy subsequent requests without successful validation on the origin server.

Returns:CacheControl

this, to facilitate method chaining

See Also
rfc7234 section 5.2.2.1
noCacheback to summary
public static CacheControl noCache()

Add a "no-cache" directive.

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().

Returns:CacheControl

this, to facilitate method chaining

See Also
rfc7234 section 5.2.2.2
noStoreback to summary
public static CacheControl noStore()

Add a "no-store" directive.

This directive is well suited for preventing caches (browsers and proxies) to cache the content of responses.

Returns:CacheControl

this, to facilitate method chaining

See Also
rfc7234 section 5.2.2.3
noTransformback to summary
public CacheControl noTransform()

Add a "no-transform" directive.

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.

Returns:CacheControl

this, to facilitate method chaining

See Also
rfc7234 section 5.2.2.4
proxyRevalidateback to summary
public CacheControl proxyRevalidate()

Add a "proxy-revalidate" directive.

This directive has the same meaning as the "must-revalidate" directive, except that it does not apply to private caches (i.e. browsers, HTTP clients).

Returns:CacheControl

this, to facilitate method chaining

See Also
rfc7234 section 5.2.2.7
sMaxAgeback to summary
public CacheControl sMaxAge(long sMaxAge, TimeUnit unit)

Add an "s-maxage" directive.

This directive indicates that, in shared caches, the maximum age specified by this directive overrides the maximum age specified by other directives.

Parameters
sMaxAge:long

the maximum time the response should be cached

unit:TimeUnit

the time unit of the sMaxAge argument

Returns:CacheControl

this, to facilitate method chaining

See Also
sMaxAge(Duration), rfc7234 section 5.2.2.9
sMaxAgeback to summary
public CacheControl sMaxAge(Duration sMaxAge)

Add an "s-maxage" directive.

This directive indicates that, in shared caches, the maximum age specified by this directive overrides the maximum age specified by other directives.

Parameters
sMaxAge:Duration

the maximum time the response should be cached

Returns:CacheControl

this, to facilitate method chaining

Since
5.2
See Also
rfc7234 section 5.2.2.9
staleIfErrorback to summary
public CacheControl staleIfError(long staleIfError, TimeUnit unit)

Add a "stale-if-error" directive.

This directive indicates that when an error is encountered, a cached stale response MAY be used to satisfy the request, regardless of other freshness information.

Parameters
staleIfError:long

the maximum time the response should be used when errors are encountered

unit:TimeUnit

the time unit of the staleIfError argument

Returns:CacheControl

this, to facilitate method chaining

See Also
staleIfError(Duration), rfc5861 section 4
staleIfErrorback to summary
public CacheControl staleIfError(Duration staleIfError)

Add a "stale-if-error" directive.

This directive indicates that when an error is encountered, a cached stale response MAY be used to satisfy the request, regardless of other freshness information.

Parameters
staleIfError:Duration

the maximum time the response should be used when errors are encountered

Returns:CacheControl

this, to facilitate method chaining

Since
5.2
See Also
rfc5861 section 4
staleWhileRevalidateback to summary
public CacheControl staleWhileRevalidate(long staleWhileRevalidate, TimeUnit unit)

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).

Parameters
staleWhileRevalidate:long

the maximum time the response should be used while being revalidated

unit:TimeUnit

the time unit of the staleWhileRevalidate argument

Returns:CacheControl

this, to facilitate method chaining

See Also
staleWhileRevalidate(Duration), rfc5861 section 3
staleWhileRevalidateback to summary
public CacheControl staleWhileRevalidate(Duration 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).

Parameters
staleWhileRevalidate:Duration

the maximum time the response should be used while being revalidated

Returns:CacheControl

this, to facilitate method chaining

Since
5.2
See Also
rfc5861 section 3
toHeaderValueback to summary
private String toHeaderValue()

Return the "Cache-Control" header value.

Returns:String

the header value (potentially empty)

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override