您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

缓存工作原理How caching works

本文概述了一般缓存概念以及 Azure 内容分发网络 (CDN) 如何使用缓存来提高性能。This article provides an overview of general caching concepts and how Azure Content Delivery Network (CDN) uses caching to improve performance. 如果想了解如何在 CDN 终结点上自定义缓存行为,请参阅使用缓存规则控制 Azure CDN 缓存行为使用查询字符串控制 Azure CDN 缓存行为If you’d like to learn about how to customize caching behavior on your CDN endpoint, see Control Azure CDN caching behavior with caching rules and Control Azure CDN caching behavior with query strings.

缓存简介Introduction to caching

缓存即在本地存储数据的过程,以便将来可以更快地访问数据请求。Caching is the process of storing data locally so that future requests for that data can be accessed more quickly. Web 浏览器缓存是最常见的缓存类型中,在此类缓存中,Web 浏览器将静态数据的副本本地存储在本地硬盘上。In the most common type of caching, web browser caching, a web browser stores copies of static data locally on a local hard drive. 通过使用缓存,Web 浏览器可避免多次往返服务器,而只需在本地访问相同的数据,从而节省时间和资源。By using caching, the web browser can avoid making multiple round-trips to the server and instead access the same data locally, thus saving time and resources. 缓存非常适合用于在本地管理小型静态数据,如静态图像、CSS 文件和 JavaScript 文件。Caching is well-suited for locally managing small, static data such as static images, CSS files, and JavaScript files.

同样,内容分发网络在靠近用户的边缘服务器上使用缓存,以避免请求返回源服务器,从而减少用户延迟。Similarly, caching is used by a content delivery network on edge servers close to the user to avoid requests traveling back to the origin and reducing end-user latency. 与仅适用于单个用户的 Web 浏览器缓存不同,CDN 具有共享缓存。Unlike a web browser cache, which is used only for a single user, the CDN has a shared cache. 在 CDN 共享缓存中,其他用户稍后可访问某一用户请求的文件,这大大减少了对源服务器的请求数量。In a CDN shared cache, a file that is requested by one user can be accessed later by other users, which greatly decreases the number of requests to the origin server.

无法缓存频繁更改或单个用户的唯一动态资源。Dynamic resources that change frequently or are unique to an individual user cannot be cached. 不过,这些类型的资源可利用 Azure 内容分发网络上的动态站点加速 (DSA) 优化来提高性能。Those types of resources, however, can take advantage of dynamic site acceleration (DSA) optimization on the Azure Content Delivery Network for performance improvements.

可在源服务器和最终用户之间的多个级别上进行缓存:Caching can occur at multiple levels between the origin server and the end user:

  • Web 服务器:使用共享缓存(适用于多个用户)。Web server: Uses a shared cache (for multiple users).
  • 内容分发网络:使用共享缓存(适用于多个用户)。Content delivery network: Uses a shared cache (for multiple users).
  • Internet 服务提供商 (ISP):使用共享缓存(适用于多个用户)。Internet service provider (ISP): Uses a shared cache (for multiple users).
  • Web 浏览器:使用专用缓存(适用于一个用户)。Web browser: Uses a private cache (for one user).

通常,每个缓存管理自己的资源刷新,并在文件过期时执行验证。Each cache typically manages its own resource freshness and performs validation when a file is stale. 此行为在 HTTP 缓存规范 RFC 7234 中定义。This behavior is defined in the HTTP caching specification, RFC 7234.

资源刷新Resource freshness

