[英]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.