You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS::Serverless::Api 配置缓存键支持任意查询字符串参数

解决方案

AWS API Gateway 的 cacheKeyParameters 不支持通配符来捕获所有未知查询参数,但可以通过**缓存键表达式(cacheKeyExpression)**实现包含所有查询字符串的需求,具体修改如下:

将原配置中的 cacheKeyParameters 替换为 cacheKeyExpression,利用 $context.request.querystring 变量获取完整的查询字符串(包含所有键值对),确保不同查询参数的请求生成不同的缓存键。

修改后的完整 API 定义:

openapi: 3.0.1
info:
  description: API as a proxy to public url
  version: 1.0.0
paths:
  "/{proxy+}":
    x-amazon-apigateway-any-method:
      parameters:
      - name: proxy
        in: path
        required: true
        schema:
          type: string
      responses: {}
      x-amazon-apigateway-integration:
        cacheKeyExpression: "$method.request.path.proxy + $context.request.querystring"
        responses:
          default:
            statusCode: '200'
        requestParameters:
          integration.request.path.proxy: method.request.path.proxy
        uri: 
          Fn::FindInMap : [EnvMap, Ref: Env, proxyUrl]
        passthroughBehavior: when_no_match
        httpMethod: ANY
        type: http_proxy

说明

  • $method.request.path.proxy 保留路径参数作为缓存键的一部分
  • $context.request.querystring 会自动拼接所有查询参数的键值对(格式与请求中的查询字符串一致),确保不同查询参数组合的请求被缓存为独立条目
  • 若坚持使用 cacheKeyParameters,由于 API Gateway 不支持通配符匹配未知查询参数,此方式无法实现需求,因此优先推荐缓存键表达式方案

内容的提问来源于stack exchange,提问作者user2919910

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 17:31:02