由于缓存资源可能会过期或过时(与源服务器上的相应资源相比),因此对任何缓存机制而言,控制刷新内容的时间至关重要。Because a cached resource can potentially be out-of-date, or stale (as compared to the corresponding resource on the origin server), it is important for any caching mechanism to control when content is refreshed. 为节省时间和带宽消耗,每次访问时都不会将缓存资源与源服务器上的版本进行比较。To save time and bandwidth consumption, a cached resource is not compared to the version on the origin server every time it is accessed. 相反,只要认定缓存资源是最新的,就认为它是当前版本,并将其直接发送到客户端。Instead, as long as a cached resource is considered to be fresh, it is assumed to be the most current version and is sent directly to the client. 如果缓存资源的寿命小于缓存设置定义的寿命或期限,则认为其是最新缓存资源。A cached resource is considered to be fresh when its age is less than the age or period defined by a cache setting. 例如,当浏览器重载网页时,它会验证硬盘上的每个缓存资源是否为最新并加载它。For example, when a browser reloads a web page, it verifies that each cached resource on your hard drive is fresh and loads it. 如果资源不是最新的(过时),则从服务器加载最新副本。If the resource is not fresh (stale), an up-to-date copy is loaded from the server.


如果认为资源已过时,则要求源服务器对其进行验证,即确定缓存中的数据是否仍与源服务器上的数据相匹配。If a resource is considered to be stale, the origin server is asked to validate it, that is, determine whether the data in the cache still matches what’s on the origin server. 如果已在源服务器上修改了文件,缓存会更新其资源的版本。If the file has been modified on the origin server, the cache updates its version of the resource. 否则,如果资源为最新,将直接从缓存中传递数据,而无需首先对其进行验证。Otherwise, if the resource is fresh, the data is delivered directly from the cache without validating it first.

CDN 缓存CDN caching

缓存是 CDN 运行方式不可或缺的一部分,用于加速传递和减少静态资产(如图像、字体和视频)的源加载。Caching is integral to the way a CDN operates to speed up delivery and reduce origin load for static assets such as images, fonts, and videos. 在 CDN 缓存中,选择性地将静态资源存储在策略性服务器上,该服务器对用户而言更本地化,并具有以下优势:In CDN caching, static resources are selectively stored on strategically placed servers that are more local to a user and offers the following advantages:

  • 由于大多数 Web 流量是静态的(例如图像、字体和视频),CDN 缓存通过将内容移近用户来减少数据传输的距离,从而减少网络延迟。Because most web traffic is static (for example, images, fonts, and videos), CDN caching reduces network latency by moving content closer to the user, thus reducing the distance that data travels.

  • 通过将工作卸载到 CDN,缓存可减少网络流量和源服务器上的加载。By offloading work to a CDN, caching can reduce network traffic and the load on the origin server. 这样做可降低应用程序的成本和资源需求,即使在有大量用户的情况下也是如此。Doing so reduces cost and resource requirements for the application, even when there are large numbers of users.

与缓存在 Web 浏览器中的执行方式类似,可通过发送缓存指令标头来控制缓存在 CDN 中的执行方式。Similar to how caching is implemented in a web browser, you can control how caching is performed in a CDN by sending cache-directive headers. 缓存指令标头即 HTTP 标头,通常由源服务器添加。Cache-directive headers are HTTP headers, which are typically added by the origin server. 尽管大部分标头最初都旨在解决客户端浏览器中的缓存问题,但现在所有中间缓存(如 CDN)也会使用这些标头。Although most of these headers were originally designed to address caching in client browsers, they are now also used by all intermediate caches, such as CDNs.

可使用两个标头来定义缓存刷新:Cache-ControlExpiresTwo headers can be used to define cache freshness: Cache-Control and Expires. 如果两者都存在,则 Cache-Control 为最新且优先于 ExpiresCache-Control is more current and takes precedence over Expires, if both exist. 还有两种用于验证的标头类型(称为验证程序):ETagLast-ModifiedThere are also two types of headers used for validation (called validators): ETag and Last-Modified. 如果两者均已定义,则 ETag 为最新且优先于 Last-ModifiedETag is more current and takes precedence over Last-Modified, if both are defined.

缓存指令标头Cache-directive headers


