简体   繁体   中英

Azure API policy for cache

i need to add the below cache response to the apim policy

HTTP Caching Headers: List of caching related headers defined within the HTTP spec.

  1. Cache-Control (Response Only)

  2. no-cache

  3. no-store

  4. Expires

  5. If-Modified-Since

  6. Last-Modified (Response Only)

I am unable to understand how to implement the above same in policy I have tried with the below one, but not able to get how to add the no cache. no store these directives.

As well as, how to add those expiry, modification in responses.

     <cache-lookup vary-by-developer="false"
   vary-by-developer-groups="false" downstream-caching-type="public"
   must-revalidate="true" >
       <vary-by-header>Accept</vary-by-header>
       <vary-by-header>Accept-Charset</vary-by-header>
       <vary-by-header>Accept-Encoding</vary-by-header>
       <vary-by-header>Accept-Language</vary-by-header> </cache-lookup>
   
   <cache-store duration="@{
           var header = context.Response.Headers.GetValueOrDefault("Cache-Control","");
           var maxAge = Regex.Match(header, @"max-age=(?<maxAge>\d+)").Groups["maxAge"]?.Value;
           return (!string.IsNullOrEmpty(maxAge))?int.Parse(maxAge):300;
       }"  />

I can see you have raised this issue in MS Q&A Forum regarding the Custom Cache Policy Implementation in Azure APIM.

Thanks to the MSFT User @MuthuKumaranMurugaachari for pointing the user in right direction by suggesting the below point.

I quickly looked at the attached policy snippet and want to confirm about statement Make two identical API requests . If you want to check Cache-Control header and make two Http requests, you can use send-request policy. Here is doc reference: Send request and sample scenarios are covered here .

Meaning you can insert send-request policy in when conditions and validate based on your need. However, this would add complexity to the policy since multiple requests are made in the outbound section.

I found an MS Doc where it is provided the sample cache-control policy for the response caching.

If any of the community user facing similar issue, can follow up on this Q&A Forum # 1165407 .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM