我无法在OSX上使用NSURLCache
取得成功(10.7):
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:1024*1024*20
diskCapacity:1024*1024*5
diskPath:NSHomeDirectory()];
[NSURLCache setSharedURLCache:cache];
NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://wikipedia.org/"]
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:60];
NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSLog(@"Received %lu bytes", [data length]);
sleep(10);
[req setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
data = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSLog(@"Received %lu bytes", [data length]);
NSURLRequestReturnCacheDataDontLoad
缓存策略初始化请求,我将获得0字节的响应。在两种情况下都会创建文件Cache.db,似乎有一个有效数据,但磁盘缓存不起作用。
NSURLCache
怎么了?或者我做错了什么?