默认情况下,针对 DSA 进行了优化的 Azure CDN 终结点将忽略缓存指令标头,绕过缓存。By default, an Azure CDN endpoint that is optimized for DSA ignores cache-directive headers and bypasses caching. 对于 Verizon 提供的标准 Azure CDNAkamai 提供的标准 Azure CDN 配置文件,可以使用 CDN 缓存规则启用缓存,来调整 Azure CDN 终结点处理这些标头的方式。For Azure CDN Standard from Verizon and Azure CDN Standard from Akamai profiles, you can adjust how an Azure CDN endpoint treats these headers by using CDN caching rules to enable caching. 仅对于 Verizon 提供的高级 Azure CDN 配置文件,可以使用 规则引擎来启用缓存。For Azure CDN Premium from Verizon profiles only, you use the rules engine to enable caching.

Azure CDN 支持以下 HTTP 缓存指令标头,它们定义了缓存持续时间和缓存共享。Azure CDN supports the following HTTP cache-directive headers, which define cache duration and cache sharing.


  • 在 HTTP 1.1 中引入,用于为 Web 发布者提供对其内容的更多控制权,并解决 Expires 标头的局限性。Introduced in HTTP 1.1 to give web publishers more control over their content and to address the limitations of the Expires header.
  • 如果同时定义了 ExpiresCache-Control 标头,则将替代前一个标头。Overrides the Expires header, if both it and Cache-Control are defined.
  • 在从客户端发往 CDN POP 的 HTTP 请求中使用时,Cache-Control 会被所有 CDN 配置文件默认忽略。When used in an HTTP request from the client to the CDN POP, Cache-Control is ignored by all Azure CDN profiles, by default.
  • 在从客户端发往 CDN POP 的 HTTP 响应中使用时:When used in an HTTP response from the client to the CDN POP:
    • Verizon 推出的 Azure CDN 标准版/高级版Microsoft 推出的 Azure CDN 标准版 支持所有 Cache-Control 指令。Azure CDN Standard/Premium from Verizon and Azure CDN Standard from Microsoft support all Cache-Control directives.
    • Verizon 推出的 Azure CDN 标准版/高级版和 Microsoft 推出的 Azure CDN 标准版遵循 RFC 7234 - 超文本传输协议 (HTTP/1.1):缓存 (ietf.org) 中 Cache-Control 指令的缓存行为 。Azure CDN Standard/Premium from Verizon and Azure CDN Standard from Microsoft honors caching behaviors for Cache-Control directives in RFC 7234 - Hypertext Transfer Protocol (HTTP/1.1): Caching (ietf.org).
    • Akamai 推出的 Azure CDN 标准版 仅支持以下 Cache-Control 指令,将忽略所有其他指令:Azure CDN Standard from Akamai supports only the following Cache-Control directives; all others are ignored:
      • max-age:缓存可存储指定秒数的内容。max-age: A cache can store the content for the number of seconds specified. 例如,Cache-Control: max-age=5For example, Cache-Control: max-age=5. 此指令指定了被视为最新内容的最长时间。This directive specifies the maximum amount of time the content is considered to be fresh.
      • no-cache:缓存内容,但每次传送缓存中的内容前会对其进行验证。no-cache: Cache the content, but validate the content every time before delivering it from the cache. 等效于 Cache-Control: max-age=0Equivalent to Cache-Control: max-age=0.
      • no-store:从不缓存内容。no-store: Never cache the content. 删除之前已存储的内容。Remove content if it has been previously stored.


  • HTTP 1.0 中引入的旧标头支持向后兼容性。Legacy header introduced in HTTP 1.0; supported for backwards compatibility.
  • 使用基于日期的过期时间,精确到秒。Uses a date-based expiration time with second precision.
  • 类似于 Cache-Control: max-ageSimilar to Cache-Control: max-age.
  • Cache-Control 不存在时使用。Used when Cache-Control doesn't exist.


  • Azure CDN 默认情况下未采用。Not honored by Azure CDN, by default.
  • HTTP 1.0 中引入的旧标头支持向后兼容性。Legacy header introduced in HTTP 1.0; supported for backwards compatibility.
  • 用作具有以下指令的客户端请求标头:no-cacheUsed as a client request header with the following directive: no-cache. 此指令指示服务器提供新的资源版本。This directive instructs the server to deliver a fresh version of the resource.
  • Pragma: no-cache 等效于 Cache-Control: no-cachePragma: no-cache is equivalent to Cache-Control: no-cache.


