How do I specify a policy so that Azure API Management creates a different response cache for each combination of query parameters?
Suppose I have an endpoint /my-endpoint that takes 2 query parameters (item_id and language).
I want API Management to make a different cache for each combination of my query parameter values.
For example, I want the following requests to store different cache values for the responses:
/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
How do I do this?
In particular, do both of the following APIM policies (APIM Policy 1 and APIM Policy 2) work? Or is there a difference in how Azure API Management response caching works when I use a single tag with values separated by commas (see APIM Policy 1), or when I use multiple tags, each with a different value (see APIM Policy 2)?
APIM Policy 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 Policy 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>
If you don't get the "vary-by-query-parameter" to work, you could do something like this:
<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" />
In this way you combine your parameters to get unique cache variabel names.
Given your case above you will have 4 different cache variables:
my-variable-4-en
my-variable-4-nl
my-variable-2-en
my-variable-2-nl
They will all contain different response bodies.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.