gpt4 book ai didi

ios - Alamofire/NSURLSession 缓存与私有(private)缓存控制和 max-age

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:07:31 27 4
gpt4 key购买 nike

我不确定我是否做错了什么,但是设置 urlRequest.cachePolicy = .useProtocolCachePolicy 时缓存不起作用并将缓存 header 设置为 private with max-age "Cache-Control" = "private, max-age=86400";

useProtocolCachePolicy 应该与 private 一起使用吗?或者我需要手动将其覆盖为公共(public)?

最佳答案

我尝试了以下对我来说效果很好的代码,使用缓存策略:NSURLRequestUseProtocolCachePolicy。

它根据http头响应中的cache-control/max-age使缓存过期:

我用了这个有用的blog

这是我使用的代码:

NSURL * url = [NSURL URLWithString:@"https://yourserver.com"];

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0
];

if (self.session == nil) {
NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration];
self.session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:NSOperationQueue.mainQueue];
}
NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

if (error != nil) {
NSLog(@"task transport error %@ / %d", error.domain, (int) error.code);
return;
}

NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse*) response;
NSLog(@"task finished with status %d, bytes %zu", (int) httpResponse.statusCode, (size_t) data.length);

NSDictionary * headers = httpResponse.allHeaderFields;
NSLog(@"response-headers %@",headers);
}];

[task resume];

关于ios - Alamofire/NSURLSession 缓存与私有(private)缓存控制和 max-age,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46544473/

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