Azure API Management(5)缓存

本文是APIM系列的一部分,完整内容参见:

  1. (视频)Azure API Management(1)创建Azure APIM并添加后端API
  2. (视频)Azure API Management(2)创建 Product/User/Subscription/体验Developer Portal
  3. (视频)Azure API Management(3)体验APIM Policy-流控/隐藏后端api url/修改response header
  4. (视频)Azure API Management(4)体验APIM 版本管理
  5. (视频)Azure API Management(5)缓存
  6. (视频)Azure API Management(6)Validate JWT Token

 


 

 

 

 

本文介绍:

APIM支持的两种缓存;

案例:配置APIM内置缓存并测试效果;

案例:配置APIM External 缓存并测试效果;

 

This article describe:

2 kinds of cache in Azure API Management:

demo: config internal cache in apim and test;

demo: config external cache (Redis ) in apim and test.

 

视频介绍:

https://blog.51azure.cloud/post/2020/8/12/azure-api-management-5-cache

图文介绍:

Azure API Management 支持两种缓存,一种是内置的缓存,一种是外部的缓存,外部的缓存支持Redis,可以直接使用Azure Cache for Redis。

内置的缓存根据APIM的SKU的不同,会有不同的缓存大小限制,具体见下表:

  消耗 开发人员 基本 标准 高级
缓存(每单位) 仅限外部 10 MB 50 MB 1 GB 5 GB
缓存,外部

 

 


案例1,配置内部缓存:

选择要添加缓存的API,点击如下图【2】所示,通过可视化界面增加缓存policy,或点击按钮【3】通过代码添加缓存policy:

Azure API Management(5)缓存_第1张图片

 

如果通过可视化界面,则选择如下图 cache response:

 

Azure API Management(5)缓存_第2张图片

 

关于配置界面的具体属性的解释,可参照官网:https://docs.microsoft.com/zh-cn/azure/api-management/api-management-caching-policies

本例中进行了如下配置:

 

Azure API Management(5)缓存_第3张图片

如果使用代码进行配置,则代码如下:


    
        
        
            Accept
            Accept-Charset
            Authorization
        
    
    
        
    
    
        
        
    
    
        
    

 

测试内置缓存:

Azure API Management(5)缓存_第4张图片

 

点击 Trace,点击Backend,如果Trace中有backend调用时间,则表示从真实的后端API获取的数据:

Azure API Management(5)缓存_第5张图片

如下图,表示缓存未命中,调用了真实的后端API,调用过程时长为 683 ms

Azure API Management(5)缓存_第6张图片

 

在Outbound中,将后端API的返回结果写入到了缓存中:

Azure API Management(5)缓存_第7张图片

 

在缓存未失效(本例为20秒)内,再次测试api,则结果如下:

已经命中缓存,同时backend小节没有数据,表示并没有再次调用后端API:

Azure API Management(5)缓存_第8张图片

同时整体的请求时间大大缩短:

Azure API Management(5)缓存_第9张图片

 


 

案例2,配置外部缓存并测试

本例中我们使用 Azure Cache for Redis作为外部缓存:

创建 Azure Cache for Redis实例:

Azure API Management(5)缓存_第10张图片

 

在APIM中配置外部缓存:

Azure API Management(5)缓存_第11张图片

 

确保policy中的 caching type为external或者prefer external,其中 prefer external表示如果配置了外部缓存则优先使用外部缓存,否则使用内部缓存。

Azure API Management(5)缓存_第12张图片

 

在缓存周期内连续两次调用API,在外部缓存中查看缓存结果:

Azure API Management(5)缓存_第13张图片

在控制台中执行 keys *,可查看当前缓存的key:

Azure API Management(5)缓存_第14张图片

 


 


声明:

 

点击可查阅本站文章目录 《文章分类目录》

本站所有内容仅代表个人观点,如与官文档冲突,请以官方文档为准。

可在本页面下方留言或通过下方联系方式联系我:

微信:wxyusz;邮箱:[email protected]

欢迎关注公众号“云计算实战”,接收最新文章推送。

知识共享许可协议

本作品由Sean Yu 采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
欢迎转载、使用、重新发布,但务必保留文章链接:https://www.51azure.cloud,且不得用于商业目的。

你可能感兴趣的:(教程,公开课,azure)