利用last-modify 和 If-Modified-Since 做页面缓存

  • Post author:
  • Post category:其他


在http中

Last-Modified



If-Modified-Since

都是用于记录页面最后修改时间的 HTTP 头信息,

注意,在这


Last-Modified 是由服务器往客户端发送的HTTP 头


另一个


If-Modified-Since是由客户端往服务器发送的头



那么它们的到底是怎么玩的呢。。。。。用我项目中的一个实例来说明把!


(基金项目) 由于基金净值

每天才更新一次

,所以在项目启动的时候到数据库中拿一次数据,然后

缓存这个list,同时也把缓存这个list的date保存起来

,然后在通过定时器,

定时在某个时间点去更新这个list,更新缓存date



由于list每天才更新一次,那么前端页面输出这个list的数据也要一天才会变化一次,为了减缓服务器的压力,页面也能缓存起来。


个人理解原理:










可以看到,再次请求本地存在的 cache 页面时,客户端会通过 If-Modified-Since 头将先前服务器端发过来的


Last-Modified 最后修改时间戳发送回去,这是为了让服务器端进行验证,通过这个时间戳判断客户端的页面


是否是最新的,如果不是最新的,则返回新的内容,如果是最新的,则 返回 304 告诉客户端其本地 cache 的页面


是最新的,于是客户端就可以直接从本地加载页面了,这样在网络上传输的数据就会大大减少,同时也减轻了服务器的负担。

在看看源码:

核心方法代码



  1. /**
  2. *
  3. * @param lastModifiedTimestamp 缓存list的时间戳
  4. * @return 是否大于缓存list的时间戳
  5. */
  6. public boolean checkNotModified(long lastModifiedTimestamp) {
  7. if (lastModifiedTimestamp >=

    0

    && !this.notModified &&
  8. (this.response == null || !this.response.containsHeader(HEADER_LAST_MODIFIED))) {
  9. long ifModifiedSince = getRequest().getDateHeader(HEADER_IF_MODIFIED_SINCE);//取得客户端传上来的时间戳  if-modified
  10. this.notModified = (ifModifiedSince >= (lastModifiedTimestamp /

    1000

    *

    1000

    ));
  11. if (this.response != null) {
  12. if (this.notModified) {//大于
  13. this.response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); //往响应流中状态设为

    305
  14. }
  15. else {
  16. this.response.setDateHeader(HEADER_LAST_MODIFIED, lastModifiedTimestamp); //把最新的缓存时间戳赋给响应流中的 Last-Modified
  17. }
  18. }
  19. }
  20. return this.notModified;
  21. }

上面这个类在  org.springframework.web.context.request.ServletWebRequest

然后在controller中调用:

Java代码




  1. @RequestMapping

    (method = { RequestMethod.GET, RequestMethod.POST })

  2. public

    HashMap getFundNavInfoList(WebRequest webRequest,HttpServletRequest request,FundNavInput in)

    throws

    Exception {

  3. //check is modify  true代表客户端是最新的,直接返回,客户端根据响应流中304取本地缓存

  4. if

    (webRequest.checkNotModified(lastModifyTime)){

  5. return


    null

    ;
  6. }

  7. //………后面是代码不重要。。。。
	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST })
	public HashMap getFundNavInfoList(WebRequest webRequest,HttpServletRequest request,FundNavInput in) throws Exception {
		//check is modify  true代表客户端是最新的,直接返回,客户端根据响应流中304取本地缓存
		if(webRequest.checkNotModified(lastModifyTime)){
			return null;
		}
		//.........后面是代码不重要。。。。
}

总结:

这种方式适合数据更新频率慢,如果是实时数据,这种方式就不行了。。。。。。

欢迎大伙指出不足地方!。。。

原文链接:

http://xw-qixia.iteye.com/blog/736963