首页 文章

Azure Api Manager缓存删除值策略不删除缓存项

提问于
浏览
1

我在我的azure api manager策略中缓存某些值,并在某些情况下删除该项以清理缓存并从api中检索值 .

根据我的经验,即使我使用cache-remove-value策略删除了值,我的下一个api调用仍然会在缓存中找到该值 . 这是一个示例代码:

<cache-store-value key="Key123" value="123" duration="300" />
    <cache-lookup-value key="Key123" variable-name="CacheVariable" />
    <cache-remove-value key="Key123" />
    <cache-lookup-value key="Key123" default-value="empty" variable-name="CacheVariable2" />
    <return-response>
        <set-status code="504" reason="" />
        <set-body>@(context.Variables.GetValueOrDefault<string>("CacheVariable2"))</set-body>
    </return-response>

根据是否在删除后找到具有键Key123的缓存项,此代码基本上在空体中返回空或“123” . 这始终返回缓存项的值“123” .

有没有人遇到过这个问题或找到了清理缓存的方法?

如果我继续检查重试,我可以看到有时在2秒钟,有时1分钟后清洁该物品 . 我认为删除调用是后台中的异步或排队调用,因此如果不进行连续检查,我们无法确定是否已清除 .

更新:

作为现在的实际解决方案,我实际上用1秒的持续时间和脏值更新缓存项,而不是删除 .

1 回答

  • 0

    发生这种情况是因为缓存删除请求在请求处理管道方面是异步的,即APIM在继续请求之前不等待缓存项被删除,因此有可能在删除请求之后仍然检索它,因为它还没有被发送 .

    根据您的情况更新:为什么不尝试这样的事情:

    <policies>
    <inbound>
        <base />
    </inbound>
    <backend>
        <retry condition="@(context.Response.StatusCode == 200)" count="10" interval="1">
            <choose>
                <when condition="@(context.Variables.GetValueOrDefault("calledOnce", false))">
                    <send-request mode="new" response-variable-name="response">
                        <set-url>https://EXTERNAL-SERVICE-URL</set-url>
                        <set-method>GET</set-method>
                    </send-request>
                    <cache-store-value key="externalResponse" value="EXPRESSION-TO-EXTRACT-DATA" duration="300" />
                    <!--... or even store whole response ...-->
                    <cache-store-value key="externalResponse" value="@((IResponse)context.Variables["response"])" duration="300" />
                </when>
                <otherwise>
                    <cache-lookup-value key="externalResponse" variable-name="externalResponse" />
    
                    <choose>
                         <when condition="@(context.Variables.ContainsKey("externalResponse"))">
                             <!-- Do something with cached data -->
                         </when>
                         <otherwise>
                             <!-- Call extenal service and store in cache again -->
                         </otherwise>
                     </choose>
    
                    <set-variable name="calledOnce" value="@(true)" />
                </otherwise>
            </choose>
            <forward-request />
        </retry>
    </backend>
    <outbound>
        <base />
    </outbound>
    

相关问题