你如何为云端设置永久缓存?

How do you set up permanent caching for cloudfront?

我已通读文档并尝试设置永久云端缓存。这个想法是,一旦你下载了一个图像,你就不需要再次下载它。我正在查看我的网络选项卡,但我认为不是。谁能告诉我这个文件的缓存设置是否正确?

https://d2t4fc8ff77neh.cloudfront.net/cardSrcMedia/1abqkohet_plain_red_heart_graphic_with_a_drop_shadow_0071-0910-0216-0922_SMU.jpg

我的headers:

var headers = {
  'Content-Length': options.data.length,
  'Vary': 'Accept-Encoding',
  'Expires': 1000 * 60 * 60 * 24 * 365 * 5,
  'x-amz-acl': 'public-read',
  'Content-Type': options.type
}

CloudFront 似乎正在缓存您的图片,但您的过期 header 值无效。

来自 HTTP 1.1 规范 (https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html):

The format is an absolute date and time as defined by HTTP-date in section 3.3.1; it MUST be in RFC 1123 date format

HTTP/1.1 clients and caches MUST treat other invalid date formats, especially including the value "0", as in the past (i.e., "already expired").

To mark a response as "never expires," an origin server sends an
Expires date approximately one year from the time the response is
sent. HTTP/1.1 servers SHOULD NOT send Expires dates more than one
year in the future.

使用 Cache-control header 可能会更好,因为它会取代 Expires header。像这样的东西应该产生相同的效果:

Cache-Control: max-age=31536000