UIWebview 的缓存策略和验证,以及 web 相关

缓存策略 NSURLRequestCachePolicy

NSURLRequestUseProtocolCachePolicy
缓存策略定义在 web 协议实现中,用于请求特定的URL。是默认的URL缓存策略
Specifies that the caching logic defined in the protocol implementation, if any, is used for a particular URL load request. This is the default policy for URL load requests.

NSURLRequestReloadIgnoringLocalCacheData
从服务端获取数据,忽略本地缓存
Specifies that the data for the URL load should be loaded from the originating source. No existing cache data should be used to satisfy a URL load request.

NSURLRequestReloadIgnoringLocalAndRemoteCacheData
不仅忽略本地的缓存数据,还忽略中间网络媒介(如代理服务器)忽略缓存。直接从最原始的服务器拿取
Specifies that not only should the local cache data be ignored, but that proxies and other intermediates should be instructed to disregard their caches so far as the protocol allows.

NSURLRequestReloadIgnoringCacheData
被NSURLRequestReloadIgnoringLocalCacheData替换了
Replaced by NSURLRequestReloadIgnoringLocalCacheData.

NSURLRequestReturnCacheDataElseLoad
已经存在的缓存数据用于请求返回,不管它的过期日期和已经存在了多久。如果没有请求对应的缓存数据,从数据源读取
Specifies that the existing cached data should be used to satisfy the request, regardless of its age or expiration date. If there is no existing data in the cache corresponding the request, the data is loaded from the originating source.

NSURLRequestReturnCacheDataDontLoad
已经存在的缓存数据用于请求返回,不管它的过期日期和已经存在了多久。如果没有请求对应的缓存数据,不要去数据源读取,该请求被设置为失败,这种情况多用于离线模式
Specifies that the existing cache data should be used to satisfy a request, regardless of its age or expiration date. If there is no existing data in the cache corresponding to a URL load request, no attempt is made to load the data from the originating source, and the load is considered to have failed. This constant specifies a behavior that is similar to an “offline” mode.

NSURLRequestReloadRevalidatingCacheData
已经存在的缓存数据先去数据源验证有效性,如果无效,将从数据源获取
Specifies that the existing cache data may be used provided the origin source confirms its validity, otherwise the URL is loaded from the origin source.

NSURLRequestUseProtocolCachePolicy 和 NSURLRequestReloadRevalidatingCacheData 区别

个人意见,仅供参考,如有错误,请指出来,这对我很重要,谢谢
NSURLRequestReloadRevalidatingCacheData 是一定要和原始的数据源验证 cache 是否有效。
而NSURLRequestUseProtocolCachePolicy 是根据 web 的协议来控制缓存,服务端返回数据的 head 有相关的信息。它可能会返回中间网络媒介(如代理服务器中的数据)

针对缓存策略做一下本地测试,这是非常有必要的
[objc] view plain copy print?在CODE上查看代码片派生到我的代码片
NSURL *webUrl = [NSURL URLWithString:@"http://localhost/test.txt"];
NSURLRequest *request =[NSURLRequest requestWithURL:webUrl cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:60];
[self.mainWebView loadRequest:request];

这里使用NSURLRequestReloadRevalidatingCacheData, 返回cache数据一定要先验证数据有效。

使用建立两个同名的test.txt, 先放一个到服务器。

然后直接修改服务器的 test.txt 文件,添加字段" ----------------------------- " 保存,可以看到马上生效了。

用另外一个test.txt替换掉服务器中当前的test.txt,也马上生效了

通过以上简单的验证可以得知NSURLRequestReloadRevalidatingCacheData 这种模式对存储在服务器中的文件的修改和替换是敏感的

我这里使用的是mac系统自带的 appache 服务器
文件目录是在 /Library/WebServer/Documents
启动Apache的方法是在命令行输入 sudo apachectl start
然后输入密码,输入过程命令行是无显示的,不用管,输入之后敲回车键。会有提示:服务器已经启动。

根据文档的信息,initwithURL 使用的默认缓存机制NSURLRequestUseProtocolCachePolicy,并且是60s的请求时间,超过时间会请求失败

相关文件
清除cookie
[objc] view plain copy print?在CODE上查看代码片派生到我的代码片
NSHTTPCookie *cookie;

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

for (cookie in [storage cookies]) {

            [storage deleteCookie:cookie];             

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 最近这了几篇文章放在了简书上,因为投了首页稿,被无情的拒绝了,而且我发现我的文章除了没人阅读外,唯一一个赞也是我自...
    卜卜卜卜萝阅读 392评论 2 3
  • 自从孩子们唤醒春天和巨人,巨人的花园,成为了孩子们的乐园,孩子们站在巨人的脚下,爬上巨人的肩膀,尽情的玩耍...
    06小石头曾立轩阅读 339评论 0 1
  • 5月20,小满。 在上班之前,对苏筱满来说,这一天,只是小满,若是真要说特别的话,那还能算是她的生日,27年前的小...
    月影寒阅读 675评论 3 2
  • 没有逻辑,想到什么便写了下来。 昨天年会,一杯红酒又一杯梅子酒(梅子酒让我想起了童年吃的腌制梅子),脸涨红,却是没...
    BIPN阅读 337评论 0 0