4

使用以下 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

当我使用路径触发 API 时/getData?path=abcd/efgh,它会被正确缓存 - 以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 is returned from the cache at 11:01:31

EDIT

I am trying to utilise the RequestParameters and then map them to CacheKeyParameters as explained in these 2 articles - 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/, but both of these use the serverless framework I am unable to figure out how this would fit into my template

4

1 回答 1

4

The end result in the AWS API Gateway console must display that the set caching checkbox is:

enter image description here

We can achieve it like this:

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: "'*'"
于 2019-07-01T15:09:31.430 回答