【Azure APIM】为何APIM自建网关中的cache-lookup-value策略无法正常工作?

问题描述

APIM支持cache-lookup-value策略,它的 caching-type 可以设置为:

  • internal,表示可使用内置的 API 管理缓存;

  • external 使用外部缓存,如Azure Redis及其它Redis服务

  • prefer-external 如果外部缓存已配置,则使用外部缓存,否则使用内部缓存。

但是,在自建网关(self-hosted gateway)中,却无法使用internal 内置缓存,详见:https://docs.azure.cn/zh-cn/api-management/caching-overview#caching-service-options

 

image

但是,在使用 APIM Self-hosted Gateway 访问 External Redis Cache 时,还是无法读取缓存值。 这是什么原因呢?

 

问题解答

根据从APIM Self-hosted Gateway中获取到的日志,可以发现[CacheEventIgnoredDueToRegionMismatch]报错。

[Info] 2026-01-4T09:48:34.330 [CacheEventIgnoredDueToRegionMismatch], message: expected: aaaa-xxxxxxxx, actual: bbbb-xxxxxxxx. Configured use from location: bbbb-xxxxxxxx, source: ConfigurationBasedCacheResolver

 

image

以上的日志表示 表示 self-hosted gateway 配置的 location 与 External Cache 支持的 location 不一致。

这就是导致缓存策略<cache-lookup-value>无法命中外部 Redis 无法被使用的根因。

 

解决方法

  1.  确保 APIM self-hosted gateway 的 location 与 External Cache 支持的 location 完全一致
  •     若 external cache 设置了特定 region,则 gateway 必须配置同样的 region。

  •     若 external cache 设置为 default(支持所有 region),则不会产生限制。

2.  可在 APIM Portal 中执行以下步骤验证:

  •     检查 External Cache 的 location 配置

  •     对比 self-hosted gateway 在 YAML 或运行环境中设定的 locationregion 字段

  •     修正为一致即可恢复缓存能力

image

 

参考资料

APIM 缓存概述:https://docs.azure.cn/zh-cn/api-management/caching-overview#caching-service-options APIM 策略从缓存中获取值:https://docs.azure.cn/zh-cn/api-management/cache-lookup-value-policy  

 

 

 

 

 

 

正在加载评论...