下午好, 我需要 Servlet 的结果始终由浏览器缓存。尝试放置相同的标题,如 http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js但结果仍然不是来自浏览器缓存(使用 FireBug 测试)。
我的代码:
response.setContentType("text/javascript");
response.setHeader("Last-Modified", "Mon, 15 Feb 2010 23:30:12 GMT");
response.setHeader("Date", "Tue, 28 Sep 2010 19:45:24 GMT");
response.setHeader("Expires", "Wed, 28 Sep 2021 19:45:24 GMT");
response.setHeader("Vary", "Accept-Encoding");
response.setHeader("X-Content-Type-Options", "nosniff");
response.setHeader("Cache-Control", "public, max-age=31536000");
response.setHeader("Age", "36");
我的标题( Firebug ):
HTTP/1.1 200 OK
Content-Type: text/javascript; charset=UTF-8
Last-Modified: Mon, 15 Feb 2010 23:30:12 GMT
Date: Sun, 10 Oct 2010 14:40:49 GMT
Expires: Mon, 10 Oct 2011 14:40:49 GMT
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
Server: sffe
Content-Encoding: gzip
Cache-Control: public, max-age=31536000
Content-Length: 46401
Age: 2
Googleapis header :
HTTP/1.1 200 OK
Content-Type: text/javascript; charset=utf-8
Last-Modified: Mon, 15 Feb 2010 23:30:12 GMT
Expires: Wed, 28 Sep 2021 19:45:24 GMT
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
Cache-Control: public, max-age=31536000
Age: 36
Content-Encoding: gzip
Date: Sun, 10 Oct 2010 14:32:06 GMT
Server: Google Frontend
Content-Length: 36838
所有 header 都相同,但顺序不同(即使代码 - 不应该,显然 GAE 颠倒了顺序)。 问题可能出在什么地方? 谢谢。
最佳答案
顺序并不重要。你有什么问题吗?理论上,如果 GAE 使用的 servletcontainer 使用 HashMap
,就会发生这种情况。在提交响应和本地 servlet 容器之前存储 header LinkedHashMap
或List<Pair>
.
至于响应缓存,你至少需要一个遥远的 future Expires
header 以及有效的 ETag
或Last-Modified
header 。 Cache-Control
和Age
没有必要。
关于java - GAE 和响应缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3900752/