我想为我的无服务器功能启用 API Gateway 缓存,但很难理解在哪里以及以何种方式进行缓存。
我试图在我的无服务器函数中设置 queryStringParameters 但这会导致错误,还尝试将它们添加到我的 GLOBAL Api 下但没有运气(也希望避免在全局中这样做)
还检查了我在 API Gateway 中的资源,并为 RequestParams 禁用了缓存,并且 QueryStringParams 从那里丢失。
- 作为参考: https ://awslabs.github.io/serverless-application-model/internals/generated_resources.html
- 作为参考: https ://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessapi
- 作为参考: https ://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
模板
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
......
***********************************编辑************** *******************
发现如果 API Gateway 不知道这些参数,那么它将忽略它以缓存https://forums.aws.amazon.com/thread.jspa?messageID=915838量
我尝试向模板添加多个 methodSetting 条目,似乎 CF 没有忽略它,但结果仍然相同。如果可能的话,我也不确定如何对 queryStringParameters 做同样的事情。
- ResourcePath: "/~1item~1/~1{itemCode}"
CachingEnabled: true
CacheDataEncrypted: true
CacheTtlInSeconds: 60
HttpMethod: "*"
***********************************编辑************** *******************
我更喜欢一种方法来为每个资源下的 RequestParams 和 QueryParams 启用缓存,也就是“AWS::Serverless::Function”
非常感谢您的帮助。