类 CacheControl
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 inCache-Control: "max-age=3600" - Preventing cache with
CacheControl cc = CacheControl.noStore()will result inCache-Control: "no-store" - Advanced cases like
CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS).noTransform().cachePublic()will result inCache-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.
CacheControl can be used to create the value for the Cache-Control header. The Cache-Control general-header field is used to specify directives for caching mechanisms in both requests and responses. Caching directives are unidirectional, meaning that a given directive in a request is not implying that the same directive is to be given in the response.
-- From Mozilla Wiki.
- 从以下版本开始:
- 2.3.7
- 作者:
- Brian Clozel, Juergen Hoeller, Harry Yang
- 另请参阅:
-
字段概要
字段 -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明private voidappendDirective(StringBuilder builder, String value) Add a "private" directive.Add a "public" directive.static CacheControlempty()Return an empty directive.Return the "Cache-Control" header value, if any.Add an "immutable" directive.static CacheControlAdd a "max-age=" directive.static CacheControlAdd a "max-age=" directive.Add a "must-revalidate" directive.static CacheControlnoCache()Add a "no-cache" directive.static CacheControlnoStore()Add a "no-store" directive.Add a "no-transform" directive.Add a "proxy-revalidate" directive.Add an "s-maxage" directive.Add an "s-maxage" directive.staleIfError(long staleIfError, TimeUnit unit) Add a "stale-if-error" directive.staleIfError(Duration staleIfError) Add a "stale-if-error" directive.staleWhileRevalidate(long staleWhileRevalidate, TimeUnit unit) Add a "stale-while-revalidate" directive.staleWhileRevalidate(Duration staleWhileRevalidate) Add a "stale-while-revalidate" directive.private StringReturn the "Cache-Control" header value.toString()
-
字段详细资料
-
maxAge
-
noCache
private boolean noCache -
noStore
private boolean noStore -
mustRevalidate
private boolean mustRevalidate -
noTransform
private boolean noTransform -
cachePublic
private boolean cachePublic -
cachePrivate
private boolean cachePrivate -
proxyRevalidate
private boolean proxyRevalidate -
staleWhileRevalidate
-
staleIfError
-
sMaxAge
-
immutable
private boolean immutable
-
-
构造器详细资料
-
CacheControl
protected CacheControl()Create an empty CacheControl instance.- 另请参阅:
-
-
方法详细资料
-
empty
Return an empty directive.This is well suited for using other optional directives without "max-age", "no-cache" or "no-store".
- 返回:
this, to facilitate method chaining
-
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 cachedunit- the time unit of themaxAgeargument- 返回:
this, to facilitate method chaining- 另请参阅:
-
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, to facilitate method chaining- 另请参阅:
-
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().- 返回:
this, to facilitate method chaining- 另请参阅:
-
noStore
Add a "no-store" directive.This directive is well suited for preventing caches (browsers and proxies) to cache the content of responses.
- 返回:
this, to facilitate method chaining- 另请参阅:
-
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.
- 返回:
this, to facilitate method chaining- 另请参阅:
-
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.
- 返回:
this, to facilitate method chaining- 另请参阅:
-
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.
- 返回:
this, to facilitate method chaining- 另请参阅:
-
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.
- 返回:
this, to facilitate method chaining- 另请参阅:
-
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).
- 返回:
this, to facilitate method chaining- 另请参阅:
-
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.
- 参数:
sMaxAge- the maximum time the response should be cachedunit- the time unit of thesMaxAgeargument- 返回:
this, to facilitate method chaining- 另请参阅:
-
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.
- 参数:
sMaxAge- the maximum time the response should be cached- 返回:
this, to facilitate method chaining- 另请参阅:
-
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 revalidatedunit- the time unit of thestaleWhileRevalidateargument- 返回:
this, to facilitate method chaining- 另请参阅:
-
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- 返回:
this, to facilitate method chaining- 另请参阅:
-
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.
- 参数:
staleIfError- the maximum time the response should be used when errors are encounteredunit- the time unit of thestaleIfErrorargument- 返回:
this, to facilitate method chaining- 另请参阅:
-
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.
- 参数:
staleIfError- the maximum time the response should be used when errors are encountered- 返回:
this, to facilitate method chaining- 另请参阅:
-
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-agedirective is strongly advised to enforce the actual freshness lifetime.- 返回:
this, to facilitate method chaining- 从以下版本开始:
- 4.0
- 另请参阅:
-
getHeaderValue
Return the "Cache-Control" header value, if any.- 返回:
- the header value, or
nullif no directive was added
-
toHeaderValue
Return the "Cache-Control" header value.- 返回:
- the header value (potentially empty)
-
appendDirective
-
toString
-