AFNetWorking是如何进行数据缓存的--之AFImageCache & NSURLCache 详解

如果你是一个正在使用由Matt Thompson开发的网络库 AFNetWorking(如果你还没有使用,那你还在等什么?)的iOS开发者,也许你一直很好奇和困惑它的缓存机制,并且想要了解如何更好地充分利用它?

AFNetworking实际上利用了两套单独的缓存机制:

  • AFImagecache : 继承于NSCache,AFNetworking的图片内存缓存的类。

  • NSURLCache : NSURLConnection的默认缓存机制,用于存储NSURLResponse对象:一个默认缓存在内存,并且可以通过一些配置操作可以持久缓存到磁盘的类。

AFImageCache是如何工作的?

AFImageCache属于UIImageView+AFNetworking的一部分,继承于NSCache,以URL(从NSURLRequest对象中获取)字符串作为key值来存储UIImage对象。 AFImageCache的定义如下:(这里我们声明了一个2M内存、100M磁盘空间的NSURLCache对象。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@interface AFImageCache : NSCache  // singleton instantiation :
 
+ (id )sharedImageCache {
     static AFImageCache *_af_defaultImageCache = nil;
     static dispatch_once_t oncePredicate;
     dispatch_once(&oncePredicate, ^{
         _af_defaultImageCache = [[AFImageCache alloc] init];
 
// clears out cache on memory warning :
 
     [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * __unused notification) {
         [_af_defaultImageCache removeAllObjects];
     }];
});
 
// key from [[NSURLRequest URL] absoluteString] :
 
static inline NSString * AFImageCacheKeyFromURLRequest(NSURLRequest *request) {
     return  [[request URL] absoluteString];
}
 
@implementation AFImageCache
 
// write to cache if proper policy on NSURLRequest :
 
- (UIImage *)cachedImageForRequest:(NSURLRequest *)request {
     switch  ([request cachePolicy]) {
         case  NSURLRequestReloadIgnoringCacheData:
         case  NSURLRequestReloadIgnoringLocalAndRemoteCacheData:
             return  nil;
         default :
             break ;
     }
 
     return  [self objectForKey:AFImageCacheKeyFromURLRequest(request)];
}
// read from cache :
 
- (void)cacheImage:(UIImage *)image
         forRequest:(NSURLRequest *)request {
     if  (image && request) {
         [self setObject:image forKey:AFImageCacheKeyFromURLRequest(request)];
     }
}

AFImageCache是NSCache的私有实现,它把所有可访问的UIImage对象存入NSCache中,并控制着UIImage对象应该在何时释放,如果UIImage对象释放的时候你希望去做一些监听操作,你可以实现NSCacheDelegate的 cache:willEvictObject 代理方法。Matt Thompson已经谦虚的告诉我在AFNetworking2.1版本中可通过setSharedImageCache方法来配置AFImageCache,这里是 AFN2.2.1中的UIImageView+AFNetworking文档。

NSURLCache

AFNetworking使用了NSURLConnection,它利用了iOS原生的缓存机制,并且NSURLCache缓存了服务器返回的NSURLRespone对象。NSURLCache 的shareCache方法是默认开启的,你可以利用它来获取每一个NSURLConnection对象的URL内容。让人不爽的是,它的默认配置是缓存到内存而且并没有写入到磁盘。为了tame the beast(驯服野兽?不太懂),增加可持续性,你可以在AppDelegate中简单地声明一个共享的NSURLCache对象,像这样:

1
2
3
4
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024
                                               diskCapacity:100 * 1024 * 1024
                                               diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];

设置NSURLRequest对象的缓存策略

NSURLCache 将对每一个NSURLRequest对象遵守缓存策略(NSURLRequestCachePolicy),策略如下所示:

1
2
3
4
5
6
- NSURLRequestUseProtocolCachePolicy                默认的缓存策略,对特定的URL请求使用网络协议中实现的缓存逻辑
- NSURLRequestReloadIgnoringLocalCacheData          忽略本地缓存,重新请请求
- NSURLRequestReloadIgnoringLocalAndRemoteCacheData 忽略本地和远程缓存,重新请求
- NSURLRequestReturnCacheDataElseLoad               有缓存则从中加载,如果没有则去请求
- NSURLRequestReturnCacheDataDontLoad               无网络状态下不去请求,一直加载本地缓存数据无论其是否存在
- NSURLRequestReloadRevalidatingCacheData           默从原始地址确认缓存数据的合法性之后,缓存数据才可使用,否则请求原始地址

