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

Pas*_*her 5 cache-control azure policies azure-api-management 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>
Run Code Online (Sandbox Code Playgroud)

我的定价层为“开发人员”,因此内部缓存必须可用,但如果我提出请求,我总是会得到以下响应:

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
Run Code Online (Sandbox Code Playgroud)

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

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

Hur*_*hen 2

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

当您第一次请求api(在APIM页面进行测试)时,点击“ Trace ” --> “ Inbound ”。Cache lookup resulted in a miss由于第一次请求时没有缓存,所以 可以查到该消息。在此输入图像描述

然后点击“出站”,即可找到Response will be buffered during streaming and stored in the cache after it is received in full在此输入图像描述

然后,由于您指定缓存存储1000秒,所以如果您在1000秒内再次请求,您可以Cache lookup resulted in a hit!在“ Inbound ”下找到该消息。 在此输入图像描述

请您在您这边进行测试,如果结果与我上面提到的相同,则该策略<cache-lookup>在您的 APIM 中运行良好。