打算将cache-control的各个值都试一遍,看看最终效果是否和预期一致。
先尝试max-age。其作用是:假如请求了服务器并在a时刻返回响应结果,则在max-age规定的秒数内,浏览器将不会发送对应的请求到服务器,数据由缓存直接返回;超过这一时间段才进一步由服务器决定是返回新数据还是仍由缓存提供。
设置max-age的方式是tomcat的filter。
package itims;
public class TestHTTP implements Filter{
private static transient Log logger = LogFactory.getLog(TestHTTP.class);
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletrequest,
ServletResponse servletresponse, FilterChain filterchain)
throws IOException, ServletException {
filterchain.doFilter(servletrequest, servletresponse);
if (servletrequest instanceof HttpServletRequest && servletresponse instanceof HttpServletResponse) {
HttpServletRequest request = (HttpServletRequest) servletrequest;
HttpServletResponse response = (HttpServletResponse) servletresponse;
logger.info("request.getRequestURI:"+request.getRequestURI());
response.setHeader("Cache-Control", "max-age=" + cacheTime);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
web.xml,只针对js和css文件进行设置。
<filter>
<filter-name>cache-filter</filter-name>
<filter-class>itims.TestHTTP</filter-class>
<init-param>
<param-name>cache-time</param-name>
<param-value>30</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>cache-filter</filter-name>
<url-pattern>*.js</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>cache-filter</filter-name>
<url-pattern>*.css</url-pattern>
</filter-mapping>
httpTest.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = (String) request.getContextPath();
%>
<html>
<head>
<!--
<meta http-equiv="refresh" content="5;url=http://localhost:8088/itims/testRRD.action">
-->
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript" src="<%=path%>/widget/jquery/jquery-1.3.2.min.js"></script>
<title>httpTest</title>
</head>
<body>
<p>
success!
</p>
</body>
</html>
在浏览器内输入httpTest.jsp,则抓取的请求/响应信息如下:
http://localhost:8088/itims/widget/jquery/jquery-1.3.2.min.js
GET /itims/widget/jquery/jquery-1.3.2.min.js HTTP/1.1
Host: localhost:8088
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:6.0) Gecko/20100101 Firefox/6.0
Accept: */*
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Referer: http://localhost:8088/itims/springTest.jsp
Cookie: JSESSIONID=4B83B20F13BBCD37D1C0FB406807FA75
If-Modified-Since: Sun, 28 Aug 2011 13:06:44 GMT
If-None-Match: W/"57254-1314536804640"
HTTP/1.1 304 Not Modified
Server: Apache-Coyote/1.1
Etag: W/"57254-1314536804640"
Cache-Control: max-age=30
Date: Sun, 28 Aug 2011 13:19:35 GMT
jquery-1.3.2.min.js的max-age被设置为30秒。即30秒内,无论用户在浏览器地址栏上敲多少次回车,后台也不应该打印日志,而我这个信息抓取工具也不应该抓取到任何信息。事实的确如此。注意,不同的操作方式影响最终结果,原因是刷新和强制刷新会额外发送cache-control信息。
f5或刷新按钮:cache-control:max-age=0
ctrl+f5:cache-control:no-cache
pragma:no-cache
疑难杂症:
1、为什么把缓存清干净并访问页面后,再次通过按回车访问页面时,浏览器却不再发送请求了,而按f5又可以?
答:观察响应信息可知,把缓存清干净后cache-control的设置消失了,原因不明。如果按回车,则此时缓存永远都起作用,自然不用访问服务器;而按f5则是强制访问,且此时cache-control的设置又能出现,所以之后所有的访问都遵循cache-control的设置。
分享到:
相关推荐
dio-http缓存 Dio-http-cache是的缓存库 ,就像Android中的一样。 Dio-http-cache使用作为... 为请求设置maxAge: Dio (). get ( "http://www.google.com" , options : buildCacheOptions ( Duration (days : 7 )
连接缓存控制2 连接中间件以轻松处理缓存控制 用法 要使用中间件,只需导入withCacheControl函数并为所需的一条或多条路线创建中间件。 const withCacheControl = require ( 'connect-cache-control2' ) . ...
ring.middleware.browser-caching ring 中间件,为浏览器提供可配置的缓存控制标头。用法将此添加到您的 Leiningen project.clj依赖项中: 要使用,只需提供一个文件类型的映射,其中包含要为Cache-Control: max-age...
max-age=xxx: 缓存的内容将在 xxx 秒后失效 no-cache: 需要使用对比缓存来验证缓存数据 no-store: 所有内容都不会缓存 4.1、Cache-Control HTTP请求报文 4、HTTP请求报文头属性 一个代表响应服务端资源(如页面)...
该代码使用以下HTTP概念: 使用Cache-Control标头,使用伪指令(如Public , Private来确定哪些HTTP响应是可缓存的,哪些不是使用诸如Max-Age和S-Maxage等指令的Cache-Control标S-Maxage确定可以将HTTP响应缓存多长...
而如果指定了max-age值,那么在此值内的时间里就不会重新访问服务器,例如:Cache-control: max-age=5(表示当访问此网页后的5秒内再次访问不会去服务器) (2)、在地址栏回车值为private或must-revalidate则只有第一...
缓存头控制 ... // Sets `Cache-Control` header to `public, max-age=600` and `Expires` too // 'minute' -> 10 minutes -> 600 seconds setCacheHeader ( res , 'minute' ) ; next ( ) ; } )
您可以使用3个可配置选项实例化缓存: maxAge , maxStaleness和fsCachePath 。 maxAge :缓存项过期之前的持续时间(以毫秒为单位) maxStaleness :持续提供过期缓存项的持续时间(以毫秒为单位) fsCachePath ...
安装composer require nomenjanahary/cache-control配置storage_cache_control : exclude_status : - " 5xx " - " 4xx " default_cache : maxAge : 3600 public : true # merge, replace override_strategy : merge ...
缓存的思想可以应用在软件分层的各个层面。它是一种内部机制,对外界而言,是不可...另外Browser也有缓存(如IE)这个大家也都知道(实现在 web server 上的缓存机制)越上层的缓存效果越好,越底层的缓存影响越深远
先来说一下缓存的优点 减少了冗余的数据传递,节省宽带流量 减少了服务器的负担,大大提高了网站性能 加快了客户端加载网页的速度 这也...max-age=n :缓存内容在n秒后过期 no-cache:需要协商缓存验证数据 no-store
表达积极的缓存 一种激进但听话的高速缓存中间件。 特征 即插即用 内置TypeScript支持 多个数据存储(内存和Redis... maxAge : 3600 } ) . middleware ) ; app . get ( "/hello" , ( req , res ) => { res . json ( {
待办事项清单 处理caddyfile指令 将缓存存储到后端 使用缓存的响应 不缓存重定向 缓存头 请勿打扰已登录的活动 提供清除ACL指令 支持Cache-Control标头s-maxage指令后端支持 映射(Golang类型)-设置内存限制 处理...
Redis 加 LRU 缓存 该模块用作缓存中间件,它为使用 lru 缓存插件和 redis 提供了辅助方法以实现持久化和集群化。 安装 $ npm install redis-plus-lru-... } , maxAge : 1000 * 60 * 60 } , redis : { port : 6379
用于管理您的客户端缓存,是IndexedDB微小封装支持版本控制和max age
网页的缓存是由HTTP消息头中的“Cache-control”来控制的,常见的取值有private、no-cache、max-age、must-revalidate等,默认为private。其作用根据不同的重新浏览方式分为以下几种情况:(1) 打开新窗口值为...
Koa简单静态用于Koa的简单缓存静态文件服务器 与koa-static-cache相似,但具有不同的选项,更好的API和TypeScript。 特征: 缓存:预加载文件并... cwd ( )} ) )选件dir: string —您要提供的目录maxAge?: number = 0
缓存控制头是这样设置的: Cache-Control: public, max-age=31536000这是近一年浏览器缓存中文件的推荐处理方式,也是Google推荐的。 使用示例 package main import ( "github.com/theosomefactory/goji-static" ...
该框架已经在使用maxAge选项支持缓存,但是这种缓存是最弱的一种。 依靠HTTP 304状态代码将无法避免向服务器询问资源是否仍然“新鲜”的往返过程。 而且,这仅在静态资源上有效。 可以使用的最好的(虽然更难控制)...