之前深入搜索了多次,根据stackoverflow的回答进行一些总结(http://stackoverflow.com/questions/18148884/difference-between-no-cache-and-must-revalidate),目前看来这三种值的区别很模糊,但其实是有区别的(这里我们讨论的是HTTP /1.1的响应报文),先看看各自的释义(见: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1):

no-cache

If the no-cache directive does not specify a field-name, then a cache MUST NOT use the response to satisfy a subsequent request without successful revalidation with the origin server. This allows an origin server to prevent caching even by caches that have been configured to return stale responses to client requests.If the no-cache directive does specify one or more field-names, then a cache MAY use the response to satisfy a subsequent request, subject to any other restrictions on caching. However, the specified field-name(s) MUST NOT be sent in the response to a subsequent request without successful revalidation with the origin server. This allows an origin server to prevent the re-use of certain header fields in a response, while still allowing caching of the rest of the response.

亦即:no-cache的响应实际是可以存储在本地缓存中的,只是在与原始服务器进行新鲜度再验证之前,缓存不能将其提供给客户端使用。

must-revalidate

Because a cache MAY be configured to ignore a server's specified expiration time, and because a client request MAY include a max- stale directive (which has a similar effect), the protocol also includes a mechanism for the origin server to require revalidation of a cache entry on any subsequent use. When the must-revalidate directive is present in a response received by a cache, that cache MUST NOT use the entry after it becomes stale to respond to asubsequent request without first revalidating it with the origin server. (I.e., the cache MUST do an end-to-end revalidation every time, if, based solely on the origin server's Expires or max-age value, the cached response is stale.)The must-revalidate directive is necessary to support reliable operation for certain protocol features. In all circumstances an HTTP/1.1 cache MUST obey the must-revalidate directive; in particular, if the cache cannot reach the origin server for any reason, it MUST generate a 504 (Gateway Timeout) response.Servers SHOULD send the must-revalidate directive if and only if failure to revalidate a request on the entity could result in incorrect operation, such as a silently unexecuted financial transaction. Recipients MUST NOT take any automated action that violates this directive, and MUST NOT automatically provide an unvalidated copy of the entity if revalidation fails.Although this is not recommended, user agents operating under severe connectivity constraints MAY violate this directive but, if so, MUST explicitly warn the user that an unvalidated response has been provided. The warning MUST be provided on each unvalidated access, and SHOULD require explicit user confirmation.

亦即:含有must-revalidate的响应会被存储在本地缓存中,在后续请求时,该指令告知缓存:在事先没有与原始服务器进行再验证的情况下,不能提供这个对象的陈旧副本,但缓存仍然可以随意提供新鲜的副本。

max-age

When the max-age cache-control directive is present in a cached response, the response is stale if its current age is greater than the age value given (in seconds) at the time of a new request for that resource. The max-age directive on a response implies that the response is cacheable (i.e., "public") unless some other, more restrictive cache directive is also present.

亦即:max-age=xxx标识了该响应从服务器那边获取过来时,文档的处于新鲜状态的秒数,若max-age=0,则表示是一个立即过期的响应(直接标记为陈旧状态)。

这里比较下no-cache和must-revalidate的区别,个人觉得主要在于:

假设一个文档的缓存时间设置为10s,若指定no-cache,则它会强制浏览器(User Agent)必须先进行新鲜度再验证(注:不管该缓存是否新鲜),待服务器那边确认新鲜(304)后,方可使用缓存。

若指定must-revalidate,则浏览器会首先等待文档过期(超过10s),然后才去验证新鲜度(10s之前,都会直接使用缓存,不与服务器交互)。

那么这样一来,基本可以将 no-cache 与 must-revalidate, max-age=0 划等了,但这两者也有些细节上的区别,即:

在执行must-revalidate时,若浏览器第二次去请求服务器来做新鲜度验证,结果服务器挂了,无法访问,那么缓存需要返回一个504 Gateway Timeout的错误(这里应该是像nginx这样的代理来返回,若是浏览器如chrome,将直接是ERR_CONNECTION_REFUSED,即无法访问,连接被拒绝)。

而如果是no-cache,当验证新鲜度时,服务器扑街,则会照样使用本地缓存显示给用户(有的总比没的好,当然有可能显示的就是旧的文档了)。

所以must-revalidate用在对事务要求比较严苛的情况下使用(比如支付)。

【测试结果】

在chrome 52.0.2743.116 m下测试时,其实 no-cache 与 must-revalidate, max-age=0 的效果是一样的,都会返回无法访问,应该印证了https://tools.ietf.org/html/rfc7234#section-5.2.2.2这里对no-cache较新的定义。

另外两者在浏览器 Back/Forward 按键跳转时,实际是直接使用本地缓存的(不会访问服务器)。

