File cache: ETag or Last-Modified to determine whether the file cache is valid
Last-Modified
It is the timestamp of the last modification of the resource, which is often compared with the cache time to determine whether the cache has expired.
ETag
Yes, the function is
Last-Modified
similar: the server will not return the file resource every time. Every time the client sends the
ETag
value returned by the server last time to the server, the server will decide whether to return data according to whether the values of the client and the server
ETag
are equal, and always return the corresponding
HTTP
status code. The client uses the
HTTP
status code to decide whether to use the cache. For example, if the values of the server and the client are
ETag
equal, the
HTTP
status code is 304, and no data is returned. Once the server file is modified, the values of the server and the client are
ETag
not equal, and the status value will become 200, and data will be returned at the same time.
- (void)downloadFile
{
/*
1.NSURLRequestUseProtocolCachePolicy The default cache policy of NSURLRequest is defined using the Protocol protocol.
2.NSURLRequestReloadIgnoringCacheData ignore the cache and download directly from the original address.
3. NSURLRequestReturnCacheDataDontLoad only uses cache data, if there is no cache, the request fails; it is used for offline mode without establishing a network connection
4. NSURLRequestReturnCacheDataElseLoad downloads from the original address only when there is no data in the cache.
5. NSURLRequestReloadIgnoringLocalAndRemoteCacheData ignores local and remote cache data and downloads directly from the original address, similar to NSURLRequestReloadIgnoringCacheData.
6. NSURLRequestReloadRevalidatingCacheData : Verify whether the local data is the same as the remote data, if it is different, download the remote data, otherwise use the local data
*/
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;
// The server does the comparison, without repeating the download
// [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:@"Downloading"];
[[[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 means the download is successful, 304 means no download", 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];
}