Azure Api 管理器缓存删除值策略未删除缓存项
Azure Api Manager cache-remove-value policy not removing the cache item
我在我的 azure api 管理器策略中缓存某些值,在某些情况下删除该项目以清理缓存并从 api.
中取回值
根据我的经验,即使在我使用缓存删除值策略删除该值后,我的下一个 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>
此代码基本上 returns 为空或正文中的“123”取决于是否在删除后找到具有键 Key123 的缓存项。这总是 returns 缓存项“123”的值。
有没有人遇到过这个问题或找到了清理缓存的方法?
如果我连续检查重试,我可以看到该项目有时会在 2 秒后清理,有时会在 1 分钟后清理。我认为 delete 调用是后台的异步或排队调用,因此如果不不断检查,我们无法真正确定它是否已被清理。
更新:
作为目前的实际解决方案,我没有删除,而是实际更新了具有 1 秒持续时间和脏值的缓存项。
发生这种情况是因为缓存删除请求在请求处理管道方面是异步的,即 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>
我在我的 azure api 管理器策略中缓存某些值,在某些情况下删除该项目以清理缓存并从 api.
中取回值根据我的经验,即使在我使用缓存删除值策略删除该值后,我的下一个 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>
此代码基本上 returns 为空或正文中的“123”取决于是否在删除后找到具有键 Key123 的缓存项。这总是 returns 缓存项“123”的值。
有没有人遇到过这个问题或找到了清理缓存的方法?
如果我连续检查重试,我可以看到该项目有时会在 2 秒后清理,有时会在 1 分钟后清理。我认为 delete 调用是后台的异步或排队调用,因此如果不不断检查,我们无法真正确定它是否已被清理。
更新:
作为目前的实际解决方案,我没有删除,而是实际更新了具有 1 秒持续时间和脏值的缓存项。
发生这种情况是因为缓存删除请求在请求处理管道方面是异步的,即 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>