首页 > 出现http 200 from cache的诡异问题

出现http 200 from cache的诡异问题

所用浏览器:Pc端chrome、手机里的各种浏览器
访问同一个这样的url地址:http://xxx.aaa.bbb.com/ship.html

在测试环境中:有的同学访问了这个地址,再次访问时,页面仍然显示的是上次访问该页面时的数据,并不是最新数据。查看其http,有几个重要的参数如下:200、from cache。而有的同学每次访问这个地址,页面都是最新数据,查看其http的重要几个参数,没有出现from cache。

from cache 懂些,搞不懂的是访问同一台服务器为什么有的同学访问时出现from cache,而有的同学访问时都是从服务器加载最新的页面数据。这跟什么有关,什么影响了它?

注:


如果服务器没有明确定义缓存控制,不同的客户端可能会有不同的缓存机制。
如果你希望用户每次获取最新数据可以这样设置 meta tag :

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

设置meta tag只对html页面有效,更好的方法是在 http 响应头中设置缓存控制:

Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

php设置 http 响应头的代码:

header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.

使用缓存有利于加快页面加载速度和减少资源消耗,你应该根据具体情况调整缓存策略。

参考

http://stackoverflow.com/ques...
http://stackoverflow.com/ques...

【热门文章】
【热门文章】