跳至主要内容

博文

目前显示的是 一月, 2020的博文

WKWebView默认缓存策略与HTTP缓存协议

原文链接 今天同事反应H5游戏资源更新了,但是iOS有缓存没有同步还是显示的原来的数据。今天研究了一下,收获颇多,分享给大家。 WKWebView默认缓存策略 下图是 苹果官方文档 提供的默认缓存策略(NSURLRequestUseProtocolCachePolicy)的流程图。这个图有点坑,容易让人误解,后面详细说。 默认缓存策略图(来源苹果官方) 官方文档上是这样描述的: For the HTTP and HTTPS protocols, NSURLRequestUseProtocolCachePolicy performs the following behavior: 1. If a cached response does not exist for the request, the URL loading system fetches the data from the originating source. 2. Otherwise,  if the cached response does not indicate that it must be revalidated every time , and if the cached response is not stale (past its expiration date), the URL loading system returns the cached response. 3. If the cached response is stale or requires revalidation, the URL loading system makes a HEAD request to the originating source to see if the resource has changed. If so, the URL loading system fetches the data from the originating source. Otherwise, it returns the cached response. 官方文档说, 缓存不存在直接请求。 缓存存在,且缓存response头没有指明每次必须校验资源...

NSURLCache

原文链接 NSURLCache  为您的应用的 URL 请求提供了内存中以及磁盘上的综合缓存机制。 作为基础类库  URL 加载系统  的一部分,任何通过  NSURLConnection  加载的请求都将被  NSURLCache  处理。 网络缓存减少了需要向服务器发送请求的次数,同时也提升了离线或在低速网络中使用应用的体验。 当一个请求完成下载来自服务器的回应,一个缓存的回应将在本地保存。下一次同一个请求再发起时,本地保存的回应就会马上返回,不需要连接服务器。 NSURLCache  会  自动  且  透明  地返回回应。 为了好好利用  NSURLCache ,你需要初始化并设置一个共享的 URL 缓存。在 iOS 中这项工作需要在  -application:did Finish Launching With Options:  完成,而 OS X 中是在  –application Did Finish Launching: : - ( BOOL ) application :( UIApplication * ) application did Finish Launching With Options :( NSDictionary * ) launch Options { NSURLCache * URLCache = [[ NSURLCache alloc ] init With Memory Capacity : 4 * 1024 * 1024 disk Capacity : 20 * 1024 * 1024 disk Path: nil ]; [ NSURLCache set Shared URLCache : URLCache ]; } 缓存策略由请求(客户端)和回应(服...