Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

Microsoft Azure API Management - Cache policy doesn't work

I have tried to set a caching policy in Azure API Management as follows:

<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>

I have pricing tier "Developer" so the internal cache has to be available but if i make a request i always get the following response:

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

cache-control is always 'no-cache' no matter what i send in the header.

How can i fix it ? Is some other configuration needed or have i enable something?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As silent mentioned in comments, the message cache-control: no-cache should not really be indicative whether the cache was being used. The <cache-lookup> policy is correct in your APIM.

When you request the api(for test in APIM page) first time, click "Trace" --> "Inbound". You can find the message Cache lookup resulted in a miss because there is no cache when you request first time. enter image description here

And then click "Outbound", you can find Response will be buffered during streaming and stored in the cache after it is received in full. enter image description here

Then, since you specified the cache be stored for 1000 seconds, so if you request again in 1000 seconds, you can find the message Cache lookup resulted in a hit! under "Inbound". enter image description here

Please test in your side, and if the result is same with what I mentioned above, the policy <cache-lookup> works fine in your APIM.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...