我已将缓存存储和缓存查找策略添加到api路由,缓存以Cache-Control标头中max-age指令指示的持续时间存储。这是政策:
<inbound>
<base />
<cache-lookup vary-by-developer="false" vary-by-developer-groups="false" must-revalidate="true" downstream-caching-type="public">
<vary-by-header>Accept</vary-by-header>
<vary-by-header>Accept-Charset</vary-by-header>
<vary-by-header>Authorization</vary-by-header>
</cache-lookup>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<set-variable name="max-age" value="@{
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):0;
}" />
<choose>
<when condition="@((int)context.Variables["max-age"] > 0)">
<cache-store duration="@((int)context.Variables["max-age"])" />
</when>
</choose>
</outbound>
我尝试频繁调用路由,但是在Application Insights的请求日志中,我没有发现响应被缓存的迹象。 customDimensions.Cache
始终设置为“无”。
编辑: 我通过使缓存持续时间相对较长并比较了后端记录的请求数来测试缓存,但是缓存命中数不确定。