Class CacheControl
Cache-Control header values.
The Cache-Control header controls how browsers and intermediate caches store and
serve HTTP responses. This class provides a type-safe, discoverable API for building
cache directives without manually constructing raw header strings.
Usage:
// Build a custom cache policy
CacheControl cache = new CacheControl();
cache.setPublic();
cache.setMaxAge(86400);
cache.mustRevalidate();
response.setHeader("Cache-Control", cache.toString());
// "public, max-age=86400, must-revalidate"
// Or use static factory methods for common patterns
response.setHeader("Cache-Control", CacheControl.staticResources().toString());
// "public, max-age=31536000, immutable"
- Since:
- 2.0
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionSets theimmutabledirective.Sets themust-revalidatedirective.noCache()Sets theno-cachedirective.noStore()Sets theno-storedirective.Sets theno-transformdirective.static CacheControlCreates aCacheControlconfigured to prevent caching entirely.Sets theproxy-revalidatedirective.static CacheControlCreates aCacheControlconfigured to require revalidation on every use.setMaxAge(int seconds) Sets themax-agedirective.Sets theprivatedirective.Sets thepublicdirective.setSMaxAge(int seconds) Sets thes-maxagedirective.setStaleIfError(int seconds) Sets thestale-if-errordirective.setStaleWhileRevalidate(int seconds) Sets thestale-while-revalidatedirective.static CacheControlCreates aCacheControlconfigured for long-lived static resources.toString()Returns the Cache-Control header value as a formatted string.
-
Constructor Details
-
CacheControl
public CacheControl()Creates a new empty Cache-Control builder.
-
-
Method Details
-
setPublic
Sets thepublicdirective.Indicates that the response may be cached by any cache, including shared caches such as CDNs and proxy servers.
- Returns:
- this instance for method chaining
-
setPrivate
Sets theprivatedirective.Indicates that the response is intended for a single user and must not be stored by shared caches. A private cache (such as the browser cache) may store the response.
- Returns:
- this instance for method chaining
-
noCache
Sets theno-cachedirective.Indicates that caches must revalidate the response with the origin server before each use, even if the cached response has not expired. The response may still be stored by caches.
- Returns:
- this instance for method chaining
-
noStore
Sets theno-storedirective.Indicates that no cache of any kind (private or shared) should store the response. This is the strongest cache prevention directive.
- Returns:
- this instance for method chaining
-
noTransform
Sets theno-transformdirective.Indicates that intermediary caches or proxies must not modify the response body,
Content-Encoding,Content-Range, orContent-Typeheaders.- Returns:
- this instance for method chaining
-
mustRevalidate
Sets themust-revalidatedirective.Indicates that once a cached response becomes stale, it must not be used without successful revalidation with the origin server.
- Returns:
- this instance for method chaining
-
proxyRevalidate
Sets theproxy-revalidatedirective.Similar to
must-revalidate, but applies only to shared caches (such as proxies and CDNs). Private caches may continue to use stale responses.- Returns:
- this instance for method chaining
-
immutable
Sets theimmutabledirective.Indicates that the response body will not change over time. This prevents browsers from sending conditional revalidation requests (e.g.,
If-None-Match) during the freshness lifetime. Best used with longmax-agevalues for versioned static resources.- Returns:
- this instance for method chaining
-
setMaxAge
Sets themax-agedirective.Specifies the maximum amount of time in seconds that a response is considered fresh. After this time elapses, the cache must revalidate the response with the origin server.
- Parameters:
seconds- the maximum age in seconds (e.g., 86400 for one day, 31536000 for one year)- Returns:
- this instance for method chaining
-
setSMaxAge
Sets thes-maxagedirective.Specifies the maximum age for shared caches (CDNs and proxies), overriding the
max-agedirective for those caches. Private caches still usemax-age.- Parameters:
seconds- the shared cache maximum age in seconds- Returns:
- this instance for method chaining
-
setStaleWhileRevalidate
Sets thestale-while-revalidatedirective.Indicates that the cache may serve a stale response while it revalidates the response in the background. The value specifies how many seconds past the
max-agethe cache may continue serving the stale response.- Parameters:
seconds- the stale-while-revalidate window in seconds- Returns:
- this instance for method chaining
-
setStaleIfError
Sets thestale-if-errordirective.Indicates that the cache may serve a stale response if the origin server responds with an error (5xx status). The value specifies how many seconds past the
max-agethe stale response may be used when errors occur.- Parameters:
seconds- the stale-if-error window in seconds- Returns:
- this instance for method chaining
-
staticResources
Creates aCacheControlconfigured for long-lived static resources.Returns a policy of
public, max-age=31536000, immutablewhich is suitable for versioned static assets such as CSS files, JavaScript bundles, and images whose URLs change when the content changes (via cache-busting query parameters or hashed filenames).- Returns:
- a new CacheControl instance configured for static resources
-
preventCaching
Creates aCacheControlconfigured to prevent caching entirely.Returns a policy of
no-storewhich prevents any cache from storing the response. Use this for sensitive data that should never be cached.- Returns:
- a new CacheControl instance configured to prevent all caching
-
revalidate
Creates aCacheControlconfigured to require revalidation on every use.Returns a policy of
no-cachewhich allows caches to store the response but requires revalidation with the origin server before each use.- Returns:
- a new CacheControl instance configured to require revalidation
-
toString
Returns the Cache-Control header value as a formatted string.Produces a comma-separated string of all configured directives, suitable for use as the value of a
Cache-ControlHTTP response header.
-