内存缓存

1,首先回忆一下文件目录结构

Documents:用来存放用户自己生成的文件、数据,或应用运行必须的数据库之类的。iCloud会同步和恢复。

Library/Preferences:存放用户偏好设置,例如NSUserDefault数据。iCloud不会同步和恢复。重启不删除。

Library/Caches:存放用户缓存数据,例如网络缓存、下载的东西等等,iCloud不会同步和恢复。重启不删除。

tmp:存放临时文件,要记得及时删除。例如存放下载的临时文件。iCloud不会同步和恢复。重启可能删除。

2,代码如下:

- (IBAction)fetchUrlData:(id)sender {
    NSString *paramURLAsString = @"http://www.baidu.com/";
    NSURLCache *urlCache = [NSURLCache sharedURLCache];
    [urlCache setMemoryCapacity:1*1024*1024];//1M
    NSURL *url = [NSURL URLWithString:paramURLAsString];
    NSMutableURLRequest *request =
    [NSMutableURLRequest requestWithURL:url
                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                        timeoutInterval:60.0f];
    NSCachedURLResponse *response = [urlCache cachedResponseForRequest:request];
    if (response != nil) {//看看缓存里有没有
        [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];//改为在此url下只从缓存里取数据
    }
    self.connection = nil;
    NSURLConnection *newConnection =
    [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
    self.connection = newConnection;
}
#pragma protocol: NSURLConnectionDataDelegate
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response{
    NSLog(@"willSendRequest");
    return request;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"didReceiveResponse");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    NSLog(@"didReceiveData and the length of data is %lu", (unsigned long)[data length]);
    
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{//对response进行内存缓存
    NSLog(@"willCacheResponse");
    return cachedResponse;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"connectionDidFinishLoading");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"didFailWithError");
}
点击按钮运行其中代码,会发现,目录发生改变:


画红色方框的文件夹就是新生成的。缓存数据存放在文件夹fsCa....里,所以删掉其他东西理论上还是可以取到缓存数据。

第一次运行log输出如下:

2015-04-18 16:33:06.514 Test_4_18[1066:711041] willSendRequest
2015-04-18 16:33:06.592 Test_4_18[1066:711041] didReceiveResponse
2015-04-18 16:33:06.592 Test_4_18[1066:711041] didReceiveData and the length of data is 3489
2015-04-18 16:33:06.592 Test_4_18[1066:711041] didReceiveData and the length of data is 89517
2015-04-18 16:33:06.593 Test_4_18[1066:711041] willCacheResponse
2015-04-18 16:33:06.593 Test_4_18[1066:711041] connectionDidFinishLoading
可见第一次是做了缓存的,调用了
willCacheResponse

第二次及以后(包括重启)运行log输出如下:

2015-04-18 16:33:13.534 Test_4_18[1066:711041] willSendRequest
2015-04-18 16:33:13.537 Test_4_18[1066:711041] didReceiveResponse
2015-04-18 16:33:13.537 Test_4_18[1066:711041] didReceiveData and the length of data is 93006
2015-04-18 16:33:13.537 Test_4_18[1066:711041] connectionDidFinishLoading

可见直接取出了总的数据,并且没有了做缓存的步骤,实现了内存缓存。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值