【问题标题】:AWS API Gateway caching ignores query parametersAWS API Gateway 缓存忽略查询参数
【发布时间】:2018-07-30 21:37:34
【问题描述】:

我正在 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

我再次收到第一次电话的回复。

缓存的设置很简单,我没有找到与参数配置相关的东西。

如何配置网关缓存以考虑查询参数?

【问题讨论】:

    标签: amazon-web-services caching aws-api-gateway


    【解决方案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: "'*'"
    

    【讨论】:

      【解决方案2】:

      您需要在 Gateway API 面板中配置此选项。

      • 选择您的 API 并点击资源。
      • 选择方法并查看 URL 查询字符串会话。
      • 如果没有查询字符串,添加一个。
      • 标记查询字符串的“缓存”选项。
      • 执行最终测试,最后部署更改。

      Screenshot

      【讨论】:

      • 如果我需要它来进行 POST 请求怎么办?应该使用什么将响应键入缓存?
      猜你喜欢
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 2022-06-30
      • 1970-01-01
      • 2022-10-06
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      相关资源
      最近更新 更多