no-cache、max-age=0、must-revalidate区别的更多相关文章

  1. $Django 虚拟环境,2.0、1.0路由层区别,Httprequest对象,视图层(fbv,cbv),文件上传

    1 虚拟环境:解决问题同一台机器上可以运行不同版本的django,  1 用pychanrm创建--->files-->newproject--->选择虚拟环境  2 setting ...

  2. 深入理解 '0' "0" '\0' 0 之间的区别

    看来基础还是很重要的,基础不扎实就难以学好c语言,就别说写出高质量的c语言代码了.今天,我就被这个问题折磨的不行了,哈哈,不过现在终于明白了‘\0’ ,‘0’, “0” 之间的区别了.困惑和快乐与你分 ...

  3. android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别

    android Activity类中的finish().onDestory()和System.exit(0) 三者的区别 Activity.finish() Call this when your a ...

  4. 老李分享:robotium3.6与4.0 later 的区别 1

    老李分享:robotium3.6与4.0 later 的区别   因为下载的直接是最新版本的robotium4.1版,这次碰到gridView问题时,发现网上有getCurrentListViews( ...

  5. localhost与127.0.0.1的区别 2

    localhost与127.0.0.1的区别localhost与127.0.0.1的区别是什么?相信有人会说是本地ip,曾有人说,用127.0.0.1比localhost好,可以减少一次解析.看来这个 ...

  6. mysql -h localhost和mysql -h 127.0.0.1的区别

    今天早上同事说MySQL root账号登录不上了.我试了一下 #mysql -u root -p 提示”Access denied for user ‘root’@’localhost’ (using ...

  7. [转帖]localhost与127.0.0.1的区别

    localhost与127.0.0.1的区别 https://www.cnblogs.com/hqbhonker/p/3449975.html 前段时间用PG的时候总有问题 当时没有考虑 localh ...

  8. 深入理解c语言——‘\0’ ,‘0’, “0” ,0之间的区别

    看来基础还是很重要的,基础不扎实就难以学好c语言,就别说写出高质量的c语言代码了.今天,我就被这个问题折磨的不行了,哈哈,不过现在终于明白了‘\0’ ,‘0’, “0” 之间的区别了.困惑和快乐与你分 ...

  9. jquery 中cache为true与false 的区别

    $.ajax({type: "get",cache: false,url: url,success: function (msg) { }}); cache为true与false ...

  10. Django day05 虚拟环境 django 2.0和django 1.0 路由层区别

    一:虚拟环境 创建虚拟环境一般有三种方式: 1)   File--->New Project--> 出现如下图,点击Project Interpreter:New Virtualenv e ...

随机推荐

  1. Selenium IDE整理

    安装 Step1: 下载Firefox浏览器 http://www.firefox.com.cn/ Step2: 安装Selenium IDE插件 http://seleniumhq.org/down ...

  2. Q - Tour - hdu 3488(最小匹配值)

    题意:一个王国有N个城市,M条路,都是有向的,现在可以去旅游,不过走的路只能是环(至少也需要有两个城市),他们保证这些城市之间的路径都是有环构成的,现在至少需要走多少路. 分析:因为是有向图所以,而且 ...

  3. 用js代码判断是否IE浏览器

    判断是否IE的方法: var isIE = function(ver) { var div = document.createElement("div"), status; div ...

  4. JNI调用native方法出现 java.lang.UnsatisfiedLinkError: XXXclass.XXXmethod()异常的解决办法

    昨天拿到JNI的Android工程Demo,然后把demo整合到开发的主线工程上,发现调用JNI方法一直抛同一个异常 java.lang.UnsatisfiedLinkError: XXXclass. ...

  5. 《JAVA课程设计》实训第四天——《猜猜看》游戏

    第四天,本来想进一步去改进<猜猜看>游戏的.可是非常多问题都不理解.也不熟悉怎么去弄到连接数据库.统计猜对次数,所以并没有进行再多的改动. 基本上就是这种执行结果了 import java ...

  6. ubuntu下libjson-c库的使用问题备忘

    首先安装libjson的c库 #apt-get install libjson0-dev libjson0 安装好后查看/usr/include/json下是否有头文件,有就对了! gcc -o ...

  7. oracle中split的使用

    1.创建自己的类型 VARCHAR2ARRAY CREATE OR REPLACE TYPE "VARCHAR2ARRAY" as table of varchar2(300); ...

  8. Java基础知识强化22:Java中数据类型转换

    数据类型转换: (1). 自动转换 低级变量可以直接转换为高级变量,这叫自动类型转换.比如: byte b: int b:  long b:  float b:   double  b: 上面的语句可 ...

  9. SuperSocket快速入门(三):实现你的AppServer和AppSession

    什么是AppSession? AppSession 代表一个和客户端的逻辑连接,基于连接的操作应该定义于在该类之中.你可以用该类的实例发送数据到客户端,接收客户端发送的数据或者关闭连接.同时可以保存客 ...

  10. Examples_06_02(android)DDMS的data文件中没有显示文件。

    以前这里不显示music.cfg.通过Reset adb,就显示了. 查看虚拟机运行时里面的文件,进入adb.exe目录: E:\TDDOWNLOAD\adt-bundle-windows-x86-2 ...