Improve HTTP caching flexiblity

This commit improves HTTP caching defaults and flexibility in
Spring MVC.

1) Better default caching headers

The `WebContentGenerator` abstract class has been updated with
better HTTP defaults for HTTP caching, in line with current
browsers and proxies implementation (wide support of HTTP1.1, etc);
depending on the `setCacheSeconds` value:

* sends "Cache-Control: max-age=xxx" for caching responses and
do not send a "must-revalidate" value by default.
* sends "Cache-Control: no-store" or "Cache-Control: no-cache"
in order to prevent caching

Other methods used to set specific header such as
`setUseExpiresHeader` or `setAlwaysMustRevalidate` are now deprecated
in favor of `setCacheControl` for better flexibility.
Using one of the deprecated methods re-enables previous HTTP caching
behavior.

This change is applied in many Handlers, since
`WebContentGenerator` is extended by `AbstractController`,
`WebContentInterceptor`, `ResourceHttpRequestHandler` and others.

2) New CacheControl builder class

This new class brings more flexibility and allows developers
to set custom HTTP caching headers.

Several strategies are provided:

* `CacheControl.maxAge(int)` for caching responses with a
"Cache-Control: max-age=xxx" header
* `CacheControl.noStore()` prevents responses from being cached
with a "Cache-Control: no-store" header
* `CacheControl.noCache()` forces caches to revalidate the cached
response before reusing it, with a "Cache-Control: no-store" header.

From that point, it is possible to chain method calls to craft a
custom CacheControl instance:

```
CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS)
    .cachePublic().noTransform();
```

3) Configuring HTTP caching in Resource Handlers

On top of the existing ways of configuring caching mechanisms,
it is now possible to use a custom `CacheControl` to serve
resources:

```
@Configuration
public class MyWebConfig extends WebMvcConfigurerAdapter {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS);
    registry.addResourceHandler("/resources/**)
            .addResourceLocations("classpath:/resources/")
            .setCacheControl(cc);
  }
}
```

or

```
<mvc:resources mapping="/resources/**" location="classpath:/resources/">
  <mvc:cachecontrol max-age="3600" cache-public="true"/>
</mvc:resources>
```

Issue: SPR-2779, SPR-6834, SPR-7129, SPR-9543, SPR-10464
This commit is contained in:
Brian Clozel
2015-03-11 11:19:52 +01:00
parent 953608ec49
commit 38f32e3816
21 changed files with 818 additions and 192 deletions

View File

@@ -477,6 +477,86 @@
<xsd:attribute name="cache-name" type="xsd:string" use="optional"/>
</xsd:complexType>
<xsd:complexType name="cachecontrol">
<xsd:annotation>
<xsd:documentation source="org.springframework.web.cache.CacheControl"><![CDATA[
Generates "Cache-Control" HTTP response headers.
]]></xsd:documentation>
</xsd:annotation>
<xsd:attribute name="must-revalidate" type="xsd:boolean" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
Adds a "must-revalidate" directive in the Cache-Control header.
This indicates that caches should revalidate the cached response when it's become stale.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="no-cache" type="xsd:boolean" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
Adds a "no-cache" directive in the Cache-Control header.
This indicates that caches should always revalidate cached response with the server.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="no-store" type="xsd:boolean" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
Adds a "no-store" directive in the Cache-Control header.
This indicates that caches should never cache the response.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="no-transform" type="xsd:boolean" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
Adds a "no-transform" directive in the Cache-Control header.
This indicates that caches should never transform (i.e. compress, optimize) the response content.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="cache-public" type="xsd:boolean" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
Adds a "public" directive in the Cache-Control header.
This indicates that any cache MAY store the response.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="cache-private" type="xsd:boolean" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
Adds a "private" directive in the Cache-Control header.
This indicates that the response is intended for a single user and may not be stored by shared caches.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="proxy-revalidate" type="xsd:boolean" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
Adds a "proxy-revalidate" directive in the Cache-Control header.
This directive has the same meaning as the "must-revalidate" directive, except it only applies to shared caches.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="max-age" type="xsd:int" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
Adds a "max-age" directive in the Cache-Control header.
This indicates that the response should be cached for the given number of seconds.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="s-maxage" type="xsd:int" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
Adds a "s-maxage" directive in the Cache-Control header.
This directive has the same meaning as the "max-age" directive, except it only applies to shared caches.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:element name="resources">
<xsd:annotation>
<xsd:documentation
@@ -487,6 +567,7 @@
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="cachecontrol" type="cachecontrol" minOccurs="0" maxOccurs="1"/>
<xsd:element name="resource-chain" type="resource-chain" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="mapping" use="required" type="xsd:string">