// 1.创建请求
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 2.设置缓存策略(有缓存就用缓存,没有缓存就重新请求)
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
// 定期处理缓存
// if (缓存没有达到7天) {
// request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
// }
// 获得全局的缓存对象
NSURLCache *cache = [NSURLCache sharedURLCache];
// if (缓存达到7天) {
// [cache removeCachedResponseForRequest:request];
// }
// lastCacheDate = 2014-06-30 11:04:30
NSCachedURLResponse *response = [cache cachedResponseForRequest:request];
if (response) {
NSLog(@"---这个请求已经存在缓存");
} else {
NSLog(@"---这个请求没有缓存");
}
// 3.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@", dict);
}
}];
NSURLCache
最新推荐文章于 2024-04-06 15:38:21 发布