iOS缓存-内存缓存

为了减少与服务器的连接沟通次数,提高应用程序的执行速度,使用了iOS的缓存机制

#import "YoungViewController.h"


@interface YoungViewController ()<NSURLConnectionDelegate>


{

   NSURLConnection * connection;

}


@end


@implementation YoungViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    UIButton * btnCache = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    btnCache.frame =CGRectMake(50,50, 100, 30);

    [btnCache setTitleColor:[UIColorgrayColor] forState:UIControlStateNormal];

    [btnCache setBackgroundColor:[UIColororangeColor]];

    [btnCache setTitle:@"缓存"forState:UIControlStateNormal];

    [btnCache addTarget:selfaction:@selector(Chcal:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewinsertSubview:btnCache atIndex:0];

}

-(void)Chcal:(id)sender

{

   NSString *paramURLAsString = @"http://www.baidu.com/";

   if([paramURLAsString length]==0)

    {

        NSLog(@"nil or empty is given!");

       return;

    }

    

   NSURLCache * urlCache = [NSURLCachesharedURLCache];

    /*设置缓存空间的大小--1M*/

    [urlCachesetMemoryCapacity:1*1024*1024];

    

    /*创建一个nsurl*/

   NSURL *url = [NSURLURLWithString:paramURLAsString];

    

    /*创建一个请求*/

    NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:60.0f];

    

    /*从请求中获取缓存输出*/

   NSCachedURLResponse *respose = [urlCache cachedResponseForRequest:request];

    

    //判断是否有缓存

   if(respose!=nil)

    {

        NSLog(@"如果有缓存输出,从缓存中获取数据");

        //[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];//忽略缓存,重新下载

        

        //[request setCachePolicy:NSURLRequestReturnCacheDataElseLoad];//缓存中不存在才下载

        

        //[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];//使用缓存,绝对不请求网络

        

        //[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];//忽略缓存,重新下载

        

        //[request setCachePolicy:NSURLRequestReloadRevalidatingCacheData];//缓存于请求是否相同,同不下载,不同下载。

    }

    

    

    connection = nil;

    NSURLConnection *newConnection = [[NSURLConnectionalloc]initWithRequest:requestdelegate:selfstartImmediately:YES];

   connection = newConnection;

}


- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse

{

    NSLog(@"即将发送请求.....");

   return(request);

}

- (void)  connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    NSLog(@"将接收输出.....");

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

   NSLog(@"数据长度为 = %lu", (unsignedlong)[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(@"请求失败");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值