[iOS 多线程 & 网络 - 2.7] - NSURLCache

时间:2023-03-09 17:24:39
[iOS 多线程 & 网络 - 2.7] - NSURLCache
A.基本知识
1.为什么需要缓存?
有时候一个url会请求多次,得到的内容确实一样的
[iOS 多线程 & 网络 - 2.7] - NSURLCache
2.缓存的概念
[iOS 多线程 & 网络 - 2.7] - NSURLCache
3.缓存数据的过程[iOS 多线程 & 网络 - 2.7] - NSURLCache
当服务器返回数据时,需要做以下步骤
(1)使用服务器的数据(比如解析、显示)
(2)将服务器的数据缓存到硬盘(沙盒)
此时缓存的情况是:内存缓存中有数据,硬盘缓存中有数据。
再次请求数据分为两种情况:
(1)如果程序并没有被关闭,一直在运行
  那么此时内存缓存中有数据,硬盘缓存中有数据。如果此时再次请求数据,直接使用内存缓存中的数据即可
(2)如果程序重新启动
  那么此时内存缓存已经消失,没有数据,硬盘缓存依旧存在,还有数据。如果此时再次请求数据,需要读取内存中缓存的数据。
提示:从硬盘缓存中读取数据后,内存缓存中又有数据了
B.实现
1.什么请求需要缓存?
     一般只对GET请求进行缓存,而不对POST请求进行缓存
     由于GET请求一般用来查询数据,POST请求一般是发大量数据给服务器处理(变动性比较大)
  在iOS中,可以使用NSURLCache类缓存数据
  iOS 5之前:只支持内存缓存。从iOS 5开始:同时支持内存缓存和硬盘缓存
 
2.NSURLCache
iOS中得缓存技术用到了NSURLCache类。
缓存原理:一个NSURLRequest对应一个NSCachedURLResponse
缓存技术:把缓存的数据都保存到数据库中。
 
3.NSURLCache的常见用法
(1)获得全局缓存对象(没必要手动创建)NSURLCache *cache = [NSURLCache sharedURLCache];
(2)设置内存缓存的最大容量(字节为单位,默认为512KB)- (void)setMemoryCapacity:(NSUInteger)memoryCapacity;
(3)设置硬盘缓存的最大容量(字节为单位,默认为10M)- (void)setDiskCapacity:(NSUInteger)diskCapacity;
(4)硬盘缓存的位置:沙盒/Library/Caches
(5)取得某个请求的缓存- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request;
(6)清除某个请求的缓存- (void)removeCachedResponseForRequest:(NSURLRequest *)request;
(7)清除所有的缓存- (void)removeAllCachedResponses;
 
4.缓存GET请求
  要想对某个GET请求进行数据缓存,非常简单
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  // 设置缓存策略
  request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
  只要设置了缓存策略,系统会自动利用NSURLCache进行数据缓存
 
5.iOSNSURLRequest提供了7种缓存策略:(实际上能用的只有4种)
NSURLRequestUseProtocolCachePolicy // 默认的缓存策略(取决于协议)
NSURLRequestReloadIgnoringLocalCacheData // 忽略缓存,重新请求
NSURLRequestReloadIgnoringLocalAndRemoteCacheData // 未实现
NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData // 忽略缓存,重新请求
NSURLRequestReturnCacheDataElseLoad// 有缓存就用缓存,没有缓存就重新请求
NSURLRequestReturnCacheDataDontLoad// 有缓存就用缓存,没有缓存就不发请求,当做请求出错处理(用于离线模式)
NSURLRequestReloadRevalidatingCacheData // 未实现
 
6.缓存的注意事项
缓存的设置需要根据具体的情况考虑,如果请求某个URL的返回数据:
  (1)经常更新:不能用缓存!比如股票、彩票数据
  (2)一成不变:果断用缓存
  (3)偶尔更新:可以定期更改缓存策略 或者 清除缓存
提示:如果大量使用缓存,会越积越大,建议定期清除缓存
 //
// ViewController.m
// NSURLCacheDemo
//
// Created by hellovoidworld on 15/1/28.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "ViewController.h" @interface ViewController ()
- (IBAction)downloadData; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (IBAction)downloadData {
// 1.创建请求
NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/video?type=json"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 2.设置缓存策略
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad; // 有缓存就不请求 // 获得全局缓存
NSURLCache *cache = [NSURLCache sharedURLCache]; // 定期处理缓存
// if (超过一定时期) {
// [cache removeCachedResponseForRequest:request];
// } // 获得某个request的response
NSCachedURLResponse *response = [cache cachedResponseForRequest:request];
if (response) {
NSLog(@"有缓存");
} else {
NSLog(@"没有缓存");
} // 3.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]);
}]; }
@end