在最近的版本迭代中,自己做了一些接口拉取数据方面的优化,由于我们是以长篇内容为主体的app,对于长篇富文本文章,客户端这边并没有直接接入webview实现,而是通过接口拉取并解析文章主体的html代码,通过native的方法实现文章富文本,以后会写一些文章关于这方面的实现。由于文章发布完了之后,不会经常的改动,那么客户端要是每次去拉取文章的html代码然后解析,会浪费大量的网络资源,所以在这个版本里,我跟后端配合用etag来实现文章接口的缓存,由于网上讲这方面的文章不多,这篇文章就讲讲网络接口缓存的实现方法
实现思路
首先讲下实现的思路,很简单,接口在response的header里放入Etag
字段,客户端抓取到Etag
的值并存取到本地,然后存取接口内容到本地。之后客户端在call这个接口的时候,在request的header里放入If-None-Match
,对应的值是之前存取的Etag
的值,后端status code
返回304
告诉客户端接口内容没有改变,客户端使用该接口缓存在本地的内容
Etag
Etag
是什么?Etag
能告知客户端实体表示,它是一种可将资源以字符串形式做唯一性标识的方式。服务器会为每份资源分配对应的Etag
值。另外,当资源更新时,Etag
值也需要更新
NSURLCache
实现方法里,最烦的一件事就是怎么缓存接口内容,一种方法是自己写一套代码来实现缓存功能,SDWebImage
就是这么实现缓存机制的,其实对于接口请求,iOS 系统已经帮你做好了缓存,而且非常完善简单,这个方式叫NSURLCache
。这种方式只需两个步骤就能缓存网络接口返回内容:
- 第一步:客户端设置缓存大小
AppDelegate.m
NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:4*1024*1024 diskCapacity:100*1024*1024 diskPath:nil];
NSURLCache.sharedURLCache = urlCache;
- 第二步:客户端发出
GET
请求 - 第三步:Done,通过
NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
可以获取接口缓存
这两个步骤之后,GET请求的内容会被系统自动缓存了,无需自己去实现内容缓存,对于这种方法,Mattt大神说过“无数开发者尝试自己做一个简陋而脆弱的系统来实现网络缓存的功能,殊不知 NSURLCache 只要两行代码就能搞定且好上 100 倍。”
Demo(NSURLSession)
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"url"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.f];
if (self.etag) {
[request setValue:self.etag forHTTPHeaderField:@"If-None-Match"];
}
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response;
if (urlResponse.statusCode == 304) {
NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
if (cachedResponse) {
data = cachedResponse.data;
}
} else {
if (urlResponse.allHeaderFields[@"Etag"]) {
NSString *etag = urlResponse.allHeaderFields[@"Etag"];
if (etag && etag.length > 0) {
[self saveEtag:etag];
}
}
}
}];
[task resume];
}
Demo(AFNetworking)
AFHTTPSessionManager *sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:baseURL] sessionConfiguration:config];
sessionManager.requestSerializer = [CustomJSONRequestSerializer serializer];
sessionManager.responseSerializer = [CustomJSONResponseSerializer serializer];
@interface CustomJSONRequestSerializer : AFJSONRequestSerializer
@end
@implementation CustomJSONRequestSerializer
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(id)parameters error:(NSError *__autoreleasing _Nullable *)error
{
NSMutableURLRequest *request = [super requestWithMethod:method URLString:URLString parameters:parameters error:error];
// add request If-None-Match header
NSString *absoluteUrl = request.URL.absoluteString;
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"etags"];
NSString *fileName = [directory stringByAppendingPathComponent:[self cachedFileNameForKey:absoluteUrl]];
if ([[NSFileManager defaultManager] fileExistsAtPath:fileName]) {
NSString *etag = [NSKeyedUnarchiver unarchiveObjectWithFile:fileName];
if (etag && etag.length > 0) {
[request addValue:etag forHTTPHeaderField:@"If-None-Match"];
}
}
return request;
}
@end
@interface CustomJSONResponseSerializer : AFJSONResponseSerializer
@end
@implementation CustomJSONResponseSerializer
- (nullable id)responseObjectForResponse:(NSURLResponse *)response data:(NSData *)data error:(NSError *__autoreleasing _Nullable *)error
{
NSUInteger responseStatusCode = 200;
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
responseStatusCode = (NSUInteger)[(NSHTTPURLResponse *)response statusCode];
}
id responseObject;
if (responseStatusCode == 304) {
// fetch cached contents
NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:task.currentRequest];
if (cachedResponse) {
responseObject = [super responseObjectForResponse:cachedResponse.response data:cachedResponse.data error:error];
} else {
// cached response was cleared, need to clear cached etag
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"etags"];
NSString *fileName = [self cachedFileNameForKey:response.URL.absoluteString];
NSString *cachedFileName = [directory stringByAppendingPathComponent:fileName];
[[NSFileManager defaultManager] removeItemAtPath:cachedFileName error:nil];
}
return responseObject;
} else if (responseStatusCode >= 200 && responseStatusCode <= 299) {
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.allHeaderFields[@"Etag"]) {
NSString *etag = httpResponse.allHeaderFields[@"Etag"];
if (etag && etag.length > 0) {
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"etags"];
if (![[NSFileManager defaultManager] fileExistsAtPath:directory]) {
[[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:NO attributes:nil error:nil];
}
NSString *fileName = [self cachedFileNameForKey:response.URL.absoluteString];
NSString *cachedFileName = [directory stringByAppendingPathComponent:fileName];
BOOL success = [NSKeyedArchiver archiveRootObject:etag toFile:cachedFileName];
}
}
}
responseObject = [super responseObjectForResponse:response data:data error:error];
return responseObject;
} else {
id responseObject = [super responseObjectForResponse:response data:data error:error];
if ([responseObject isKindOfClass:[NSDictionary class]]) {
*error = [NSError errorWithDomain:NSURLErrorDomain code:responseStatusCode userInfo:responseObject];
} else {
*error = [NSError errorWithDomain:NSURLErrorDomain code:responseStatusCode userInfo:nil];
}
return nil;
}
}
@end
转载请注明出处,原文地址:http://kobedai.me/p9rsts-6o/
网友评论
No visible @interface for 'CustomJSONResponseSerializer' declares the selector 'cachedFileNameForKey:'
2个报错,楼主能提供一下源码地址吗