[英]NSURLCache, together with NSURLSession, does not respect: Cache-Control: max-age:86000, private, must-revalidate
[英]Alamofire/NSURLSession caching with private Cache-Control and max-age
我不确定自己是否做错了什么,但是在设置urlRequest.cachePolicy = .useProtocolCachePolicy
并将缓存标头设置为private时使用max-age "Cache-Control" = "private, max-age=86400";
useProtocolCachePolicy是否应与private一起使用? 还是我需要手动将其公开?
我使用cachePolicy:NSURLRequestUseProtocolCachePolicy尝试了以下对我来说效果很好的代码。
它根据http标头响应中的cache-control / max-age使缓存过期:
我使用了这个有用的博客
这是我使用的代码:
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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.