Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
<!-- Custom caching policy for on HTTP POST for Azure API Management:
1. Policy looks in the Request body - 'cacheKey' property which then used as cache key.
Expected values are: <null>, ALL or NOEXPIRED
Defaults to ALL in case <null>
2. Cache expiration set to 60 seconds/1 minute
<base />
<!-- CACHING: checks for cacheKey property in message body !-->
<set-variable name="cacheKey" value="@(context.Request.Body?.As<JObject>(preserveContent: true)["cacheKey"]?.ToString() ?? "ALL")" />
<cache-lookup-value key="@((string)context.Variables["cacheKey"])" variable-name="cachedResponseValue" />
<when condition="@(context.Variables.ContainsKey("cachedResponseValue"))">
<set-header name="Content-Type" exists-action="override">application/json</set-header>
<base />
<base />
<set-variable name="responseValue" value="@(context.Response.Body.As<string>(preserveContent: true))" />
<cache-store-value key="@((string)context.Variables["cacheKey"])" value="@((string)context.Variables["responseValue"])" duration="60" />
<base />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment