SAM API 使用带有查询参数的方法进行网关缓存
SAM API Gateway caching with a method with query parameters
使用以下 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 网关控制台中的最终结果必须显示设置缓存复选框为:
我们可以这样实现:
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: "'*'"
使用以下 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 网关控制台中的最终结果必须显示设置缓存复选框为:
我们可以这样实现:
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: "'*'"