緩存策略 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];
}