[英]Microsoft Azure API Management - Cache policy doesn't work
我曾尝试在 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
,因为您第一次请求时没有缓存。
然后点击“ Outbound ”,可以看到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!
在“入站”下。
请在您身边进行测试,如果结果与我上面提到的相同,则策略<cache-lookup>
在您的 APIM 中可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.