網絡請求的緩存機制cachePolicy 和緩存的清理
- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
// 緩存會自動存到以下這個文件夾中,創建三個文件,其中db文件可以使用數據庫打開
NSString * tem = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"%@", tem);
// 開始請求數據
NSString * path = @"http://localhost/netTest.html";
NSURL * url = [NSURL URLWithString:path];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
// 設置緩存機制,只要這一句代碼就可以了
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", str);
}];
// 緩存的清理
NSURLCache caches = [NSURLCache sharedURLCache];
NSCachedURLResponse response = [caches cachedResponseForRequest:request];
if (response == nil)
{
NSLog(@"沒有緩存");
}
else
{
NSLog(@"有緩存");
[caches removeAllCachedResponses];
}
} </pre>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!