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

A.基本知识
1.为什么需要缓存?
 
有时候一个url会请求多次,得到的内容确实一样的
Image(48)
 
2.缓存的概念
Image(49)
 
 
3.缓存数据的过程Image(50)
 
 
当服务器返回数据时,需要做以下步骤
(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)偶尔更新:可以定期更改缓存策略 或者 清除缓存
提示:如果大量使用缓存,会越积越大,建议定期清除缓存
 
 
 1 //
 2 //  ViewController.m
 3 //  NSURLCacheDemo
 4 //
 5 //  Created by hellovoidworld on 15/1/28.
 6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 
11 @interface ViewController ()
12 - (IBAction)downloadData;
13 
14 @end
15 
16 @implementation ViewController
17 
18 - (void)viewDidLoad {
19     [super viewDidLoad];
20     // Do any additional setup after loading the view, typically from a nib.
21 }
22 
23 - (void)didReceiveMemoryWarning {
24     [super didReceiveMemoryWarning];
25     // Dispose of any resources that can be recreated.
26 }
27 
28 - (IBAction)downloadData {
29     // 1.创建请求
30     NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/video?type=json"];
31     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
32 
33     // 2.设置缓存策略
34     request.cachePolicy = NSURLRequestReturnCacheDataElseLoad; // 有缓存就不请求
35    
36     // 获得全局缓存
37     NSURLCache *cache = [NSURLCache sharedURLCache];
38    
39     // 定期处理缓存
40 //    if (超过一定时期) {
41 //        [cache removeCachedResponseForRequest:request];
42 //    }
43    
44     // 获得某个request的response
45     NSCachedURLResponse *response = [cache cachedResponseForRequest:request];
46     if (response) {
47         NSLog(@"有缓存");
48     } else {
49         NSLog(@"没有缓存");
50     }
51    
52     // 3.发送请求
53     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
54        
55         NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]);
56     }];
57    
58 }
59 @end

 

 
 
posted @ 2015-01-29 00:32  HelloVoidWorld  阅读(298)  评论(0编辑  收藏  举报