首页 > 解决方案 > 如何在 Azure API 管理中测试缓存策略

问题描述

我已向 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始终设置为“无”。

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

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

解决方案


推荐阅读