Search code examples
azureazure-api-managementazure-caching

'vary' in APIM caching policies


I am learning Apim policies. In caching policies there is many elements of <vary-by.... In description of microsoft docs, it is mentionned that it caches by for example developer, query parameter... What does it mean exactly? Does this have a relation to refreshing values?


Solution

  • I guess you are referring to https://learn.microsoft.com/en-us/azure/api-management/api-management-caching-policies#GetFromCache

    <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-header>Accept</vary-by-header>
      <!-- should be present in most cases -->
      <vary-by-header>Accept-Charset</vary-by-header>
      <!-- should be present in most cases -->
      <vary-by-header>Authorization</vary-by-header>
      <!-- should be present when allow-private-response-caching is "true"-->
      <vary-by-header>header name</vary-by-header>
      <!-- optional, can repeated several times -->
      <vary-by-query-parameter>parameter name</vary-by-query-parameter>
      <!-- optional, can repeated several times -->
    </cache-lookup>
    

    By default the cache lookup would only use the URL path as a reference (the cache key) to find the cached item.

    If you however wanted also to vary the cache lookup depending on a certain header or query parameter - with that extending the cache key - you would use <vary-by-header> or <vary-by-query-parameter> - so that for the same URL path responses are cached based on additional header and/or query parameters.

    vary-by-developer or vary-by-developer-groups would extend the cache key with user or the group of the user assigned to the subscription key used - so that for the same URL path responses are cached based on who is calling the API operation.