如何指定策略,以便Azure管理为每个查询参数组合创建不同的响应缓存?
假设我有一个端点/我的端点,它接受2个查询参数(item_id和language)。
我希望API管理为我的查询参数值的每个组合创建一个不同的缓存。
例如,我希望以下请求为响应存储不同的缓存值:
/my-endpoint?item_id=4&language=en
/my-endpoint?item_id=4&language=nl
/my-endpoint?item_id=2&language=en
/my-endpoint?item_itd=2&language=nl我该怎么做?
特别是,下面的APIM策略(APIM策略1和APIM策略2)是否都有效?或者当我使用由逗号分隔的值的单个标记(请参见APIM 1),或者使用多个标记,每个标记都有不同的值(请参见APIM 2)时,Azure API管理响应缓存的工作方式是否有区别?
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>发布于 2022-10-03 19:57:36
如果您不能让“逐个查询”参数工作,您可以这样做:
<cache-store-value key="@("my-variable-" + context.Request.MatchedParameters.GetValueOrDefault("item_id","")) + "-" + context.Request.MatchedParameters.GetValueOrDefault("language",""))" value="@(context.Response.Body.As<String>(true))" duration="3600" />通过这种方式,您可以组合您的参数以获得唯一的缓存变量名。
鉴于以上情况,您将有4个不同的缓存变量:
我的变量-4-en
我的变量-4nl
我的变量-2-en
我的变量2-nl
它们都将包含不同的反应机构。
https://stackoverflow.com/questions/73919621
复制相似问题