当缓存过时时,使用 HTTP 缓存验证程序将文件的缓存版本与源服务器上的版本进行比较。When the cache is stale, HTTP cache validators are used to compare the cached version of a file with the version on the origin server. Verizon 推出的 Azure CDN 标准版/高级版 默认支持 ETagLast-Modified 验证程序,而 Microsoft 推出的 Azure CDN 标准版Akamai 推出的 Azure CDN 标准版 默认仅支持 Last-ModifiedAzure CDN Standard/Premium from Verizon supports both ETag and Last-Modified validators by default, while Azure CDN Standard from Microsoft and Azure CDN Standard from Akamai supports only Last-Modified by default.


  • Verizon 推出的 Azure CDN 标准版/高级版 默认支持 ETag,而 Microsoft 推出的 Azure CDN 标准版Akamai 推出的 Azure CDN 标准版 则不。Azure CDN Standard/Premium from Verizon supports ETag by default, while Azure CDN Standard from Microsoft and Azure CDN Standard from Akamai do not.
  • ETag 为每个文件和文件版本定义唯一字符串。ETag defines a string that is unique for every file and version of a file. 例如,ETag: "17f0ddd99ed5bbe4edffdd6496d7131f"For example, ETag: "17f0ddd99ed5bbe4edffdd6496d7131f".
  • 在 HTTP 1.1 中引入,并且比 Last-Modified 更新。Introduced in HTTP 1.1 and is more current than Last-Modified. 当很难确定上次修改日期时,会非常有用。Useful when the last modified date is difficult to determine.
  • 支持强验证和弱验证,不过,Azure CDN 仅支持强验证。Supports both strong validation and weak validation; however, Azure CDN supports only strong validation. 对于强验证,两种资源表示形式的每个字节都必须相同。For strong validation, the two resource representations must be byte-for-byte identical.
  • 缓存通过在请求中发送带有一个或多个 ETag 验证程序的 If-None-Match 标头来验证使用 ETag 的文件。A cache validates a file that uses ETag by sending an If-None-Match header with one or more ETag validators in the request. 例如,If-None-Match: "17f0ddd99ed5bbe4edffdd6496d7131f"For example, If-None-Match: "17f0ddd99ed5bbe4edffdd6496d7131f". 如果服务器的版本与列表中的 ETag 验证程序相匹配,则在其响应中发送状态代码 304(未修改)。If the server’s version matches an ETag validator on the list, it sends status code 304 (Not Modified) in its response. 如果版本不同,则服务器响应状态代码 200(确定)和更新后的资源。If the version is different, the server responds with status code 200 (OK) and the updated resource.


  • 如果 ETag 不是 HTTP 响应的一部分,则使用 Last-Modified(仅限 Verizon 推出的 Azure CDN 标准版/高级版)。For Azure CDN Standard/Premium from Verizon only, Last-Modified is used if ETag is not part of the HTTP response.
  • 指定源服务器已确定上次修改资源的日期和时间。Specifies the date and time that the origin server has determined the resource was last modified. 例如,Last-Modified: Thu, 19 Oct 2017 09:28:00 GMTFor example, Last-Modified: Thu, 19 Oct 2017 09:28:00 GMT.
  • 缓存通过在请求中发送带有日期和时间的 If-Modified-Since 标头来验证使用 Last-Modified 的文件。A cache validates a file using Last-Modified by sending an If-Modified-Since header with a date and time in the request. 源服务器将该日期与最新资源的 Last-Modified 标头进行比较。The origin server compares that date with the Last-Modified header of the latest resource. 如果自指定时间以来未修改该资源,则服务器在其响应中返回状态代码 304(未修改)。If the resource has not been modified since the specified time, the server returns status code 304 (Not Modified) in its response. 如果已修改该资源,则服务器返回状态代码 200(确定)和更新后的资源。If the resource has been modified, the server returns status code 200 (OK) and the updated resource.

