作了一个demo,放到码云上了,有兴趣的点我。html
一个良好的HTTP缓存策略能够显著地增进web应用的性能和其客户端的体验。主要使用”Cache-Control” HTTP response header来完成,配合conditional headers例如”Last-Modified”和”ETag”。git
“Cache-Control” HTTP response header 会建议私有缓存(如浏览器)和公开缓存(如代理)如何缓存HTTP response以供未来复用。web
“ETag” (entity tag) 是由兼容HTTP/1.1 的web server返回的HTTP response header,用于判断给定URL的内容的改变。它可被认为是”Last-Modified” header的更复杂的继承者。 当服务器返回了一个带有ETag header的representation时,客户端能够在后续的GETs中使用该header -- 在一个”If-None_Match” header中。 若是内容没有改变,server会返回 “304: Not Modified”。spring
本部分描述了在一个Spring Web MVC 应用中配置HTTP caching的可行方式。浏览器
一、 Cache-Control HTTP header缓存
Spring Web MVC支持不少使用环境和方式来配置一个应用的Cache-Control headers。RFC 7234 Section 5.2.2详细的描述了该header以及其可能的directives,有几种不一样的方式来实现经常使用的案例。服务器
Spring Web MVC 在其几个APIs中使用了一个配置惯例:setCachePeriod(int seconds) :网络
CacheControl 构造类简单的描述了可用的 Cache-Control directives,能够更简单的build你本身的HTTP caching 策略。 一旦构造完毕,一个CacheControl实例能够在几个Spring Web MVC APIs中被用做参数。并发
// Cache for an hour - "Cache-Control: max-age=3600" CacheControl ccCacheOneHour = CacheControl.maxAge(1, TimeUnit.HOURS); // Prevent caching - "Cache-Control: no-store" CacheControl ccNoStore = CacheControl.noStore(); // Cache for ten days in public and private caches, // public caches should not transform the response // "Cache-Control: max-age=864000, public, no-transform" CacheControl ccCustom = CacheControl.maxAge(10, TimeUnit.DAYS) .noTransform().cachePublic();
二、支持静态资源的HTTP caching supportmvc
静态资源应该使用合适的 Cache-Control以及conditional headers来优化性能。 配置一个ResourceHttpRequestHandler来服务静态资源,不只会自然地写入 Last-Modified headers (经过读取文件的metadata),还会写入 Cache-Control headers -- 若是正确的配置了。
你能够设置ResourceHttpRequestHandler的cachePeriod attribute 或者使用一个CacheControl实例,它们均可以支持更多特定的directives:
@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/public-resources/") .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic()); } }
在XML中:
<mvc:resources mapping="/resources/**" location="/public-resources/"> <mvc:cache-control max-age="3600" cache-public="true"/> </mvc:resources>
三、在Controllers中支持 Cache-Control、ETag、Last-Modified response headers
Controllers能够支持 Cache-Control、ETag、和/或 If-Modified-Since HTTP request;若是response设置了一个Cache-Control header,很是推荐这样作。这会计算给定request的一个lastModified long 和/或 一个Etag value,将其与 If-Modified-Since request header value做比较,并可能返回一个status code 304 (Not Modified) 的response。
如同在“Using HttpEntity”部分描述的,controllers可使用HttpEntity 类型来与request/response 交互。
返回ResponseEntity的Controllers 能够在responses中以以下方式包含HTTP caching information:
@GetMapping("/book/{id}") public ResponseEntity<Book> showBook(@PathVariable Long id) { Book book = findBook(id); String version = book.getVersion(); return ResponseEntity .ok() .cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS)) .eTag(version) // lastModified is also available .body(book); }
这样作,不只会在response中带有ETag和Cache-Control headers,还会 将response转换成 一个响应体为空的HTTP 304 Not Modified response -- 若是客户端发送的conditional headers 匹配Controller设置的caching information。
一个@RequestMapping method 可能也会但愿支持一样的行为。 能够这样作:
@RequestMapping public String myHandleMethod(WebRequest webRequest, Model model) { long lastModified = // 1. application-specific calculation if (request.checkNotModified(lastModified)) { // 2. shortcut exit - no further processing necessary return null; } // 3. or otherwise further request processing, actually preparing content model.addAttribute(...); return "myViewName"; }
这里有两个关键元素: 调用 request.checkNotModified(lastModified)、返回null。前者在其返回true以前设置了合适的response status和headers。后者,结合前者,会让Spring MVC 再也不更进一步地处理该request。
注意,这里有3 个变体:
当接收 conditional GET/HEAD requests时, checkNotModified 会检查resource是否没有被修改;若是没有,它会返回一个HTTP 304 Not Modified response。
而在POST/PUT/DELETE requests时,checkNotModified 会检查resouce是否没有被修改;若是有修改,会返回一个HTTP 409 Precondition Failed response 来阻止并发修改。
四、Shallow ETag support
对于ETags的支持是由Servlet filter ShallowEtagHeaderFilter提供的。 这是一个简单的Servlet Filter,所以可与任何web框架结合使用。 ShallowEtagHeaderFilter filter 会建立 shallow ETags (与deep ETags相对,后面有讲)。 该filter会缓存被渲染的JSP的内容(或其余内容),生成一个MD5 hash,并将其返回做为response的一个ETag header。等下次客户端请求一样的资源时,它会使用该hash做为 If-None-Match value。 该filter会侦测到它,从新渲染视图,而后比较两者的hash。 若是相等,返回304。
注意,这种策略节省了网络带宽,而非CPU,由于仍然须要为每次request计算response。而controller级别的其余策略(上面讲过的),则既能节省带宽,又能避免计算。
该filter有一个writeWeakETag parameter,是用来配置该filter写入Weak ETags的,就像这样:W/"02a2d595e6ed9a0b24f027f2b63b134d6"
, 如同 RFC 7232 Section 2.3 中定义的同样。
你能够在web.xml中配置 ShallowEtagHeaderFilter:
<filter> <filter-name>etagFilter</filter-name> <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class> <!-- Optional parameter that configures the filter to write weak ETags <init-param> <param-name>writeWeakETag</param-name> <param-value>true</param-value> </init-param> --> </filter> <filter-mapping> <filter-name>etagFilter</filter-name> <servlet-name>petclinic</servlet-name> </filter-mapping>
或者在Servlet 3.0+ 环境中:
public class MyWebAppInitializer extends AbstractDispatcherServletInitializer { // ... @Override protected Filter[] getServletFilters() { return new Filter[] { new ShallowEtagHeaderFilter() }; } }
See Section 22.15, “Code-based Servlet container initialization” for more details.
官方文档连接:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-caching