ios url缓存策略——NSURLCache、 NSURLRequest

一: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 仅支持内存缓存,不支持硬盘缓存)

 

 

 

C代码  收藏代码
  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.   }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值