zoukankan      html  css  js  c++  java
  • 内存缓存的使用Demo

    使用缓存的目的是为了使用的应用程序能更快速的响应用户输入,是程序高效的运行。有时候我们需要将远程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] view plain copy
    1. -(IBAction) buttonPress:(id) sender    
    2.   
    3. {    
    4.   
    5.     NSString *paramURLAsString= @"http://www.baidu.com/";    
    6.   
    7.     if ([paramURLAsString length] == 0){    
    8.   
    9.         NSLog(@"Nil or empty URL is given");    
    10.   
    11.         return;    
    12.   
    13.     }    
    14.   
    15.     NSURLCache *urlCache = [NSURLCache sharedURLCache];    
    16.   
    17.     /* 设置缓存的大小为1M*/   
    18.   
    19.     [urlCache setMemoryCapacity:1*1024*1024];    
    20.   
    21.      //创建一个nsurl    
    22.   
    23.     NSURL *url = [NSURL URLWithString:paramURLAsString];    
    24.   
    25.         //创建一个请求    
    26.   
    27.     NSMutableURLRequest *request =    
    28.   
    29.     [NSMutableURLRequest   
    30.   
    31.      requestWithURL:url    
    32.   
    33.      cachePolicy:NSURLRequestUseProtocolCachePolicy   
    34.   
    35.      timeoutInterval:60.0f];    
    36.   
    37.      //从请求中获取缓存输出    
    38.   
    39.     NSCachedURLResponse *response =    
    40.   
    41.     [urlCache cachedResponseForRequest:request];    
    42.   
    43.     //判断是否有缓存    
    44.   
    45.     if (response != nil){    
    46.   
    47.         NSLog(@"如果有缓存输出,从缓存中获取数据");    
    48.   
    49.         [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];    
    50.   
    51.     }    
    52.   
    53.     self.connection = nil;    
    54.   
    55.     /* 创建NSURLConnection*/   
    56.   
    57.     NSURLConnection *newConnection =    
    58.   
    59.     [[NSURLConnection alloc] initWithRequest:request    
    60.   
    61.                                     delegate:self   
    62.   
    63.                             startImmediately:YES];    
    64.   
    65.     self.connection = newConnection;    
    66.   
    67.     [newConnection release];    
    68.   
    69. }   

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

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

    [plain] view plain copy
    1. - (void)  connection:(NSURLConnection *)connection    
    2.   
    3.   didReceiveResponse:(NSURLResponse *)response{    
    4.   
    5.     NSLog(@"将接收输出");    
    6.   
    7. }    
    8.   
    9. - (NSURLRequest *)connection:(NSURLConnection *)connection    
    10.   
    11.              willSendRequest:(NSURLRequest *)request    
    12.   
    13.             redirectResponse:(NSURLResponse *)redirectResponse{    
    14.   
    15.     NSLog(@"即将发送请求");    
    16.   
    17.     return(request);    
    18.   
    19. }    
    20.   
    21. - (void)connection:(NSURLConnection *)connection    
    22.   
    23.     didReceiveData:(NSData *)data{    
    24.   
    25.     NSLog(@"接受数据");    
    26.   
    27.     NSLog(@"数据长度为 = %lu", (unsigned long)[data length]);    
    28.   
    29. }    
    30.   
    31. - (NSCachedURLResponse *)connection:(NSURLConnection *)connection    
    32.   
    33.                   willCacheResponse:(NSCachedURLResponse *)cachedResponse{    
    34.   
    35.     NSLog(@"将缓存输出");    
    36.   
    37.     return(cachedResponse);    
    38.   
    39. }    
    40.   
    41. - (void)connectionDidFinishLoading:(NSURLConnection *)connection{    
    42.   
    43.     NSLog(@"请求完成");    
    44.   
    45. }    
    46.   
    47. - (void)connection:(NSURLConnection *)connection    
    48.   
    49.   didFailWithError:(NSError *)error{    
    50.   
    51.     NSLog(@"请求失败");    
    52.   
    53. }   


     

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

    [html] view plain copy
    1. 2011-07-30 18:50:24.910 Caching[3971:207] 即将发送请求    
    2.   
    3. 2011-07-30 18:50:28.557 Caching[3971:207] 将接收输出    
    4.   
    5. 2011-07-30 18:50:31.677 Caching[3971:207] 接受数据    
    6.   
    7. 2011-07-30 18:50:31.681 Caching[3971:207] 数据长度为 = 4414    
    8.   
    9. 2011-07-30 18:50:31.682 Caching[3971:207] 接受数据    
    10.   
    11. 2011-07-30 18:50:31.682 Caching[3971:207] 数据长度为 = 2996    
    12.   
    13. 2011-07-30 18:50:38.107 Caching[3971:207] 将缓存输出    
    14.   
    15. 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的内存缓存机制,下一篇文章将重点介绍一下本地缓存机制

  • 相关阅读:
    Perforce服务器的备份还原
    asp.net C#页面中添加普通视频的几种方式
    九度OJ1085
    poj3253
    POJ1276
    POJ1113
    POJ1273
    九度OJ1084
    xdoj 1108 淼·诺贝尔
    九度OJ1081
  • 原文地址:https://www.cnblogs.com/W-Kr/p/5208740.html
Copyright © 2011-2022 走看看