缓存使用.net core和APIM的最佳实践



我是APIM缓存的新手。我正在建立一个。net核心webapi项目(微服务),其中有GET端点,例如"GetProduct(QueryPara参数)"(见下文)。我想实现缓存。

注意:我的web api (service)是通过APIM调用的。

我的要求是如果Get参数保持相同的返回数据从缓存(2分钟)。如果输入参数(QueryPara)改变,从DB返回数据。

  1. 我需要在。net/服务器级别或APIM级别实现缓存吗?
  2. 如果在。net核心级别,我可以使用https://code-maze.com/aspnetcore-response-caching/实现缓存,如[ResponseCache(Duration = 120, Location = ResponseCacheLocation.Client)]
  3. 我的理解是我不需要缓存在。net应用程序/服务级别。只需要在APIM上实现缓存策略就足以实现缓存了吗?只需配置cache-lookup和cache-store(见
  4. )

注意:我的回复小于2mb。

public QueryPara {
public string Page {get;set;}
public string Limit {get;set;}
public string PurchasedDate {get;set;}
}

APIM缓存策略

<cache-lookup vary-by-developer="false" vary-by-developer-groups="false" downstream-caching-type="none" must-revalidate="true" allow-private-response-caching="true" caching-type="internal" >
<vary-by-query-parameter>productCode</vary-by-query-parameter>
<vary-by-query-parameter>pageNo</vary-by-query-parameter>
<vary-by-query-parameter>limit</vary-by-query-parameter>
</cache-lookup>
</inbound>
<backend>
<base/>
</backend>
<outbound>
<cache-store duration="2400" />
<base/>
如果你能分享一些例子,那就太好了。

感谢

根据@Markus Meyer的建议,这里是缓存APIM的完整策略和文档与缓存APIM的示例。

<policies>
<inbound>
<base />
<cache-lookup vary-by-developer="false" vary-by-developer-groups="false" allow-private-response-caching="true" must-revalidate="true" downstream-caching-type="none" caching-type="internal">
<vary-by-header>userAuthorizationToken</vary-by-header>
<vary-by-query-parameter>productCode</vary-by-query-parameter>
<vary-by-query-parameter>pageNo</vary-by-query-parameter>
<vary-by-query-parameter>limit</vary-by-query-parameter>
</cache-lookup>
<rewrite-uri template="/private/product" />
</inbound>
<backend>
<forward-request timeout="10" />
</backend>
<outbound>
<base />
<cache-store duration="2400" />
</outbound>
<on-error>
<base />
</on-error>
</policies>

最新更新