简体   繁体   English

如何将缓存控制设置为始终检查更新,但如果服务器无法访问则始终回退到缓存

[英]How to set cache-control to always check for updates but always fall back to cache if server is unreachable

I'm missing something trying to understand cache-control (eg, from https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control ).我遗漏了一些试图理解缓存控制的东西(例如,来自https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control )。

How do I set up cache control to accomplish the following (I'll be using an .htaccess file):如何设置缓存控制以完成以下操作(我将使用 .htaccess 文件):

  1. If client fetches a file, it should always store it in the cache.如果客户端获取文件,它应该始终将其存储在缓存中。
  2. When client needs a file, it should always check to see if the file has been changed and download a new copy if it has changed.当客户端需要一个文件时,它应该始终检查文件是否已被更改,如果已更改则下载一个新副本。
  3. If the attempt to check fails -- eg, server down or no Inte.net connection -- client should always use a cached copy if available, no matter how old.如果检查尝试失败——例如,服务器关闭或没有 Inte.net 连接——客户端应该始终使用缓存副本(如果可用),无论它有多旧。 Any copy is better than none.任何副本总比没有好。

Use Cache-Control: no-cache and set the ETag header.使用Cache-Control: no-cache并设置ETag header。

  1. The resource will be stored in the cache.资源将存储在缓存中。 This is true of any cache header other than no-store .除了no-store之外的任何缓存 header 都是如此。
  2. no-cache tells the client that it must check with the server to see if the cached copy is valid. no-cache告诉客户端它必须检查服务器以查看缓存的副本是否有效。 It does this by sending a conditional request, which requires that the cached response have an ETag (or Last-Modified ) header.它通过发送条件请求来做到这一点,这要求缓存的响应具有ETag (或Last-Modified )header。
  3. Using a cached copy of a resource when there's no connectivity is the default behavior.在没有连接时使用资源的缓存副本是默认行为。 You could prevent this with the must-revalidate directive.您可以使用must-revalidate指令来防止这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM