所以,从 ASICacheDelegate.h ,我们可以读到:
// Cache storage policies control whether cached data persists between application launches (ASICachePermanentlyCacheStoragePolicy) or not (ASICacheForSessionDurationCacheStoragePolicy)
// Calling [ASIHTTPRequest clearSession] will remove any data stored using ASICacheForSessionDurationCacheStoragePolicy
typedef enum _ASICacheStoragePolicy {
ASICacheForSessionDurationCacheStoragePolicy = 0,
ASICachePermanentlyCacheStoragePolicy = 1
} ASICacheStoragePolicy;
来自Originate 的article 解释说AFNetworking 依赖于NSURLCache。
所以当你说你想要的时候
指定响应应在AFNetworking中缓存多长时间
关于 ASI 所做的事情,我们谈论的是应用程序启动,而不是秒(或任何时间单位)。
因为NSURLCache 可以用作内存缓存(默认情况下,在每次应用启动之间重置)或磁盘上的持久缓存,所以您应该能够尝试所需的行为。
简而言之,对NSURLCache 不做任何事情会给您带来ASICacheForSessionDurationCacheStoragePolicy 的行为。
下面的sn-p会给你ASICachePermanentlyCacheStoragePolicy的行为:
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024
diskCapacity:100 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
最后,如果你想拥有类似[ASIHTTPRequest clearSession] 的东西,或者如果你想使用时间单位来管理你的缓存,the same article 有一个标题为“Subclass NSURLCache for Ultimate Control”的部分。
AFAIK,使用 AFNetworking 的 API 并不容易,但这应该是为开源项目做出贡献的好机会 :)