更新内容时如何使动态内容的CloudFront缓存无效 [英] How to invalidate CloudFront cache of dynamic content when content is updated

查看:459
本文介绍了更新内容时如何使动态内容的CloudFront缓存无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AWS Lambda,API Gateway和CloudFront开发Web API。

CloudFront当前用作API Gateway某些终结点的反向代理,完全禁用了缓存行为。

I am developing Web APIs using AWS Lambda, API Gateway and CloudFront.
CloudFront is currently used as reverse proxy to some endpoints of API Gateway, cache behaviors are disabled at all.

有几种常用的API,它们的内容很少更新(例如,每周一次或每月一次),因此是缓存速度更快的候选对象响应时间。

There are several frequently-used APIs, of which contents are rarely-updated (e.g. once a week or once a month), therefore are candidates for caching for faster response time.

但是,我想知道如何在内容更新时使经常使用但很少更新的动态内容的CloudFront缓存无效

我的观察是,由于CloudFront不会将请求传递给API原始端,因此CloudFront会保留较旧内容的缓存,直到缓存达到最大使用期限为止。

However, I am wondering how to invalidate such CloudFront cache of frequently-used-but-rarely-updated dynamic content when their contents are updated.
My observation is that CloudFront holds cache of older content, since CloudFront does not pass requests to API origin, until cache reaches max-age.

例如:假设我有一个 GET / projects / PROJECT_ID / members API。

项目成员很少更新,但是一旦通过 PUT / projects / PROJECT_ID / members API,应该使缓存无效,以交付项目成员的最新内容。

E.g.: Let's say I have a GET /projects/PROJECT_ID/members API.
Project members are rarely updated, but once member added/removed via PUT /projects/PROJECT_ID/members API, cache should be invalidated to deliver the freshly-updated content of project members.

我应该使用 / projects / ABC / members )。每次更新内容后( PUT / projects / ABC / members 无效API

还是应该避免缓存此类动态内容?

Should I invalidate the very-particular URL (/projects/ABC/members for above example) using invalidation API each time after updating their content (PUT /projects/ABC/members invoked) ?
Or, should I avoid to cache such dynamic content?

推荐答案

有多种方法处理此问题:

There are multiple ways to handle this:


  1. CloudFront缓存无效。

    如果您使用S3作为来源,那么您可以在lambda函数上创建S3事件。如果S3中有更改,此lambda将自动使CloudFront缓存无效。

  1. CloudFront Cache Invalidation.
    If you are using S3 as origin, then you can create an S3 event on lambda function. This lambda will automatically invalidate CloudFront cache if there is a change in S3.

使用诸如Min之类的缓存头。 TTL,最大值TTL或默认TTL定义在CloudFront调用原始对象之前您希望这些对象在缓存中的时间。

Use cache headers like Min. TTL, Max. TTL or Default TTL to define how much time you want those objects in cache before CloudFront makes a call to origin.

在对象上使用对象版本控制。因此,该CloudFront每次都会获取对象的最新版本。

Use object versioning on you object. So, that CloudFront will fetch latest version of the objects everytime.

有关这些的更多信息,请参阅CloudFront文档。

Can refer CloudFront docs for more information on these.

这篇关于更新内容时如何使动态内容的CloudFront缓存无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