确定可以缓存哪些文件Determining which files can be cached

并非所有资源均可缓存。Not all resources can be cached. 下表根据 HTTP 响应的类型显示了可缓存的资源。The following table shows what resources can be cached, based on the type of HTTP response. 无法缓存不满足所有条件的 HTTP 响应所提供的资源。Resources delivered with HTTP responses that don't meet all of these conditions cannot be cached. 可使用规则引擎自定义上述某些条件(仅限 Verizon 推出的 Azure CDN 高级版)。For Azure CDN Premium from Verizon only, you can use the rules engine to customize some of these conditions.

Microsoft 推出的 Azure CDNAzure CDN from Microsoft Verizon 的 Azure CDNAzure CDN from Verizon Akamai 的 Azure CDNAzure CDN from Akamai
HTTP 状态代码HTTP status codes 200、203、206、300、301、410、416200, 203, 206, 300, 301, 410, 416 200200 200、203、300、301、302、401200, 203, 300, 301, 302, 401
文件大小限制File size limits 300 GB300 GB 300 GB300 GB - 常规 Web 传递优化:1.8 GB- General web delivery optimization: 1.8 GB
- 媒体流式处理优化:1.8 GB- Media streaming optimizations: 1.8 GB
- 大型文件优化:150 GB- Large file optimization: 150 GB

若要在资源上使用 Microsoft 推出的 Azure CDN 标准版 缓存,源服务器必须支持任何 HEAD 和 GET HTTP 请求,且资产的任何 HEAD 和 GET HTTP 响应的 content-length 值必须相同。For Azure CDN Standard from Microsoft caching to work on a resource, the origin server must support any HEAD and GET HTTP requests and the content-length values must be the same for any HEAD and GET HTTP responses for the asset. 对于 HEAD 请求,源服务器必须支持 HEAD 请求,且必须使用接收 GET 请求时所使用的标头进行响应。For a HEAD request, the origin server must support the HEAD request, and must respond with the same headers as if it had received a GET request.

默认缓存行为Default caching behavior

下表介绍了 Azure CDN 产品的默认缓存行为及其优化。The following table describes the default caching behavior for the Azure CDN products and their optimizations.

Microsoft:常规 Web 分发Microsoft: General web delivery Verizon:常规 Web 分发Verizon: General web delivery Verizon:DSAVerizon: DSA Akamai:常规 Web 分发Akamai: General web delivery Akamai:DSAAkamai: DSA Akamai:大型文件下载Akamai: Large file download Akamai:常规或 VOD 媒体流式处理Akamai: general or VOD media streaming
优先处理源Honor origin Yes Yes No Yes No Yes Yes
CDN 缓存持续时间CDN cache duration 2 天2 days 7 天7 days None 7 天7 days None 1 天1 day 1 年1 year

优先处理源:指定是否优先处理支持的缓存指令标头(如果它们存在于源服务器的 HTTP 响应中)。Honor origin: Specifies whether to honor the supported cache-directive headers if they exist in the HTTP response from the origin server.

CDN 缓存持续时间:指定在 Azure CDN 上可缓存资源的时间。CDN cache duration: Specifies the amount of time for which a resource is cached on the Azure CDN. 然而,如果“优先处理源”为“是”,并且源服务器的 HTTP 响应包括缓存指令标头 ExpiresCache-Control: max-age,则 Azure CDN 将改用由标头指定的持续时间值。However, if Honor origin is Yes and the HTTP response from the origin server includes the cache-directive header Expires or Cache-Control: max-age, Azure CDN uses the duration value specified by the header instead.

后续步骤Next steps