在 Azure APIM 中,我有一个查询 SQL table 的 API。我为 API 创建了一个缓存策略,使其在 300 秒后过期。然后我创建了一个脚本来每 10 分钟调用一次 API,并跟踪 API 响应和 SQL Server db。
将近一个小时,对 API 的所有调用都是缓存命中 - App Insights 中仅记录了两次缓存未命中(第一次调用和最后一次调用在 +50 分钟标记处),并且我的 sql profiler 跟踪中仅记录了两次 sql 调用(第一次和最后一次通话)。
我几乎每次都希望看到缓存未命中,但没有。我是否误解了缓存-store?
这是策略定义:
<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>xxxxx</vary-by-query-parameter>
</cache-lookup>
</inbound>
<backend>
<base />
</backend>
<outbound>
<cache-store duration="300" />
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
回答1
您能否尝试修改后的出站策略:
<cache-store duration="10" cache-response="true" />
https://docs.microsoft.com/en-us/azure/api-management/api-management-caching-policies#StoreToCache
cache-response:
设置为 true 以缓存当前的 HTTP 响应。如果该属性被省略或设置为 false,则仅缓存状态代码为 200 OK 的 HTTP 响应。
这也意味着,您必须检查您当前的响应代码 200/400/404/500/... 也许大部分时间都是 404 响应,它没有被缓存。