如何在Azure Api管理中测试缓存策略

时间:2020-04-28 11:27:52

标签: azure-application-insights azure-api-management

我已将缓存存储和缓存查找策略添加到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始终设置为“无”。

编辑: 我通过使缓存持续时间相对较长并比较了后端记录的请求数来测试缓存,但是缓存命中数不确定。

0 个答案:

没有答案