栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我们如何在所有浏览器中控制网页缓存?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我们如何在所有浏览器中控制网页缓存?

介绍
适用于所有提到的客户端(和代理)的正确的最小标头集:

Cache-Control: no-cache, no-store, must-revalidatePragma: no-cacheExpires: 0

该Cache-Control是每HTTP 1.1规范,为客户和代理(和一些客户需要隐旁边Expires)。根据Pragma史前客户端的HTTP 1.0规范。根据Expires客户端和代理的HTTP 1.0和1.1规范。在HTTP 1.1中,Cache-Control优先级高于Expires,因此仅适用于HTTP 1.0代理。

如果仅通过HTTPS提供页面时,如果您不在意IE6及其缓存中断no-store,那么您可以省略Cache-Control: no-cache。

Cache-Control: no-store, must-revalidatePragma: no-cacheExpires: 0

如果您不关心IE6或HTTP 1.0客户端(HTTP 1.1于1997年推出),则可以忽略Pragma。

Cache-Control: no-store, must-revalidateExpires: 0

如果您也不关心HTTP 1.0代理,则可以省略Expires。

Cache-Control: no-store, must-revalidate

另一方面,如果服务器自动包含有效的Date标头,则从理论上讲,您也可以忽略它Cache-Control而Expires仅依赖它。

Date: Wed, 24 Aug 2016 18:32:02 GMTExpires: 0

但这可能会失败,例如,如果最终用户操纵了操作系统日期,而客户端软件则依赖它。

如果指定了上述参数,则其他Cache-Control参数如max-age无关紧要Cache-Control。仅在您确实要缓存请求时,Last-Modified此处大多数其他答案中包含的标头才是有趣的,因此您根本不需要指定它。

如何设置?
使用PHP:

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

使用Java Servlet或Node.js:

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

使用ASP.NET-MVC

Response.Cache.SetCacheability(HttpCacheability.NoCache);  // HTTP 1.1.Response.Cache.AppendCacheExtension("no-store, must-revalidate");Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.Response.AppendHeader("Expires", "0"); // Proxies.使用ASP.NET Web API:// `response` is an instance of System.Net.Http.HttpResponseMessageresponse.Headers.CacheControl = new CacheControlHeaderValue{    NoCache = true,    NoStore = true,    MustRevalidate = true};response.Headers.Pragma.ParseAdd("no-cache");// We can't use `response.Content.Headers.Expires` directly// since it allows only `DateTimeOffset?` values.response.Content?.Headers.TryAddWithoutValidation("Expires", 0.ToString()); 

使用ASP.NET:

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

使用ASP.NET Core v3

// using Microsoft.Net.Http.HeadersResponse.Headers[HeaderNames.CacheControl] = "no-cache, no-store, must-revalidate";Response.Headers[HeaderNames.Expires] = "0";Response.Headers[HeaderNames.Pragma] = "no-cache";

使用ASP:

Response.addHeader "Cache-Control", "no-cache, no-store, must-revalidate" ' HTTP 1.1.Response.addHeader "Pragma", "no-cache" ' HTTP 1.0.Response.addHeader "Expires", "0" ' Proxies.

使用Ruby on Rails或Python / Flask:

headers["Cache-Control"] = "no-cache, no-store, must-revalidate" # HTTP 1.1.headers["Pragma"] = "no-cache" # HTTP 1.0.headers["Expires"] = "0" # Proxies.

使用Python / Django:

response["Cache-Control"] = "no-cache, no-store, must-revalidate" # HTTP 1.1.response["Pragma"] = "no-cache" # HTTP 1.0.response["Expires"] = "0" # Proxies.

使用Python /金字塔:

request.response.headerlist.extend(    (        ('Cache-Control', 'no-cache, no-store, must-revalidate'),        ('Pragma', 'no-cache'),        ('Expires', '0')    ))

使用Go:

responseWriter.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1.responseWriter.Header().Set("Pragma", "no-cache") // HTTP 1.0.responseWriter.Header().Set("Expires", "0") // Proxies.

使用Apache.htaccess文件:

<IfModule mod_headers.c>    Header set Cache-Control "no-cache, no-store, must-revalidate"    Header set Pragma "no-cache"    Header set Expires 0</IfModule>

使用HTML:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"><meta http-equiv="Pragma" content="no-cache"><meta http-equiv="Expires" content="0">

HTML元标记与HTTP响应标头
重要的是要知道,当通过HTTP连接为HTML页面提供服务,并且HTTP响应标头和HTML标记中都存在标头时

<meta http-equiv>
,则HTTP响应标头中指定的标头将优先于HTML meta标记。仅当通过file://URL从本地磁盘文件系统查看页面时,才使用HTML元标记。另请参阅W3 HTML规范第5.2.2章。当您不以编程方式指定它们时,请务必小心,因为Web服务器可以包含一些默认值。

通常,最好不要指定HTML元标记,以免引起初学者的困惑,并依靠硬HTTP响应标头。此外,尤其是这些

<meta http-equiv>
标记在HTML5中无效。仅允许HTML5规范中http-equiv列出的值。

验证实际的HTTP响应标头
要验证彼此,可以在webbrowser开发人员工具集的HTTP流量监视器中查看/调试它们。您可以通过在Chrome / Firefox23 + / IE9 +中按F12,然后打开“网络”或“网络”标签面板,然后单击感兴趣的HTTP请求以发现有关HTTP请求和响应的所有详细信息,来到达那里。在下面的截图是从Chrome中:

Chrome开发人员工具集HTTP流量监视器,上显示HTTP响应标头
我也想在文件下载中设置这些标题
首先,此问题和答案针对“网页”(HTML页面),而不是“文件下载”(PDF,zip,Excel等)。您最好将它们缓存起来,并在URI路径或querystring中的某处使用一些文件版本标识符,以强制在更改的文件上重新下载。无论如何,当在文件下载上应用这些无缓存头时,请注意在通过HTTPS而非HTTP提供文件下载服务时的IE7 / 8错误。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/454484.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号