用NSURLCache缓存数据到磁盘

Cache-Control HTTP Header

Cache-Controlheader或Expires header存在于服务器返回的HTTP response header中,来用于客户端的缓存工作(前者优先级要高于后者),这里面有很多地方需要注意,Cache-Control可以拥有被定义为类似max-age的参数(在更新响应之前要缓存多长时间), public/private 访问或者是non-cache(不缓存响应数据),这里对HTTP cache headers进行了很好的介绍。

继承并控制NSURLCache

如果你想跳过Cache-Control,并且想要自己来制定规则读写一个带有NSURLResponse对象的NSURLCache,你可以继承NSURLCache。下面有个例子,使用 CACHE_EXPIRES来判断在获取源数据之前对缓存数据保留多长时间.(感谢 Mattt Thompson的回复)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  @interface CustomURLCache : NSURLCache
 
static NSString * const CustomURLCacheExpirationKey = @ "CustomURLCacheExpiration" ;
static NSTimeInterval const CustomURLCacheExpirationInterval = 600;
 
@implementation CustomURLCache
 
+ (instancetype)standardURLCache {
     static CustomURLCache *_standardURLCache = nil;
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
         _standardURLCache = [[CustomURLCache alloc]
                                  initWithMemoryCapacity:(2 * 1024 * 1024)
                                  diskCapacity:(100 * 1024 * 1024)
                                  diskPath:nil];
     }
 
     return  _standardURLCache;
}
 
#pragma mark - NSURLCache
 
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
     NSCachedURLResponse *cachedResponse = [ super  cachedResponseForRequest:request];
 
     if  (cachedResponse) {
         NSDate* cacheDate = cachedResponse.userInfo[CustomURLCacheExpirationKey];
         NSDate* cacheExpirationDate = [cacheDate dateByAddingTimeInterval:CustomURLCacheExpirationInterval];
         if  ([cacheExpirationDate compare:[NSDate date]] == NSOrderedAscending) {
             [self removeCachedResponseForRequest:request];
             return  nil;
         }
     }
}
 
     return  cachedResponse;
}
 
- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse
                  forRequest:(NSURLRequest *)request
{
     NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:cachedResponse.userInfo];
     userInfo[CustomURLCacheExpirationKey] = [NSDate date];
 
     NSCachedURLResponse *modifiedCachedResponse = [[NSCachedURLResponse alloc] initWithResponse:cachedResponse.response data:cachedResponse.data userInfo:userInfo storagePolicy:cachedResponse.storagePolicy];
 
     [ super  storeCachedResponse:modifiedCachedResponse forRequest:request];
}
@end

现在你有了属于自己的NSURLCache的子类,不要忘了在AppDelegate中初始化并且使用它。

在缓存之前重写NSURLResponse

-connection:willCacheResponse 代理方法是在被缓存之前用于截断和编辑由NSURLConnection创建的NSURLCacheResponse的地方。 对NSURLCacheResponse进行处理并返回一个可变的拷贝对象(代码来自NSHipster blog)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                   willCacheResponse:(NSCachedURLResponse *)cachedResponse {
     NSMutableDictionary *mutableUserInfo = [[cachedResponse userInfo] mutableCopy];
     NSMutableData *mutableData = [[cachedResponse data] mutableCopy];
     NSURLCacheStoragePolicy storagePolicy = NSURLCacheStorageAllowedInMemoryOnly;
 
     // ...
 
     return  [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response]
                                                     data:mutableData
                                                 userInfo:mutableUserInfo
                                            storagePolicy:storagePolicy];
}
 
// If you do not wish to cache the NSURLCachedResponse, just return nil from the delegate function:
 
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                   willCacheResponse:(NSCachedURLResponse *)cachedResponse {
     return  nil;
}

禁用NSURLCache

不想使用NSURLCache?不为所动?好吧,你可以禁用NSURLCache,只需要将内存和磁盘空间设置为0就行了.

1
2
3
4
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0
                                               diskCapacity:0
                                               diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值