gpt4 book ai didi

objective-c - 为什么 NSURLSession 不使用我配置的 NSURLCache?

转载 作者:太空狗 更新时间:2023-10-30 03:51:11 30 4
gpt4 key购买 nike

iOS7带来了NSURLSession,借助NSURLSessionConfigure,我们可以自定义URLCache,所以我试了一下,但是没有运气,好像我的URLCache没有用到全部。

- (void)testURLCache
{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:1024 * 100 diskCapacity:1024 * 100 diskPath:@"test.urlcache"];

NSLog(@"usage:%lu", (unsigned long)[cache currentDiskUsage]);

config.URLCache = cache;

NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
for (int i = 0; i < 40; i++) {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js?%d", i]]];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"error:%@", error);
} else {
NSLog(@"done:%d", i);
}
}];
[task resume];
[NSThread sleepForTimeInterval:0.5];
}
NSLog(@"usage:%lu", (unsigned long)[cache currentDiskUsage]);
}

我观察到的东西:

1) 在Caches文件夹下创建一个test.urlcache文件夹,里面有3个文件Cache.dbCache。 db-shmCache.db-walCache.db大小为4KB,里面没有记录。

2)每次运行代码,第一次输出usage总是0,for循环结束后usage变为4096,下一次usage又变为0。

最佳答案

似乎有两个潜在的问题:

  1. 您的缓存太小。从历史上看,如果缓存的项目超过总缓存大小的 10%,它就不会缓存。

  2. 您正在检查上次请求后 0.5 秒的磁盘使用情况。将缓存写入持久存储可能为时过早。

下面我使用了一个更大的缓存并在最终检查磁盘使用情况之前等待 10 秒,缓存工作正常。我还使用 GCD 对串行任务进行排队,以消除任意 0.5 秒的延迟,这既更安全,也使它在通过网络检索数据和使用缓存时更加明显。

- (void)testURLCache
{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;

// this is too small and won't cache
// NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:100 * 1024 diskCapacity:100 * 1024 diskPath:@"small.urlcache"];

// use bigger cache
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:10 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:@"big.urlcache"];

config.URLCache = cache;

// I'm going to add tasks to serial queue so that (a) I don't block main queue;
// and (b) I don't need arbitrary delay, but I can just signal when it's done.

dispatch_queue_t queue = dispatch_queue_create("com.domain.app.datatasks", 0);

NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
for (int i = 0; i < 40; i++) {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

dispatch_async(queue, ^{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js?%d", i]]];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

if (error) {
NSLog(@"error:%@", error);
} else {
NSLog(@"done:%d", i);
}

dispatch_semaphore_signal(semaphore); // signal dispatched block that request is done
}];
[task resume];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); // wait until task is complete
});
}

// when the queued tasks finish, then check disk usage both immediately and 10 seconds later

dispatch_async(queue, ^{

// check cache size immediately after the data tasks finish

NSLog(@"usage:%lu", (unsigned long)[cache currentDiskUsage]);

// check again in 10 seconds

double delayInSeconds = 10.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
NSLog(@"usage:%lu", (unsigned long)[cache currentDiskUsage]);
});
});
}

顺便说一句,对于像这样的网络任务,我通常会使用操作队列而不是调度队列,然后将我的数据任务包装在并发的 NSOperation 子类中。然后我可以使用依赖关系来指示最终的完成操作,但同时既允许并发又指示并发程度,但这似乎超出了原始问题的范围,我希望尽可能简单。

关于objective-c - 为什么 NSURLSession 不使用我配置的 NSURLCache?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20768968/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com