简体   繁体   English

什么决定了 HTTP 缓存的请求等效性?

[英]What determines request equivalence for HTTP caching?

I feel like this has to be easy to Google, but I can't find it: from the perspective of an HTTP cache, what determines if two requests are equivalent?感觉这个一定要google好方便,但是找不到:从一个HTTP缓存的角度来看,是什么决定了两个请求是否等价?

I imagine one ingredient is that that their URLs need to be identical;我想一个要素是它们的 URL 需要相同; for example, rearranging (but not changing) query string parameters seems to cause a cache miss.例如,重新排列(但不更改)查询字符串参数似乎会导致缓存未命中。 Presumably they need to have the same Accept header.据推测,它们需要具有相同的Accept标头。 What else determines if a request can be served from cache?还有什么决定是否可以从缓存中处理请求?

This is mostly described in this RFC: https://tools.ietf.org/html/rfc7234#section-4这主要在此 RFC 中进行了描述: https : //tools.ietf.org/html/rfc7234#section-4

Summary:概括:

  • The method方法
  • The full uri完整的 uri
  • Caching-related headers in response influence whether something got stored.响应中与缓存相关的标头会影响是否存储了某些内容。
  • Any request headers that appeared in the list of the Vary response header.出现在Vary响应标头列表中的任何请求标头。

It also matters whether you are caching for a specific user (for example a browser), or many users (for example a proxy).您是为特定用户(例如浏览器)还是许多用户(例如代理)进行缓存也很重要。

There are all the conditional requests for cache control like If-match, If-unmodified-since, If-none-match and If-modified-since . 存在缓存控制的所有条件请求,如If-match, If-unmodified-since, If-none-match and If-modified-since For example If-modified-since works this way: suppose you have already requested a page and now you want to reload it. 例如,If-modified-since以这种方式工作:假设您已经请求了一个页面,现在您想重新加载它。 If the header is present then a new page will be sent back from the server ONLY if it was modified since the date indicated as a value for If-modified-since, otherwise 304(not-modified) status will be returned. 如果标题存在,则只有在自指示为If-modified-since的值的日期之后修改了新页面时才会从服务器发回新页面,否则将返回304(not-modified)状态。 Accept and Accept-* instead are necessary for Content-Negotiation, like in which language the page should be returned. 接受和接受 - *是内容协商所必需的,就像应该返回页面的语言一样。

More on conditional requests here: https://tools.ietf.org/html/rfc7232#page-13 有关条件请求的更多信息,请访问: https//tools.ietf.org/html/rfc7232#page-13

I also struggled with this.我也为此苦苦挣扎。 Changing my google search to use "http cache key" generated better results.将我的 google 搜索更改为使用“http 缓存密钥”会产生更好的结果。 Using the URL seems to be the most common.使用 URL 似乎是最常见的。 Query strings are also generally included.通常还包括查询字符串。

https://support.cloudflare.com/hc/en-us/articles/115004290387-Using-Custom-Cache-Keys describes what the default is for cloudflare and a discussion on the impact of using different keys. https://support.cloudflare.com/hc/en-us/articles/115004290387-Using-Custom-Cache-Keys描述了 cloudflare 的默认设置,并讨论了使用不同密钥的影响。

Another parameter that could be useful is to identifying the type of assets that you want to cache.另一个可能有用的参数是标识要缓存的资产类型。 Or leave it open (no filtering)或保持打开状态(无过滤)

"Authorization" header is specifically mentioned in the HTTP spec ( https://tools.ietf.org/html/rfc7234 ) and needs to be handled. “授权”标头在 HTTP 规范( https://tools.ietf.org/html/rfc7234 )中特别提到,需要处理。

Upon further reading, I noticed the section on "Secondary keys" in the standard ( https://tools.ietf.org/html/rfc7234#section-4.1 ) and the use of "Vary" header in a response.进一步阅读后,我注意到标准( https://tools.ietf.org/html/rfc7234#section-4.1 )中关于“辅助键”的部分以及在响应中使用“Vary”标头。 Headers presented in the "Vary" response header have to match in both the original and the new request for the cache to declare it as a match. “Vary”响应标头中出现的标头必须在原始请求和新请求中都匹配,以便缓存将其声明为匹配。

And as for the primary key, standard says "The primary cache key consists of the request method and target URI."至于主键,标准说“主缓存键由请求方法和目标 URI 组成”。 in https://tools.ietf.org/html/rfc7234#section-2https://tools.ietf.org/html/rfc7234#section-2

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

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