我正在AWS API网关端配置缓存,以提高我的REST API的性能。我正在尝试配置的端点正在使用查询参数。我已经在AWS API Gateway端启用了缓存,但不幸的是,我发现它在构建缓存键时忽略了查询参数。
例如,当我第一次使用查询参数"test1“进行GET调用时
GET https://2kdslm234ds9.execute-api.us-east-1.amazonaws.com/api/test?search=test1此调用的响应保存在缓存中,之后我调用另一个查询参数-- "test2“
GET https://2kdslm234ds9.execute-api.us-east-1.amazonaws.com/api/test?search=test2我再次得到第一次呼叫的响应。
缓存的设置非常简单,我没有找到任何与参数配置相关的东西。

如何配置网关缓存以考虑查询参数?
发布于 2019-07-03 19:04:20
下面是我们如何利用SAM来实现这一点:
AWS API网关控制台中的最终结果必须显示设置缓存复选框为:

应用编程接口网关的*.yml模板为:
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:
# ** Parameter(s) can be set here **
parameters:
- name: "path"
in: "query"
required: "false"
type: "string"
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: "'*'"https://stackoverflow.com/questions/48886715
复制相似问题