Issue
Do "max-age" with a positive value and "no-cache" make sense? The documentation doesn't mention whether they are mutually exclusive https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
However org.springframework.http.CacheControl doesn't allow me to create an instance of it which would contain both of them. It has an interface that allows you to create an instance of CacheControl that would have either "max-age" or "no-cache".
public static CacheControl noCache() {
CacheControl cc = new CacheControl();
cc.noCache = true;
return cc;
}
public static CacheControl maxAge(Duration maxAge) {
CacheControl cc = new CacheControl();
cc.maxAge = maxAge;
return cc;
}
There is no no-static method for adding "no-cache" or "max-age" after instance creation. Is it on purpose and correct?
Solution
No, it doesn't make sense to use a max-age
with a positive value when you use no-cache
. Since no-cache
means "that the response MUST NOT be used to satisfy a subsequent request without successful validation on the origin server", the freshness time set by max-age
won't have any effect.
There's also no reason—at least according to the standard—to use max-age=0
when using no-cache
, though some people do it anyway, presumably due to a perceived benefit to backward compatibility.
Answered By - Kevin Christopher Henry
Answer Checked By - Timothy Miller (JavaFixing Admin)