一般控制浏览器中网页缓存可以通过设置HTTP响应头来实现。本文主要介绍PHP、Java Servlet、Node.js、ASP.NET MVC、ASP.NET Web API、ASP.NET、ASP.NET Core v3、ASP、Ruby、Python/Flask、Python/Django、Python/Pyramid、Go、Clojure、HTML和Apache .htaccess中设置HTTP响应头,来实现控制浏览器中的网页缓存的方法,以及相关的示例代码。

1、控制网页缓存的HTTP协议头

在Internet Explorer 6+、Firefox 1.5+、Safari 3+、Opera 9+、Chrome浏览器中适用的协议头:

Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 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代理。

如不关使用no-store时,通过HTTPS提供页面时的IE6及其错误缓存问题,可以省略Cache-Control: no-cache。

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

如不关心IE6和HTTP 1.0客户端(HTTP 1.1于1997年引入),则可以省略Pragma。

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

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

Cache-Control: no-store, must-revalidate

如果服务器自动包含有效的Date标头,那么理论上可以省略Cache-Control,并仅依赖于Expires。

Date: Wed, 24 Aug 2016 18:32:02 GMT
Expires: 0

但如最终用户操纵操作系统日期,而客户端软件依赖它,那么这种方法可能会失败。

注意:HTTP 1.0代理指的是符合HTTP 1.0协议规范的代理服务器。在HTTP 1.0协议中,代理服务器在请求和响应中使用Expires标头来确定资源的过期时间。当客户端发送请求时,代理服务器会检查资源的过期时间,如果资源已过期,则代理服务器会重新请求该资源并将更新后的响应返回给客户端。

2、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.

3、Java Servlet或Node.js中设置HTTP缓存协议头

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.

4、.NET(C#)中设置HTTP缓存协议头

1)ASP.NET MVC

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.

2)ASP.NET Web API

/ `response` is an instance of System.Net.Http.HttpResponseMessage
response.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()); 

3)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.

4)ASP.NET Core v3

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

5、ASP中设置HTTP缓存协议头

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.

6、Ruby中设置HTTP缓存协议头

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

7、Python/Flask中设置HTTP缓存协议头

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

8、Python/Django中设置HTTP缓存协议头

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

9、Python/Pyramid中设置HTTP缓存协议头

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

10、Go中设置HTTP缓存协议头

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.

11、Clojure中设置HTTP缓存协议头

(require '[ring.util.response :as r])
(-> response
  (r/header "Cache-Control" "no-cache, no-store, must-revalidate")
  (r/header "Pragma" "no-cache")
  (r/header "Expires" 0))

12、Apache .htaccess中设置HTTP缓存协议头

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

13、HTML中设置HTTP缓存协议头

<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">

推荐文档