代码改变世界

ios url缓存策略——NSURLCache、 NSURLRequest

2013-05-07 14:23  三戒1993  阅读(180)  评论(0编辑  收藏  举报
一:url 缓存策略

 

NSURLRequest

 

 

requestWithURL:cachePolicy:timeoutInterval:

 

1:NSURLRequestUseProtocolCachePolicy


This flag will use the underlying protocol’s caching mechanism if the protocol
supports it.

 

基础策略

 

2:NSURLRequestReloadIgnoringLocalCacheData


This flag specifies that the local cached copy of the resource that is about to be
downloaded must be disregarded and the remote cache policy must be effective.
If there is a local copy of the resource, managed by the framework itself, it will be
ignored.

 

忽略本地缓存

 

 

3:NSURLRequestReturnCacheDataElseLoad

 


This flag specifies that the cached data must be used before attempting to load the
data from the original source. The cached data could be protocol-based cached or
locally cached. If there is no cached data, the data will be downloaded from the
original source.

 

首先使用缓存,如果没有本地缓存,才从原地址下载

 

 

4:NSURLRequestReturnCacheDataDontLoad


This flag specifies that only the local cached data must be used. If the data has not
been cached, the request will fail. This is a great flag to use whenever your application
wants to perform operations in offline mode (such as the Offline Mode in
web browsers).

 

使用本地缓存,从不下载,如果本地没有缓存,则请求失败。此策略多用于离线操作

 

5:NSURLRequestReloadIgnoringLocalAndRemoteCacheData


This flag disregards any type of caching involved in the process, local and remote,
and always attempts to download the data from the original source.

 

无视任何的缓存策略,无论是本地的还是远程的,总是从原地址重新下载

 

 

6:NSURLRequestReloadRevalidatingCacheData

 


This flag specifies that the original source of data must validate the local cache (if
any) before an attempt is made to download the data from the original source. If
there is a copy of the original source cached locally and the remote source specifies
that the cached data is valid, the data won’t be downloaded again. In any other
case, the data will be downloaded from the original source.

 

如果本地缓存是有效的则不下载。其他任何情况都从原地址重新下载

 

 

二:NSURLCache

 

NSURLCache *urlCache = [NSURLCache sharedURLCache];

 

 

In iOS, NSURLCache supports caching data only in memory and not
on disk.


 

(NSURLCache 仅支持内存缓存,不支持硬盘缓存)




  1. - (void) downloadURL:(NSString *)paramURLAsString{  
  2.   
  3. if ([paramURLAsString length] == 0){  
  4.     NSLog(@"Nil or empty URL is given");  
  5.     return;  
  6. }  
  7.   
  8. /* Get the shared URL Cache object. No need to create a new one */  
  9. NSURLCache *urlCache = [NSURLCache sharedURLCache];  
  10.   
  11. /* We will store up to 1 Megabyte of data into the cache */  
  12. [urlCache setMemoryCapacity:1*1024*1024];  
  13.   
  14. /* For our request, we need an instance of NSURL so let's retrieve 
  15. that from the string that we created before */  
  16. NSURL *url = [NSURL URLWithString:paramURLAsString];  
  17.   
  18. /* And this is our request */  
  19. NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0f];  
  20.   
  21. /* Try to get a cached response to our request. 
  22. This might come back as nil */  
  23. NSCachedURLResponse *response =[urlCache cachedResponseForRequest:request];  
  24.   
  25. /* Do we have a cached response? */  
  26. if (response != nil){  
  27.     NSLog(@"Cached response exists. Loading data from cache...");  
  28.     [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];  
  29. }  
  30.   
  31. self.connection = nil;  
  32.   
  33. /* Start the connection with the request */  
  34. NSURLConnection *newConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];  
  35. self.connection = newConnection;  
  36. [newConnection release];  
  37.   
  38. }  

  1. // Create the request.  
  2. //所构建的NSURLRequest具有一个依赖于缓存响应的特定策略,cachePolicy取得策略,timeoutInterval取得超时值  
  3. NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]  
  4.                         cachePolicy:NSURLRequestUseProtocolCachePolicy  
  5.                     timeoutInterval:60.0];  
  6. // create the connection with the request  
  7. // and start loading the data  
  8. NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];  
  9. if (theConnection) {  
  10.     // Create the NSMutableData to hold the received data.  
  11.     // receivedData is an instance variable declared elsewhere.  
  12.     receivedData = [[NSMutableData data] retain];  
  13. else {  
  14.     // Inform the user that the connection failed.  
  15. }