admin管理员组

文章数量:1359245

I'd like to know if a cache-lookup was successful in azure API Management. I'm attempting to queue requests while a cache lookup is happening and then release them once it's finished. It's working well but right now I'd like to set my cache in progress variable conditionally dependent on if the cache lookup fails and I know the request is headed to the backend. Right now cache-store-value in the inbound is called for all requests, not just ones that didn't hit the cache. So that could theoretically limit concurrency.

<inbound>
    <base />
    <set-variable name="cacheInProgressKey" value="@(context.Request.Url + "-" + context.Request.Headers.GetValueOrDefault("Accept", "") + "-" + context.Request.Headers.GetValueOrDefault("Accept-Charset", ""))" />
    <cache-lookup-value key="@(context.Variables["cacheInProgressKey"] as string)" variable-name="cacheInProgressValue" />
    <retry condition="@(context.Variables.GetValueOrDefault("cacheInProgressValue", "false") != "false")" count="5" interval="1" first-fast-retry="false">
        <set-variable name="cacheInProgressValue" value="false" />
        <cache-lookup-value key="@(context.Variables["cacheInProgressKey"] as string)" variable-name="cacheInProgressValue" />
    </retry>
    <cache-lookup vary-by-developer="false" vary-by-developer-groups="false" allow-private-response-caching="true" downstream-caching-type="none">
        <vary-by-header>Accept</vary-by-header>
        <vary-by-header>Accept-Charset</vary-by-header>
    </cache-lookup>
    <cache-store-value key="@(context.Variables["cacheInProgressKey"] as string)" value="true" duration="5" />
</inbound>
<backend>
    <base />
</backend>
<outbound>
    <base />
    <cache-remove-value key="@(context.Variables["cacheInProgressKey"] as string)" />
    <cache-store duration="60" />
</outbound>

Would also like to know if it's possible to get the key used in cache-lookup so I can use that to build my cachInProgressKey instead of having to recreate a similar key in the policy based on query parameters and headers.

I'd like to know if a cache-lookup was successful in azure API Management. I'm attempting to queue requests while a cache lookup is happening and then release them once it's finished. It's working well but right now I'd like to set my cache in progress variable conditionally dependent on if the cache lookup fails and I know the request is headed to the backend. Right now cache-store-value in the inbound is called for all requests, not just ones that didn't hit the cache. So that could theoretically limit concurrency.

<inbound>
    <base />
    <set-variable name="cacheInProgressKey" value="@(context.Request.Url + "-" + context.Request.Headers.GetValueOrDefault("Accept", "") + "-" + context.Request.Headers.GetValueOrDefault("Accept-Charset", ""))" />
    <cache-lookup-value key="@(context.Variables["cacheInProgressKey"] as string)" variable-name="cacheInProgressValue" />
    <retry condition="@(context.Variables.GetValueOrDefault("cacheInProgressValue", "false") != "false")" count="5" interval="1" first-fast-retry="false">
        <set-variable name="cacheInProgressValue" value="false" />
        <cache-lookup-value key="@(context.Variables["cacheInProgressKey"] as string)" variable-name="cacheInProgressValue" />
    </retry>
    <cache-lookup vary-by-developer="false" vary-by-developer-groups="false" allow-private-response-caching="true" downstream-caching-type="none">
        <vary-by-header>Accept</vary-by-header>
        <vary-by-header>Accept-Charset</vary-by-header>
    </cache-lookup>
    <cache-store-value key="@(context.Variables["cacheInProgressKey"] as string)" value="true" duration="5" />
</inbound>
<backend>
    <base />
</backend>
<outbound>
    <base />
    <cache-remove-value key="@(context.Variables["cacheInProgressKey"] as string)" />
    <cache-store duration="60" />
</outbound>

Would also like to know if it's possible to get the key used in cache-lookup so I can use that to build my cachInProgressKey instead of having to recreate a similar key in the policy based on query parameters and headers.

Share Improve this question edited Mar 27 at 15:04 Will asked Mar 27 at 14:31 WillWill 2,88021 silver badges23 bronze badges 1
  • What response are you getting for cacheInProgressValue? – Pravallika KV Commented Apr 4 at 12:05
Add a comment  | 

1 Answer 1

Reset to default 0

To determine if cache-lookup was successful in Azure APIM, inspect the value of the variable cachedResponse which was assigned to store the result of the cache lookup.

I have used below policy to test the requirement:

<policies>
    <inbound>
        <base />
        <set-variable name="cacheKey" value="@(context.Request.Url.ToString())" />
        <set-variable name="cacheInProgressKey" value="@(context.Variables["cacheKey"] + "-in-progress")" />
        <cache-lookup-value key="@(context.Variables["cacheKey"] as string)" variable-name="cachedResponse" />
        <cache-lookup-value key="@(context.Variables["cacheInProgressKey"] as string)" variable-name="cacheInProgressFlag" />

        <choose>

            <when condition="@(context.Variables.GetValueOrDefault<string>("cachedResponse") == null && context.Variables.GetValueOrDefault<string>("cacheInProgressFlag") == null)">
                <set-variable name="cacheInProgressFlag" value="true" />
                <cache-store-value key="@(context.Variables["cacheInProgressKey"] as string)" value="true" duration="300" />
            </when>
            <when condition="@(context.Variables.GetValueOrDefault<string>("cachedResponse") != null)">

                <set-body>@(context.Variables.GetValueOrDefault<string>("cachedResponse"))</set-body>
                <return-response />
            </when>
        </choose>
    </inbound>
    <backend>
        <choose>

            <when condition="@(context.Variables.GetValueOrDefault<string>("cachedResponse") == null && context.Variables.GetValueOrDefault<string>("cacheInProgressFlag") == "true")">
                <forward-request />
            </when>
        </choose>
    </backend>
    <outbound>
        <base />
        <choose>
            <when condition="@(context.Response.StatusCode == 200 && context.Variables.GetValueOrDefault<string>("cacheInProgressFlag") == "true")">
                <set-variable name="responseBody" value="@(context.Response.Body.As<string>(preserveContent: true))" />
                <cache-remove-value key="@(context.Variables["cacheInProgressKey"] as string)" />
                <cache-store-value key="@(context.Variables["cacheKey"] as string)" value="@(context.Variables["responseBody"] as string)" duration="3600" />
            </when>
        </choose>
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

The cachedResponse variable is used in a choose condition to check if the cache-lookup succeeded i.e., if cachedResponse value is not null.

Invoking cache-lookup-value to search the cache for the key. If the key exists, the corresponding value will be stored in cachedResponse.

Cache-lookup status is set to hit as below:

Performing a cache-lookup-value to check if the cache in-progress flag exists for the key cacheInProgressKey. The key is constructed by appending "-in-progress" to the regular cache key.

本文标签: Azure API Management policy how to know if cachelookup was successfulStack Overflow