我在AWS API Gateway端配置缓存以提高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
我再次回复第一次电话。
缓存设置非常简单,我没有找到与参数配置相关的内容。
如何配置网关缓存以考虑查询参数?
答案 0 :(得分:3)
您需要在Gateway API面板中配置此选项。
答案 1 :(得分:1)
以下是我们如何利用SAM实现此目标的方法:
AWS API Gateway控制台中的最终结果必须显示设置缓存复选框为:
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: "'*'"