1. AFNETWorking
在AFNETWorking中,并没有提供现成的缓存方案,我们可以通过写文件的方式,自行做缓存。
在下载方法中:
|
1
2
3
4
5
6
7
|
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//写缓存
NSString *cachePath = @
"你的缓存路径"
;
// /Library/Caches/MyCache
[data writeToFile:cachePath atomically:YES];
succsee(data);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
|
然后在每次下载前,进行如下判断:
|
1
2
3
4
5
|
NSString * cachePath = @
"你的缓存路径"
;
if
([[NSFileManager defaultManager] fileExistsAtPath:cachePath]) {
//从本地读缓存文件
NSData *data = [NSData dataWithContentsOfFile:cachePath];
}
|
有时,我们的下载请求可能是用户的动作触发的,比如一个按钮。我们还应该做一个保护机制的处理,
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//初始化一个下载请求数组
NSArray * requestArray=[[NSMutableArray alloc]init];
//每次开始下载任务前做如下判断
for
(NSString * request in requestArray) {
if
([url isEqualToString:request]) {
return
;
}
}
[requestArray addObject:url];
//下载成功或失败后
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
[requestArray removeObject:url]
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[requestArray removeObject:url]
}];
|
2. ASIHttpRequest
http://blog.sina.com.cn/s/blog_b638dc8901019zcd.html
ASIHTTPRequest会自动保存访问过的URL信息,并备之后用。在以下几个场景非常有用:
1,当没有网络连接的时候。
2,已下载的数据再次请求时,仅当它与本地版本不样时才进行下载。
1,当没有网络连接的时候。
2,已下载的数据再次请求时,仅当它与本地版本不样时才进行下载。
ASIDownloadCache 设置下载缓存
它对Get请求的响应数据进行缓存(被缓存的数据必需是成功的200请求):
当设置缓存策略后,所有的请求都被自动的缓存起来。
另外,如果仅仅希望某次请求使用缓存操作,也可以这样使用:
另外,如果仅仅希望某次请求使用缓存操作,也可以这样使用:
多种的缓存并存
仅仅需要创建不同的ASIDownloadCache,并设置缓存所使用的路径,并设置到需要使用的request实例中:
缓存策略
缓存策略是我们控制缓存行为的主要方式,如:什么时候进行缓存,缓存数据的利用方式。
以下是策略可选列表(可组合使用):
以下是策略可选列表(可组合使用):
缓存存储方式
你可以设置缓存的数据需要保存多长时间,ASIHTTPRequest提供了两种策略:
a,ASICacheForSessionDurati onCacheStoragePolicy,默认策略,基于session的缓存数据存储。当下次运行或[ASIHTTPRequest clearSession]时,缓存将失效。
b,ASICachePermanentlyCache StoragePolicy,把缓存数据永久保存在本地,
如:
a,ASICacheForSessionDurati
b,ASICachePermanentlyCache
如:
另外,也可以使用clearCachedResponsesForS
toragePolicy来清空指定策略下的缓存数据。
缓存其它特性
设置是否按服务器在Header里指定的是否可被缓存或过期策略进行缓存:
设置request缓存的有效时间:
可以判断数据是否从缓存读取:
设置缓存所使用的路径:
实现自定义的缓存
只要简单的实现ASICacheDelegate接口就可以被用来使用。
使用代理请求
默认的情况下,ASIHTTPRequest会使用被设置的默认代理。但你也可以手动修改http代理:
3. SDWebImage
http://www.cocoachina.com/ios/20141212/10622.html
使用最新版的SDWebImage,默认就是磁盘缓存:
@implementation UIImageView (WebCache)
- (void)sd_setImageWithURL:(NSURL *)url {
[self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
}
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder {
[self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
}
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
[self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil];
}
- (void)sd_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock {
[self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock];
}
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock {
[self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
}
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock {
[self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
}
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock {
如果不想用磁盘缓存,那么使用SDWebImageOptions-->SDWebImageCacheMemoryOnly:
//失败后重试
SDWebImageRetryFailed = 1 << 0,
//UI交互期间开始下载,导致延迟下载比如UIScrollView减速。
SDWebImageLowPriority = 1 << 1,
//只进行内存缓存
SDWebImageCacheMemoryOnly = 1 << 2,
//这个标志可以渐进式下载,显示的图像是逐步在下载
SDWebImageProgressiveDownload = 1 << 3,
//刷新缓存
SDWebImageRefreshCached = 1 << 4,
//后台下载
SDWebImageContinueInBackground = 1 << 5,
//NSMutableURLRequest.HTTPShouldHandleCookies = YES;
SDWebImageHandleCookies = 1 << 6,
//允许使用无效的SSL证书
//SDWebImageAllowInvalidSSLCertificates = 1 << 7,
//优先下载
SDWebImageHighPriority = 1 << 8,
//延迟占位符
SDWebImageDelayPlaceholder = 1 << 9,
//改变动画形象
SDWebImageTransformAnimatedImage = 1 << 10,
本文详细介绍了如何在AFNetworking和ASIHttpRequest中实现缓存策略,包括本地缓存、网络请求前的判断、缓存路径管理、缓存策略选择以及缓存存储方式等关键点。同时,通过对比不同缓存策略的应用场景,帮助开发者有效优化资源加载速度,减少网络开销。
929

被折叠的 条评论
为什么被折叠?



