Set response cache duration
This article shows an Azure API management policy sample that demonstrates how to set response cache duration using maxAge value in Cache-Control header sent by the backend. To set or edit a policy code, follow the steps described in Set or edit a policy. To see other examples, see policy samples.
Policy
Paste the code into the inbound block.
<!-- This snippet demonstrates how to set response cache duration using maxAge value in Cache-Control header sent by the backend. -->
<!-- Copy the following snippet into the outbound section. -->
<policies>
<inbound>
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<!-- The following policy can be tested by setting up an API and operation mapped to GET http://httpbin.org/cache/{duration} -->
<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;
}"
/>
</outbound>
<on-error>
<base />
</on-error>
</policies>
Next steps
Learn more about APIM policies:
Povratne informacije
Pošalјite i prikažite povratne informacije za