我想为我的无服务器功能启用API缓存,但很难理解在哪里和以何种方式进行缓存。
我试图在我的无服务器函数中设置queryStringParameters,但这会导致错误,也试图将它们添加到我的全局Api中,但没有运气(也希望避免在全局中这样做)
还检查了我在API中的资源,并禁用了RequestParams和QueryStringParams的缓存。
模板
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Api:
EndpointConfiguration: REGIONAL
CacheClusterEnabled: true
CacheClusterSize: "0.5"
MethodSettings:
- CachingEnabled: true
CacheDataEncrypted: true
CacheTtlInSeconds: 60
HttpMethod: "*"
ResourcePath: "/*"
Resources:
......
GetItem:
Type: 'AWS::Serverless::Function'
Properties:
Handler: GetItem.handler
Runtime: nodejs8.10
Timeout: 20
CodeUri: "codes"
Events:
GetItem:
Type: Api
Properties:
Path: /item/{itemCode}
Method: get
......***********************************EDIT*********************************
如果API不知道params,那么它就会忽略它来缓存
我尝试过向模板中添加多个methodSetting条目,而且CF似乎没有忽略它,但结果仍然相同。如果可能的话,我也不知道如何为queryStringParameters做同样的事情。
- ResourcePath: "/~1item~1/~1{itemCode}"
CachingEnabled: true
CacheDataEncrypted: true
CacheTtlInSeconds: 60
HttpMethod: "*"***********************************EDIT*********************************
我更喜欢在每个资源(即'AWS::Serverless::Function‘)下为RequestParams和QueryParams启用缓存的方法
非常感谢你的帮助。
发布于 2019-09-30 13:31:36
到目前为止,SAM框架没有支持这样做。他们计划发布一个更新,在这个链接上启用更多的功能:https://github.com/awslabs/serverless-application-model/issues/1140。
在此之前,我能够找到的唯一解决方案是在API网关前面创建一个CloudFront发行版,这是一种资源的浪费,但它工作得很好。
https://stackoverflow.com/questions/57907320
复制相似问题