Last-Modified
是资源最后修改的时间戳,每每与缓存时间进行对比来判断缓存是否过时。json
ETag
是的功能与
Last-Modified
相似:服务端不会每次都会返回文件资源。客户端每次向服务端发送上次服务器返回的
ETag
值,服务器会根据客户端与服务端的
ETag
值是否相等,来决定是否返回 data,同时老是返回对应的
HTTP
状态码。客户端经过
HTTP
状态码来决定是否使用缓存。好比:服务端与客户端的
ETag
值相等,则
HTTP
状态码为 304,不返回 data。服务端文件一旦修改,服务端与客户端的
ETag
值不等,而且状态值会变为200,同时返回 data。
- (void)downloadFile缓存
{服务器
/*网络
1.NSURLRequestUseProtocolCachePolicy NSURLRequest 默认的cache policy,使用Protocol协议定义。ide
2.NSURLRequestReloadIgnoringCacheData 忽略缓存直接从原始地址下载。code
3.NSURLRequestReturnCacheDataDontLoad 只使用cache数据,若是不存在cache,请求失败;用于没有创建网络链接离线模式orm
4.NSURLRequestReturnCacheDataElseLoad 只有在cache中不存在data时才从原始地址下载。资源
5.NSURLRequestReloadIgnoringLocalAndRemoteCacheData 忽略本地和远程的缓存数据,直接从原始地址下载,与NSURLRequestReloadIgnoringCacheData相似。get
6.NSURLRequestReloadRevalidatingCacheData :验证本地数据与远程数据是否相同,若是不一样则下载远程数据,不然使用本地数据string
*/
NSString *fileDownLoadPath = @"https://s3.cn-north-1.amazonaws.com.cn/zplantest.s3.seed.meme2c.com/area/area.json";
NSString *lastModified = [NSUserDefaults.standardUserDefaults stringForKey:@"Last-Modified"] ?: @"";
NSString *lastModifiedeTag = [NSUserDefaults.standardUserDefaults stringForKey:@"Etag"] ?: @"";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:fileDownLoadPath]];
// request.cachePolicy = NSURLRequestUseProtocolCachePolicy;
// 服务器作对比, 不用重复下载
// [request setValue:lastModified forHTTPHeaderField:@"If-Modified-Since"]; // Last-Modified
[request setValue:lastModifiedeTag forHTTPHeaderField:@"If-None-Match"]; // ETag
[request setValue:@"no-cache" forHTTPHeaderField:@"Cache-Control"];
LMJWeakSelf(self);
NSLog(@"%@", request);
MBProgressHUD *hud = [MBProgressHUD showProgressToView:weakself.view Text:@"下载中"];
[[[LMJRequestManager sharedManager] downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
hud.progress = (downloadProgress.completedUnitCount) / (CGFloat)(downloadProgress.totalUnitCount);
NSLog(@"%lf", ((float)downloadProgress.completedUnitCount) / (downloadProgress.totalUnitCount));
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
return [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:[fileDownLoadPath lastPathComponent]]];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
[MBProgressHUD hideHUDForView:weakself.view animated:YES];
NSLog(@"%@", filePath);
NSLog(@"%@", response);
NSLog(@"%@", error);
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
[self.view makeToast:[NSString stringWithFormat:@"statuscode: %zd, \n200是下载成功, 304是不用下载", httpResponse.statusCode]];
NSString *lastModified = [httpResponse allHeaderFields][@"Last-Modified"];
NSString *lastModifiedeTag = [httpResponse allHeaderFields][@"Etag"];
if (lastModified && !error) {
[NSUserDefaults.standardUserDefaults setObject:lastModified forKey:@"Last-Modified"];
[NSUserDefaults.standardUserDefaults setObject:lastModifiedeTag forKey:@"Etag"];
}
NSLog(@"%@", lastModified);
}] resume];
}