ITeye博客

AFNetWorking and SDUrlCache 缓存http请求

zhy584520 · 2014-11-03 14:25 · IOS · 移动开发 · Markdown

     SDURLCache *urlCache = [[SDURLCache alloc]
     initWithMemoryCapacity:1024*1024*2   // 2MB mem cache
     diskCapacity:1024*1024*15 // 15MB disk cache
     diskPath:[SDURLCache defaultCachePath]];
     [urlCache setMinCacheInterval:1];
     [NSURLCache setSharedURLCache:urlCache];
     
     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
     [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSLog(@"从服务端获取数据:%@",operation.responseString);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     if ([urlCache isCached:[NSURL URLWithString:@"http://www.baidu.com"]]) {
     
     }
     NSCachedURLResponse *resp = [urlCache cachedResponseForRequest:request];
     NSString  *str = [[NSString alloc] initWithData:resp.data encoding:NSUTF8StringEncoding];
     NSLog(@"从缓存中获取数据:%@",str);
     }];
     [operation start];

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。