轉自:http://blog.sina.com.cn/s/blog_9693f61a01016t4w.html
在IOS應用程序開發中,爲了減少與服務端的交互次數,加快用戶的響應速度,一般都會在IOS設備中加一個緩存的機制。使用緩存的目的是爲了使用的應用程序能更快速的響應用戶輸入,是程序高效的運行。有時候我們需要將遠程web服務器獲取的數據緩存起來,減少對同一個url多次請求。下面將介紹如何在IOS設備中進行緩存。
內存緩存我們可以使用sdk中的NSURLCache類。NSURLRequest需要一個緩存參數來說明它請求的url何如緩存數據的,我們先看下它的CachePolicy類型。
1、NSURLRequestUseProtocolCachePolicyNSURLRequest默認的cachepolicy,使用Protocol協議定義。
2、NSURLRequestReloadIgnoringCacheData忽略緩存直接從原始地址下載。
3、NSURLRequestReturnCacheDataElseLoad只有在cache中不存在data時才從原始地址下載。
4、NSURLRequestReturnCacheDataDontLoad只使用cache數據,如果不存在cache,請求失敗;用於沒有建立網絡連接離線模式;
5、NSURLRequestReloadIgnoringLocalAndRemoteCacheData:忽略本地和遠程的緩存數據,直接從原始地址下載,與NSURLRequestReloadIgnoringCacheData類似。
6、NSURLRequestReloadRevalidatingCacheData:驗證本地數據與遠程數據是否相同,如果不同則下載遠程數據,否則使用本地數據。
NSURLCache還提供了很多方法,來方便我們實現應用程序的緩存機制。下面我通過一個例子來說明,這個例子減少我們對同一個url多次請求。看下面代碼:
#import
@interface ViewController : UIViewController
@property (strong,nonatomic) NSURLConnection*connection;
@property (strong, nonatomic) NSURLCache *urlCache;
@property (strong, nonatomic) NSURL*url;
@property (strong, nonatomic) NSMutableURLRequest *request;
- (IBAction)reloadWebView:(UIButton *)sender;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
NSString *paramURLAsString=@"http://blog.sina.com.cn/u/2526279194";
self.urlCache =[NSURLCachesharedURLCache];
[self.urlCache setMemoryCapacity:1*1024*1024];
//創建一個nsurl
self.url = [NSURL URLWithString:paramURLAsString];
//創建一個請求
self.request=[NSMutableURLRequest requestWithURL:self.url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0f];
[self.myWebView loadRequest:self.request];
}
這個例子中,我們請求url爲http://blog.sina.com.cn/u/2526279194的網站。如果這個url被緩存了,我們直接從緩存中獲取數據,否則從http://blog.sina.com.cn/u/2526279194站點上重新獲取數據。我們設置了緩存大小爲1M。
- (IBAction)reloadWebView:(UIButton *)sender {
//從請求中獲取緩存輸出
NSCachedURLResponse*response =[self.urlCachecachedResponseForRequest:self.request];
//判斷是否有緩存
if (response!= nil){
NSLog(@"如果有緩存輸出,從緩存中獲取數據");
[self.request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
}
[self.myWebView loadRequest:self.request];
self.connection = nil;
NSURLConnection *newConnection =[[NSURLConnection alloc]initWithRequest:self.request
delegate:self
startImmediately:YES];
self.connection = newConnection;
}
使用下面代碼,我將請求的過程打印出來
- (void) connection:(NSURLConnection*)connection
didReceiveResponse:(NSURLResponse *)response{
NSLog(@"將接收輸出");
}
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse*)redirectResponse{
NSLog(@"即將發送請求");
return(request);
}
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data{
NSLog(@"接受數據");
NSLog(@"數據長度爲= %lu", (unsigned long)[data length]);
}
-(NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse{
NSLog(@"將緩存輸出");
return(cachedResponse);
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection{
NSLog(@"請求完成");
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError*)error{
NSLog(@"請求失敗");
}
@end
第一次打印結果如下
2013-01-31 15:28:29.923NSURLCacheDemo[27848:907] 即將發送請求
2013-01-31 15:28:30.043NSURLCacheDemo[27848:907] 將接收輸出
2013-01-31 15:28:30.045NSURLCacheDemo[27848:907] 接受數據
2013-01-31 15:28:30.047NSURLCacheDemo[27848:907] 數據長度爲= 30047
2013-01-31 15:28:30.095NSURLCacheDemo[27848:907] 接受數據
2013-01-31 15:28:30.098NSURLCacheDemo[27848:907] 數據長度爲= 3575
2013-01-31 15:28:30.102NSURLCacheDemo[27848:907] 接受數據
2013-01-31 15:28:30.104NSURLCacheDemo[27848:907] 數據長度爲= 1482
2013-01-31 15:28:30.105NSURLCacheDemo[27848:907] 將緩存輸出
2013-01-31 15:28:30.107NSURLCacheDemo[27848:907] 請求完成
第二次點擊打印結果如下
2013-01-31 15:28:31.599NSURLCacheDemo[27848:907] 如果有緩存輸出,從緩存中獲取數據
2013-01-31 15:28:31.607NSURLCacheDemo[27848:907] 即將發送請求
2013-01-31 15:28:31.840NSURLCacheDemo[27848:907] 將接收輸出
2013-01-31 15:28:31.843NSURLCacheDemo[27848:907] 接受數據
2013-01-31 15:28:31.845NSURLCacheDemo[27848:907] 數據長度爲= 35104
2013-01-31 15:28:31.846NSURLCacheDemo[27848:907] 請求完成