DO RUBY
  • Mobile
    • Modularization
      • 再谈移动端跨平台框架 Flutter 与 React Native
      • 打造工业级 App 开发流程 (JIRA+CI/CD)
      • 模块化设计-iOS团队协作开发
      • 剖析几种流行的 iOS 设计模式--MVC;MVVM;VIPER
    • iOS Foundation
      • ARC 与内存管理
      • 从头梳理内存与内存管理
      • 事件交付: 响应链
      • iOS 手势识别
      • iOS 手势操作详解
      • UIKit Class Hierarchy
      • iOS 13 兼容性适配检查
      • iOS 上的蓝牙框架 Bluetooth
    • Performance
      • App 启动优化之一:main.m 方法之前的优化
    • Debug
      • 在Xcode里,为项目定义不同的环境配置
      • 调试我们的 APP (iOS)
      • URLCache 设置不当引起的 App 故障
      • ASDebugger -- 超简单的 App 网络监控调试工具
    • Tips
      • Xcode 自动生成 Objective C SDK 文档
      • 无线分发应用--通过 Safari 安装 App
      • 智能的 App Banner - Smart App Banners
      • 点阴影区关闭 Modal View Controller
    • 3rd Party
      • MKNetworkKit Auto Caching
  • Computer Science
    • 重头梳理网络协议栈
  • Productivity
    • Git
      • Gitflow 在客户端开发中的实践
    • Security
      • 10 分钟将你的网站升级到 HTTPS
    • Others
      • FlexBox 使用向导
  • TECH MANAGER
    • Google VP 聊职场干货
    • 无身份建立影响力 Influence without Authority
Powered by GitBook
On this page

Was this helpful?

  1. Mobile
  2. 3rd Party

MKNetworkKit Auto Caching

Previous3rd PartyNext重头梳理网络协议栈

Last updated 5 years ago

Was this helpful?

MKNetworkKit 网上已有不少介绍它的文章了,不过对于它提供的众多特性的实现机制,还是值得研究研究的。其中 Auto caching 是其中之一。

官方文档是这样写的:

High performance background caching (based on HTTP 1.1 caching specs) built in

MKNetworkKit can automatically cache all your “GET” requests. When you make the same request again, MKNetworkKit calls your completion handler with the cached version of the response (if it’s available) almost immediately. It also makes a call to the remote server again. After the server data is fetched, your completion handler is called again with the new response data. This means, you don’t have to handle caching manually on your side. All you need to do is call one method

[[MKNetworkEngine sharedEngine] useCache];

Optionally, you can override methods in your MKNetworkEngine subclass to customize your cache directory and in-memory cache cost.

  • 它是基于 协议设计的Cache模式,刚好我们请求的后端服务也是基于这个协议,Cache-Control 用的是 max-age=0, private, must-revalidate , ETag 标记缓存

  • 如果你启用了 useCache ,它可以将所有的GET请求进行Cache,它根据请求的 url 来判断是否是同一个请求,然后调用缓存数据

  • 它在返回缓存数据时,同时也向服务器请求最新数据,如果有最新内容,会返回新的 ETag,然后MK会更新缓存中的ETag

  • 当 APP 退到后端时,缓存数据会持久化到Caches目录中

  • 它生成的缓存数据文件根据 unique Identifier 命名,放在 MKNetworkKitCache 目录中,生成规则可以查看 MKNetworkKitOperation uniqueIdentifier 方法。

  • 用 MKNetworkEngine emptyCache 实例方法清理缓存

HTTP1.1