Search code examples
c#.net-corecachingazure-api-management

APIM Cache base on query parameter


I am working on Azure APIM. I have a endpoint as below. which take QueryParam as input. I would like to cache for 2 mins(If QueryParam is same).

In order to cache for 2 mins. I have to use outbound => Cache-store duration. BUT did not find any example HOW TO CHECK THE Query parameter

<policies>
    <inbound>
        <base />
    </inbound>
    <outbound>
        <cache-store duration="seconds" />
        <base />
    </outbound>
</policies>

[HTTPGET]
Public GetProduct(QueryParam param) 
{
}

Class QueryParam {
 String productCode {get;set;}
 DateTime PurchasedDate {get;set;}
}

Solution

  • Example from Microsoft Docs:

    <policies>
        <inbound>
            <base />
            <cache-lookup vary-by-developer="false" vary-by-developer-groups="false" downstream-caching-type="none" must-revalidate="true" caching-type="internal" >
                <vary-by-query-parameter>version</vary-by-query-parameter>
            </cache-lookup>
        </inbound>
        <outbound>
            <cache-store duration="seconds" />
            <base />
        </outbound>
    </policies>
    

    When using vary-by-query-parameter, you might want to declare the parameters in the rewrite-uri template or set the attribute copy-unmatched-params to false. By deactivating this flag, parameters that aren't declared are sent to the back end.

    Policy statement

    <cache-lookup vary-by-developer="true | false" vary-by-developer-groups="true | false" caching-type="prefer-external | external | internal" downstream-caching-type="none | private | public" must-revalidate="true | false" allow-private-response-caching="@(expression to evaluate)">
      <vary-by-query-parameter>parameter name</vary-by-query-parameter>
      <!-- optional, can repeated several times -->
    </cache-lookup>