azure - Microsoft Azure API 管理 - 缓存策略不起作用

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

我尝试在 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

无论我在 header 中发送什么内容,缓存控制始终为“无缓存”。

我该如何修复它?是否需要其他配置或者我是否启用了某些功能?

最佳答案

正如评论中无声提到的,消息 cache-control: no-cache不应该真正指示缓存是否正在使用。 <cache-lookup>您的 APIM 中的策略是正确的。

第一次请求api(在APIM页面测试)时,点击“Trace”-->“Inbound”。您可以找到消息Cache lookup resulted in a miss因为第一次请求的时候没有缓存。 enter image description here

然后点击“出站”,可以找到Response will be buffered during streaming and stored in the cache after it is received in fullenter image description here

然后,由于您指定了缓存保存1000秒,所以如果您在1000秒后再次请求,您可以找到消息Cache lookup resulted in a hit!在“入站”下。 enter image description here

请您这边测试一下,如果结果和我上面说的一样,政策<cache-lookup>在您的 APIM 中运行良好。

关于azure - Microsoft Azure API 管理 - 缓存策略不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65687689/

相关文章:

asp.net-mvc - 为什么我不能在 Web API2 中设置 Cache-Control header

node.js - Express.js 如何自动处理 Cache-Control?

azure - 可用性测试摘要小部件显示 1 个 Application Insights 中每个单独测试的结果

c# - 使现有实体实现 TableEntity

azure - 如果在 azure 开发者门户中输入错误的用户名,则不会显示错误消息

azure - 如何禁用Azure API管理开发者门户中的try it功能?

Azure 媒体播放器 - 缓存控制

azure - 使用 azure python/java sdk 在 IoT 中心创建 azure IoT 设备

c# - 避免在 Azure 表存储中进行新的更新插入

azure - 是否有任何 Powershell 脚本或 az 命令可以获取用于 API 管理的产品及其订阅 ID 列表?