【问题标题】:Microsoft Azure API Management - Cache policy doesn't workMicrosoft Azure API 管理 - 缓存策略不起作用
【发布时间】:2021-01-12 16:12:43
【问题描述】:

我尝试在 Azure API 管理中设置缓存策略如下:

<policies>
    <inbound>
        <base />
        <cache-lookup vary-by-developer="false" vary-by-developer-groups="false" must-revalidate="true" downstream-caching-type="none" caching-type="internal">
            <vary-by-query-parameter>KontoNr</vary-by-query-parameter>
        </cache-lookup>
        <set-backend-service id="apim-generated-policy" backend-id="LogicApp_GeldEinzahlen_APIM_f597e3433e7847cb9d689c3f95bf1d6d" />
        <validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
            <openid-config url="https://login.microsoftonline.com/1b7c4ba5-7701-49e7-94d8-5ddbc87f8b6e/v2.0/.well-known/openid-configuration" />
            <required-claims>
                <claim name="aud">
                    <value>7068cdb6-0e5c-49c5-aaa8-ec8fc941de22</value>
                </claim>
            </required-claims>
        </validate-jwt>
        <set-variable name="isKontoNr" value="@(context.Request.MatchedParameters["kontoNr"].ToString().Length != 10)" />
        <choose>
            <when condition="@(context.Variables.GetValueOrDefault<bool>("isKontoNr"))">
                <return-response>
                    <set-status code="400" reason="Bad Request" />
                    <set-header name="WWW-Request" exists-action="override">
                        <value>Generell error="kontoNr invalid"</value>
                    </set-header>
                </return-response>
            </when>
        </choose>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <cache-store duration="1000" />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

我有定价层“开发者”,所以内部缓存必须可用,但如果我提出请求,我总是会得到以下响应:

HTTP/1.1 200 OK

cache-control: no-cache
content-encoding: gzip
content-type: text/plain; charset=utf-8
date: Tue, 12 Jan 2021 15:54:26 GMT
expires: -1
pragma: no-cache
strict-transport-security: max-age=31536000; includeSubDomains

无论我在标头中发送什么,缓存控制始终是“无缓存”。

我该如何解决?是否需要一些其他配置或者我是否启用了某些东西?

【问题讨论】:

  • no-cache pragma 不应该真正指示是否正在使用缓存。转到 APIM 中的测试页面,运行查询并查看跟踪。在那里你会看到是否使用了缓存查找
  • 嗨帕斯卡,如果我在下面提供的答案对您的问题有帮助,请mark(单击我的答案旁边的复选标记将其从灰色切换为填充)它为“已接受”,提前致谢。

标签: azure azure-api-management cache-control policies api-management


【解决方案1】:

正如 cmets 中提到的那样,消息 cache-control: no-cache 不应该真正指示缓存是否正在使用。 &lt;cache-lookup&gt; 策略在您的 APIM 中是正确的。

当您第一次请求 api(用于 APIM 页面中的测试)时,单击“Trace” --> “Inbound”。您可以找到消息Cache lookup resulted in a miss,因为您第一次请求时没有缓存。

然后点击“Outbound”,可以找到Response will be buffered during streaming and stored in the cache after it is received in full

那么,由于你指定缓存存储1000秒,所以如果你在1000秒后再次请求,你可以在“Inbound”下找到消息Cache lookup resulted in a hit!

请在您身边测试,如果结果与我上面提到的相同,则策略 &lt;cache-lookup&gt; 在您的 APIM 中可以正常工作。

【讨论】:

  • 谢谢。我发现默认情况下没有为带有 Authorization 标头的请求启用缓存。允许缓存私人响应解决了这个问题。
  • 在策略的 元素上设置 allow-private-response-caching="true" 属性对我有用。
猜你喜欢
  • 2020-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-28
相关资源
最近更新 更多