MKNetworkKit Auto Caching

躍然發表於2014-11-14

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.

  • 它是基於 HTTP1.1 協議設計的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 例項方法清理快取

原文:http://doruby.com/mknetworkkit/third-libs/plugins/cache/2014/05/28/mknetworkkit-auto-caching/

相關文章