简体   繁体   English

即使使用缓存控制,来自云端的 RefreshHit:max-age=0,无存储

[英]RefreshHit from cloudfront even with cache-control: max-age=0, no-store

Cloudfront is getting a RefreshHit for a request that is not supposed to be cached at all. Cloudfront 正在为根本不应缓存的请求获取 RefreshHit。

在此处输入图像描述

It shouldn't be cached because:它不应该被缓存,因为:

  1. It has cache-control: max-age=0, no-store ;它有cache-control: max-age=0, no-store ;
  2. The Minimum TTL is 0;最小 TTL 为 0; and
  3. I've created multiple invalidations (on /* ) so this cached resource isn't from some historical deploy我已经创建了多个失效(在/*上)所以这个缓存的资源不是来自一些历史部署

在此处输入图像描述

Any idea why I'm getting RefreshHits?知道为什么我会收到 RefreshHits 吗?

I also tried modifying Cache-Control to be cache-control no-store, stale-if-error=0 , creating a new invalidation on /* and now I'm seeing a cache hit (this time in Firefox):我还尝试将 Cache-Control 修改为cache-control no-store, stale-if-error=0 ,在/*上创建新的失效,现在我看到缓存命中(这次是在 Firefox 中):

在此处输入图像描述

After talking extensively with support, they explained what's going on.在与支持人员进行广泛交谈后,他们解释了正在发生的事情。

So, if you have no-store and a Minimum TTL of 0, then CloudFront will indeed not store your resources.因此,如果您no-store且最小 TTL 为 0,则 CloudFront 确实不会存储您的资源。 However, if your Origin is taking a long time to respond (so likely under heavy load), while CloudFront waits for the response to the request, if it gets another identical request (identical with respect to the cache key), then it'll send the one response to both requests.但是,如果您的 Origin 需要很长时间来响应(很可能在高负载下),而 CloudFront 等待对请求的响应,如果它收到另一个相同的请求(与缓存键相同),那么它会向两个请求发送一个响应。 This is in order to lighten the load on the server.这是为了减轻服务器的负载。 (see docs ) (见文档

Support was calling these "collapse hits" although I don't see that in the docs.尽管我在文档中没有看到,但支持人员将这些称为“崩溃命中”。

So, it seems you can't have a single Behavior serving some pages that must have a unique response per request while serving other pages that are cached.因此,似乎您不能让单个 Behavior 为某些页面提供服务,而这些页面必须为每个请求提供唯一响应,同时为其他缓存页面提供服务。 Support said:支持说:

I just confirmed that, with min TTL 0 and cache-control: no-store, we cannot disable collapse hit.我刚刚确认,使用 min TTL 0 和 cache-control: no-store,我们无法禁用崩溃命中。 If you do need to fully disable cloudfront cache, you can use cache policy CachingDisabled如果您确实需要完全禁用 Cloudfront 缓存,则可以使用缓存策略 CachingDisabled

We'll be making a behavior for every path prefix that we need caching on.我们将为我们需要缓存的每个路径前缀创建一个行为。 It seems there was no better way than this for our use-case (transitioning our website one page at a time from a non-cacheable, backend-rendered jinja2/jQuery to a cacheable, client-side rendered React/Next.js).对于我们的用例来说,似乎没有比这更好的方法(一次将我们的网站从不可缓存的、后端渲染的 jinja2/jQuery 转换为可缓存的、客户端渲染的 React/Next.js)。

It's probably too late for OP's project, but I would personally handle this with a simple origin-response Lambda@Edge function, and a single cache behavior for /* and cache policy.对于 OP 的项目来说可能为时已晚,但我个人会使用简单的原始响应 Lambda@Edge function 以及 /* 和缓存策略的单一缓存行为来处理这个问题。 You can write all of the filtering/caching logic in the origin-response function. That way you only manage one bit of function code in one place, instead of a bunch of individual cache behaviors (and possibly a bunch of cache policies).您可以在原始响应 function 中编写所有过滤/缓存逻辑。这样您只需在一个地方管理 function 代码的一位,而不是一堆单独的缓存行为(可能还有一堆缓存策略)。

For example, an origin-response function that looks for a cache-control response header coming from your origin.例如,原始响应 function 查找来自您的来源的缓存控制响应 header。 If it exists, pass it back to the client.如果存在,则将其传回给客户端。 However if it doesn't exist (or if you want to overwrite it with something else) then you can create the response header there.但是,如果它不存在(或者如果您想用其他内容覆盖它),那么您可以在那里创建响应 header。 The edge doesn't care if the cache-control header came from your origin, or from an origin-response Lambda. To the edge, it is all the same.边缘不关心缓存控制 header 是来自您的来源,还是来自来源响应 Lambda。对于边缘来说,一切都是一样的。

Another trick you can use in order to avoid caching and still use the default CloudFront behavior, is: Have a dummy unused query parameter that equals to a unique value for each request.为了避免缓存并仍然使用默认的 CloudFront 行为,您可以使用的另一个技巧是:有一个虚拟未使用的查询参数,该参数等于每个请求的唯一值。

Python example: Python 示例:

import requests
import uuid
requests.get(f'http://my-test-server-x.com/my/path?nochace={uuid.uuid4()}')
requests.get(f'http://my-test-server-x.com/my/path?nochace={uuid.uuid4()}')

Note that both calls will reach destination and will not get response from cache since the uuid.uuid4() will always generate a unique value请注意,这两个调用都将到达目的地并且不会从缓存中获得响应,因为uuid.uuid4()将始终生成唯一值

This works since by default (if not defined otherwise in the Behavior section) the query parameters are part of the cache key这是有效的,因为默认情况下(如果未在“行为”部分中另外定义)查询参数是缓存键的一部分

Note: Doing so will avoid cache use, hence your backend might be loaded with requests.注意:这样做会避免使用缓存,因此您的后端可能会加载请求。

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

相关问题 如何使用 github s3 同步操作将缓存控制设置为 max-age=0 - How to set cache-control to max-age=0 with github s3 sync action Cloudfront 缓存控制标头丢失 - Cloudfront cache-control headers missing 来自 AWS CloudFront 的外部 Next.js 图像的缓存控制策略对于 Google Lighthouse 效率不高 - Cache-Control policy for external Next.js Image coming from AWS CloudFront is not efficient for Google Lighthouse GCP云存储在指定cache-control header后仍然缓存public object - GCP cloud storage still caches public object after specifying cache-control header Cloudfront X-Cache一直显示“云端小姐” - Cloudfront X-Cache shows "Miss from cloudfront" all the time 如何处理从云端下载的浏览器缓存 - How to handle browser cache downloaded from cloudfront 选择 CORS Access-Control-Max-Age 值 - Choose CORS Access-Control-Max-Age value 通过 gsutils 设置缓存控制元数据不会更新响应标头 - Setting Cache-Control metadata via gsutils does not update response headers CloudFront 源路径和缓存行为交互 - CloudFront Origin Path and Cache Behavior interaction 无法从 Cloudfront 登录到 symfony - Cannot login to symfony from Cloudfront
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM