一: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 仅支持内存缓存,不支持硬盘缓存)
- (void) downloadURL:(NSString *)paramURLAsString{
if ([paramURLAsString length] == 0){
NSLog(@”Nil or empty URL is given”);
return;
}
/* Get the shared URL Cache object. No need to create a new one */
NSURLCache *urlCache = [NSURLCache sharedURLCache];
/* We will store up to 1 Megabyte of data into the cache */
[urlCache setMemoryCapacity:1*1024*1024];
/* For our request, we need an instance of NSURL so let’s retrieve
that from the string that we created before */
NSURL *url = [NSURL URLWithString:paramURLAsString];
/* And this is our request */
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0f];
/* Try to get a cached response to our request.
This might come back as nil */
NSCachedURLResponse *response =[urlCache cachedResponseForRequest:request];
/* Do we have a cached response? */
if (response != nil){
NSLog(@”Cached response exists. Loading data from cache…”);
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
}
self.connection = nil;
/* Start the connection with the request */
NSURLConnection *newConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
self.connection = newConnection;
[newConnection release];
}