[英]How to specify rewrite url for query string parameters in Azure API Management
[英]How do I specify a policy so that Azure API Management creates a different response cache for each combination of query parameters?
如何指定策略以便 Azure API 管理為每個查詢參數組合創建不同的響應緩存?
假設我有一個帶有 2 個查詢參數(item_id 和語言)的端點 /my-endpoint。
我希望 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)時,管理響應緩存的工作方式是否存在差異?
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-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
我的變量 4-nl
我的變量 2-en
我的變量 2-nl
它們都將包含不同的響應主體。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.