iOS 内存缓存

来源:互联网 发布:巨人网络怎么样 知乎 编辑:程序博客网 时间:2024/06/07 17:55

{

NSURLCacheStoragePolicy

These constants specify the caching strategy used by an NSCachedURLResponse object.

typedef enum{   NSURLCacheStorageAllowed,   NSURLCacheStorageAllowedInMemoryOnly,   NSURLCacheStorageNotAllowed,} NSURLCacheStoragePolicy;
Constants
NSURLCacheStorageAllowed

Specifies that storage in NSURLCache is allowed without restriction.

Important: iOS prior to version 5 ignores this cache policy, and instead treats it as NSURLCacheStorageAllowedInMemoryOnly.

Available in iOS 2.0 and later.

Declared in NSURLCache.h.

NSURLCacheStorageAllowedInMemoryOnly

Specifies that storage in NSURLCache is allowed; however storage should be restricted to memory only.

Available in iOS 2.0 and later.

Declared in NSURLCache.h.

NSURLCacheStorageNotAllowed

Specifies that storage in NSURLCache is not allowed in any fashion, either in memory or on disk.

Available in iOS 2.0 and later.

Declared in NSURLCache.h.


} from apple document 


这篇文章将介绍一下如何在iOS设备中进行缓存。

这篇文章将只介绍一下将内容缓存到内存中,下一篇文章就介绍一下在iOS磁盘上缓存内容。

使用缓存的目的是为了使用的应用程序能更快速的响应用户输入,是程序高效的运行。有时候我们需要将远程web服务器获取的数据缓存起来,减少对同一个url多次请求。

内存缓存我们可以使用sdk中的NSURLCache类。NSURLRequest需要一个缓存参数来说明它请求的url何如缓存数据的,我们先看下它的CachePolicy类型。

1、NSURLRequestUseProtocolCachePolicy NSURLRequest默认的cache policy,使用Protocol协议定义。
2、NSURLRequestReloadIgnoringCacheData 忽略缓存直接从原始地址下载。
3、NSURLRequestReturnCacheDataElseLoad 只有在cache中不存在data时才从原始地址下载。
4、NSURLRequestReturnCacheDataDontLoad 只使用cache数据,如果不存在cache,请求失败;用于没有建立网络连接离线模式;
5、NSURLRequestReloadIgnoringLocalAndRemoteCacheData:忽略本地和远程的缓存数据,直接从原始地址下载,与NSURLRequestReloadIgnoringCacheData类似。
6NSURLRequestReloadRevalidatingCacheData:验证本地数据与远程数据是否相同,如果不同则下载远程数据,否则使用本地数据。

NSURLCache还提供了很多方法,来方便我们实现应用程序的缓存机制。下面我通过一个例子来说明,这个例子减少我们对同一个url多次请求。看下面代码:

-(IBAction) buttonPress:(id) sender
{
    NSString *paramURLAsString= @"http://www.baidu.com/";
    if ([paramURLAsString length] == 0){
        NSLog(@"Nil or empty URL is given");
        return;
    }
    NSURLCache *urlCache = [NSURLCache sharedURLCache];
    /* 设置缓存的大小为1M*/
    [urlCache setMemoryCapacity:1*1024*1024];
     //创建一个nsurl
    NSURL *url = [NSURL URLWithString:paramURLAsString];
        //创建一个请求
    NSMutableURLRequest *request =
    [NSMutableURLRequest
     requestWithURL:url
     cachePolicy:NSURLRequestUseProtocolCachePolicy
     timeoutInterval:60.0f];
     //从请求中获取缓存输出
    NSCachedURLResponse *response =
    [urlCache cachedResponseForRequest:request];
    //判断是否有缓存
    if (response != nil){
        NSLog(@"如果有缓存输出,从缓存中获取数据");
        [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
    }
    self.connection = nil;
    /* 创建NSURLConnection*/
    NSURLConnection *newConnection =
    [[NSURLConnection alloc] initWithRequest:request
                                    delegate:self
                            startImmediately:YES];
    self.connection = newConnection;
    [newConnection release];
}

这个例子中,我们请求url为www.baidu.com的网站。如果这个url被缓存了,我们直接从缓存中获取数据,否则从www.baidu.com站点上重新获取数据。我们设置了缓存大小为1M。

使用下面代码,我将请求的过程打印出来:

- (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(@"请求失败");
}

当我们第一次点击界面上的按钮,打印的结果如下:

2011-07-30 18:50:24.910 Caching[3971:207] 即将发送请求
2011-07-30 18:50:28.557 Caching[3971:207] 将接收输出
2011-07-30 18:50:31.677 Caching[3971:207] 接受数据
2011-07-30 18:50:31.681 Caching[3971:207] 数据长度为 = 4414
2011-07-30 18:50:31.682 Caching[3971:207] 接受数据
2011-07-30 18:50:31.682 Caching[3971:207] 数据长度为 = 2996
2011-07-30 18:50:38.107 Caching[3971:207] 将缓存输出
2011-07-30 18:50:38.109 Caching[3971:207] 请求完成

在看我们第二次点击界面上的按钮,打印结果如下:

2011-07-30 18:52:18.894 Caching[3971:207] 即将发送请求
2011-07-30 18:52:18.895 Caching[3971:207] 将接收输出
2011-07-30 18:52:18.895 Caching[3971:207] 接受数据
2011-07-30 18:52:18.896 Caching[3971:207] 数据长度为 = 7410
2011-07-30 18:52:18.896 Caching[3971:207] 请求完成

我们看到没有“将缓存输出”一项,请求到的数据是第一次请求的累积,也就是第二次是从内存中获取数据的。

总结:本文简单的介绍了一下iOS的内存缓存机制,下一篇文章将重点介绍一下本地缓存机制。

0 0
原创粉丝点击