“Cache-control”与java配置

来源:互联网 发布:域名注册管理局 编辑:程序博客网 时间:2024/06/06 05:00

网页的缓存是由HTTP消息头中的“Cache-control”来控制的,常见的取值有private、no-cache、max-age、must-revalidate等,默认为private。其作用根据不同的重新浏览方式分为以下几种情况:
(1) 打开新窗口
如果指定cache-control的值为private、no-cache、must-revalidate,那么打开新窗口访问时都会重新访问服务器。而如果指定了max-age值,那么在此值内的时间里就不会重新访问服务器,例如:
Cache-control: max-age=5
表示当访问此网页后的5秒内再次访问不会去服务器
(2) 在地址栏回车
如果值为private或must-revalidate(和网上说的不一样),则只有第一次访问时会访问服务器,以后就不再访问。如果值为no-cache,那么每次都会访问。如果值为max-age,则在过期之前不会重复访问。
(3) 按后退按扭
如果值为private、must-revalidate、max-age,则不会重访问,而如果为no-cache,则每次都重复访问
(4) 按刷新按扭
无论为何值,都会重复访问

当指定Cache-control值为“no-cache”时,访问此页面不会在Internet临时文章夹留下页面备份。
另外,通过指定“Expires”值也会影响到缓存。例如,指定Expires值为一个早已过去的时间,那么访问此网时若重复在地址栏按回车,那么每次都会重复访问:
Expires: Fri, 31 Dec 1999 16:00:00 GMT

在ASP中,可以通过Response对象的Expires、ExpiresAbsolute属性控制Expires值;通过Response对象的CacheControl属性控制Cache-control的值,例如:
Response.ExpiresAbsolute = #2000-1-1# ' 指定绝对的过期时间,这个时间用的是服务器当地时间,会被自动转换为GMT时间
Response.Expires = 20  ' 指定相对的过期时间,以分钟为单位,表示从当前时间起过多少分钟过期。
Response.CacheControl = "no-cache" 
Expires值是可以通过在Internet临时文件夹中查看临时文件的属性看到的。

数据包中的格式: 

Cache-Control: cache-directive 

cache-directive可以为以下: 

request时用到: 

| "no-cache" 
| "no-store" 
| "max-age" "=" delta-seconds 
| "max-stale" [ "=" delta-seconds ] 
| "min-fresh" "=" delta-seconds 
| "no-transform" 
| "only-if-cached" 
| "cache-extension" 

response时用到: 

| "public" 
| "private" [ "=" <"> field-name <"> ] 
| "no-cache" [ "=" <"> field-name <"> ] 
| "no-store" 
| "no-transform" 
| "must-revalidate" 
| "proxy-revalidate" 
| "max-age" "=" delta-seconds 
| "s-maxage" "=" delta-seconds 
| "cache-extension" 

部分说明:

根据是否可缓存分为 
Public  指示响应可被任何缓存区缓存。 
Private  指示对于单个用户的整个或部分响应消息,不能被共享缓存处理。这允许服务器仅仅描述当用户的 
部分响应消息,此响应消息对于其他用户的请求无效。 
no-cache  指示请求或响应消息不能缓存(HTTP/1.0用Pragma的no-cache替换) 
根据什么能被缓存 
no-store  用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用缓存。 
根据缓存超时 
max-age  指示客户机可以接收生存期不大于指定时间(以秒为单位)的响应。 
min-fresh  指示客户机可以接收响应时间小于当前时间加上指定时间的响应。 
max-stale  指示客户机可以接收超出超时期间的响应消息。如果指定max-stale消息的值,那么客户机可以 
接收超出超时期指定值之内的响应消息。 
Expires 表示存在时间,允许客户端在这个时间之前不去检查(发请求),等同max-age的 
效果。但是如果同时存在,则被Cache-Control的max-age覆盖。 
格式: 
Expires = "Expires" ":" HTTP-date 
例如 
Expires: Thu, 01 Dec 1994 16:00:00 GMT (必须是GMT格式) 
通过HTTP的META设置expirescache-control

Html代码
  1. <meta http-equiv="Cache-Control" content="max-age=7200" />  
  2. <meta http-equiv="Expires" content="Mon, 20 Jul 2009 23:00:00 GMT" />  
[html] view plaincopy
  1. <meta http-equiv="<span class="hilite1">Cache-Control</span>" content="max-age=7200" /> <meta http-equiv="<span class="hilite2">Expires</span>" content="Mon, 20 Jul 2009 23:00:00 GMT" />  
上述设置仅为举例,实际使用其一即可。这样写的话仅对该网页有效,对网页中的图片或其他请求无效,并不会做任何cache。 
这样客户端的请求就多了,尽管只是检查Last-modified状态的东西,但是请求一多对浏览速度必定有影响。 
如果要对文件添加cache可以通过apache的mod_expire模块,写法为 
<IfModule mod_expires.c> 
ExpiresActive On 
ExpiresDefault "access plus 1 days" 
</IfModule> 
记得ExpiresActive设为On,我起先没设置On,似乎怎样YSlow都查不到缓存机制。这样添加的话就是默认所有的。 
如果要针对个别MIME类型则可以: 
ExpiresByType image/gif "access plus 5 hours 3 minutes" 
见 Apache Module mod_expires 
另外,当点击浏览器上的刷新,客户端发送的请求中均是max-age=0,表示validate操作,发送请求到服务器 
要求检查cache,再更新cache,一般得到的是304 Not Modified,表示没变动。
项目中使用过滤器来设置网页的缓存

FilterConfig fc; 

    
public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) 
throws IOException, ServletException {
        HttpServletResponse response 
= (HttpServletResponse) res;
        
// set the provided HTTP response parameters
        for (Enumeration e = fc.getInitParameterNames(); e.hasMoreElements();) {
            String headerName 
= (String) e.nextElement();
            response.addHeader(headerName, fc.getInitParameter(headerName));
        }

        
// pass the request/response on
        chain.doFilter(req, response);
    }
 

配置文件的配置:
<filter>
        
<filter-name>NoCache</filter-name>
        
<filter-class>filter.CacheFilter</filter-class>
        
<init-param>
            
<param-name>Cache-Control</param-name>
            
<param-value>no-cache, must-revalidate</param-value>
        
</init-param>
    
</filter>
    
<filter>
        
<filter-name>CacheForWeek</filter-name>
        
<filter-class>filter.CacheFilter</filter-class>
        
<init-param>
            
<param-name>Cache-Control</param-name>
            
<param-value>max-age=604800</param-value>
        
</init-param>
    
</filter>

    
<filter-mapping>
        
<filter-name>CacheForWeek</filter-name>
        
<url-pattern>*.js</url-pattern>
    
</filter-mapping>
    
<filter-mapping>
        
<filter-name>CacheForWeek</filter-name>
        
<url-pattern>*.css</url-pattern>
    
</filter-mapping> 
    
<filter-mapping>
        
<filter-name>CacheForWeek</filter-name>
        
<url-pattern>*.gif</url-pattern>
    
</filter-mapping> 

上述设置保存了1周的缓存。