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

如何配置Azure APIM策略按查询参数组合生成不同响应缓存?

Azure APIM 按查询参数组合生成独立缓存的配置及策略对比

我需要配置Azure API Management(APIM)策略,让系统为每个查询参数的组合生成不同的响应缓存。假设存在端点/my-endpoint,接收item_id和language两个查询参数,要求APIM为每一组参数值的组合独立存储响应,比如以下请求的响应需要分别缓存:

/my-endpoint?item_id=4&language=en
/my-endpoint?item_id=4&language=nl
/my-endpoint?item_id=2&language=en
/my-endpoint?item_id=2&language=nl

请问该如何实现?另外,以下两种APIM策略是否都能生效?使用单个<vary-by-query-parameter>标签并用分号分隔参数(如策略1),与使用多个<vary-by-query-parameter>标签分别指定参数(如策略2),在APIM响应缓存机制上是否存在差异?

APIM策略1

<policies>
        <inbound>
            <base />
            <cache-lookup vary-by-developer="false" vary-by-developer-groups="false" allow-private-response-caching="false" must-revalidate="false" downstream-caching-type="none">
                <vary-by-query-parameter>item_id;language</vary-by-query-parameter>
            </cache-lookup>
        </inbound>
        <backend>
            <base />
        </backend>
        <outbound>
            <base />
            <cache-store duration="3600" />
        </outbound>
        <on-error>
            <base />
        </on-error>
</policies>

APIM策略2

<policies>
        <inbound>
            <base />
            <cache-lookup vary-by-developer="false" vary-by-developer-groups="false" allow-private-response-caching="false" must-revalidate="false" downstream-caching-type="none">
                <vary-by-query-parameter>item_id</vary-by-query-parameter>
                <vary-by-query-parameter>language</vary-by-query-parameter>
            </cache-lookup>
        </inbound>
        <backend>
            <base />
        </backend>
        <outbound>
            <base />
            <cache-store duration="3600" />
        </outbound>
        <on-error>
            <base />
        </on-error>
</policies>

解答

实现方法

要实现按查询参数组合生成独立缓存,核心是在cache-lookup策略中通过<vary-by-query-parameter>指定需要参与缓存键计算的查询参数,确保不同参数组合生成唯一的缓存键。同时配合cache-store策略将响应存入缓存,设置缓存时长(示例中为3600秒)。

两种策略的有效性与差异

  1. 有效性:两种策略都能生效,都可以实现为item_id和language的每一组参数组合生成独立缓存的需求。
  2. 缓存机制差异:从APIM的缓存逻辑来看,两种写法的效果完全一致。无论是在单个<vary-by-query-parameter>标签中用分号分隔多个参数,还是使用多个<vary-by-query-parameter>标签分别指定参数,APIM都会将所有指定的查询参数纳入缓存键的计算逻辑,最终都是根据这两个参数的组合来区分不同的缓存条目。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 23:15:34