IOS开发(99)之内存缓存
这篇文章将只介绍一下将内容缓存到内存中,下一篇文章就介绍一下在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多次请求。看下面代码:
[plain]
-(ibaction) buttonpress:(id) sender
{
nsstring *paramurlasstring= @"https://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。
使用下面代码,我将请求的过程打印出来:
[plain]
- (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(@"请求失败");
}
第一次点击按钮的结果:
2013-05-21 16:26:18.959 cachedatatest[2838:c07]即将发送请求
2013-05-21 16:26:19.029 cachedatatest[2838:c07]将接收输出
2013-05-21 16:26:19.029 cachedatatest[2838:c07]接受数据
2013-05-21 16:26:19.029 cachedatatest[2838:c07]数据长度为 = 2896
2013-05-21 16:26:19.032 cachedatatest[2838:c07]接受数据
2013-05-21 16:26:19.032 cachedatatest[2838:c07]数据长度为 = 4344
2013-05-21 16:26:19.033 cachedatatest[2838:c07]接受数据
2013-05-21 16:26:19.033 cachedatatest[2838:c07]数据长度为 = 1448
2013-05-21 16:26:19.036 cachedatatest[2838:c07]接受数据
2013-05-21 16:26:19.036 cachedatatest[2838:c07]数据长度为 = 1749
2013-05-21 16:26:19.037 cachedatatest[2838:c07]将缓存输出
2013-05-21 16:26:19.037 cachedatatest[2838:c07] addresponse - adding to memory only: https://www.baidu.com/
2013-05-21 16:26:19.037 cachedatatest[2838:c07]请求完成
在看我们第二次点击界面上的按钮,打印结果如下:
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] 请求完成
我们看到没有“将缓存输出”一项的内容,请求到的数据是第一次请求的累积,也就是第二次是从内存中获取数据的。
上一篇: java基础之面向对象:继承,封装,多态
下一篇: 图像进行傅里叶变换