使用以下 SAM 模板:
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
CacheClusterEnabled: true
CacheClusterSize: '0.5'
MethodSettings:
- HttpMethod: GET
CacheTtlInSeconds: 120
ResourcePath: "/getData"
CachingEnabled: true
DefinitionBody:
swagger: 2.0
basePath: /Prod
info:
title: OutService
x-amazon-apigateway-policy:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal: "*"
Action: execute-api:Invoke
Resource:
- execute-api:/*/*/*
paths:
"/getData":
get:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${OutLambda.Arn}/invocations
responses: {}
EndpointConfiguration: PRIVATE
Cors:
AllowHeaders: "'*'"
现在/getData 接受查询参数,例如- /getData?path=abcd/efgh
响应 1234
。
当我使用路径 /getData?path=abcd/efgh
触发 API 时,它会正确缓存 - 响应 1234
。
但是,在我使用不同的查询参数触发 api 之后 - 例如/getData?path=uvw/xyz
期望响应 789
返回为第一个请求缓存的响应 - 1234
。
如何确保缓存应用于具有查询参数的路径?
发出的请求序列及其各自响应的示例:
/getData?path=abcd/efgh
-> 1234
在 11:01:01 返回并缓存
/getData?path=uvw/xyz
-> 789
在 11:01:02 返回并缓存
/getData?path=abcd/efgh
-> 1234
在 11:01:20 从缓存中返回
/getData?path=uvw/xyz
-> 789
在 11:01:31 从缓存中返回
编辑
我正在尝试利用RequestParameters
,然后将它们映射到CacheKeyParameters
,如这两篇文章中所述 - https://medium.com/@dougmoscrop/i-set-up-api-gateway-caching-here-are-some-things-that-surprised-me-7526d954fbe6 & https://theburningmonk.com/2016/04/serverless-enable-caching-on-query-string-parameters-in-api-gateway/ ,但是这两个都使用无服务器框架,我无法弄清楚这如何适合我的模板
最佳答案
AWS API Gateway 控制台中的最终结果必须显示设置缓存复选框为:
我们可以这样实现:
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
CacheClusterEnabled: true
CacheClusterSize: '0.5'
MethodSettings:
- HttpMethod: GET
CacheTtlInSeconds: 120
ResourcePath: "/getData"
CachingEnabled: true
DefinitionBody:
swagger: 2.0
basePath: /Prod
info:
title: OutService
x-amazon-apigateway-policy:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal: "*"
Action: execute-api:Invoke
Resource:
- execute-api:/*/*/*
paths:
"/getData":
get:
# ** Missing param start **
parameters:
- name: "path"
in: "query"
required: "false"
type: "string"
# ** Missing param end **
x-amazon-apigateway-integration:
# ** Key is cached **
cacheKeyParameters:
- method.request.querystring.path
httpMethod: POST
type: aws_proxy
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${OutLambda.Arn}/invocations
responses: {}
EndpointConfiguration: PRIVATE
Cors:
AllowHeaders: "'*'"
关于amazon-web-services - 使用带有查询参数的方法进行 SAM API 网关缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56838064/