问题
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.
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.
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.
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.
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?
回答1:
You have two options:
1-Call the invalidation API each time someone edits your member list. For example, if someone adds/remove/edit member list (not get request), you should call invalidation API in Cloudfront and invalidate the path
/projects/PROJECT_ID/members
2-Control cache on your API side, also you can set header for some resources to cache in Cloudfront for sometimes.
For more info about CloudFront invalidation API, check the following link
https://docs.aws.amazon.com/cloudfront/latest/APIReference/API_CreateInvalidation.html
来源:https://stackoverflow.com/questions/49705089/how-to-invalidate-cloudfront-cache-of-dynamic-content-when-content-is-updated