iOS开发教程之ASIHTTPRequest:使用download cache

本文为大家介绍了 iOS 开发 ASIHTTPRequest 使用 download cache 的内容,其中包括 cache 策略,存储策略,其他 cache 相关的特性,编写自己的 cache 等等内容。
1.8 版本开始, ASIDownloadCache ASICacheDelegate API 改变了,你可能需要修改你的代码。
尤其是, cache 策略的可用选项发生了改变,你现在可以对单一 request 使用结合的 cache 策略
ASIHTTPRequest 可以自动缓存下载的数据,在很多情况下这很有用。
当你离线时,你无法再次下载数据,而你又需要访问这些数据
从上次下载这些数据后,你只想在数据更新后才下载新的数据
你处理的数据永远不会发生改变,所以你只想下载一次数据
在之前版本的 ASIHTTPRequest 里,遇到上述情况,你只能自己处理这些策略。在一些情况下,使用 download cache 可以让你不用再写本地缓存机制。
ASIDownloadCache  是个简单的 URL cache ,可以用来缓存 GET 请求的相应数据。一个 request 要被缓存,它首先必须请求成功(没有发送错误),服务器必须返回 200HTTP 状态值。或者,从 1.8.1 版本开始, 301,302,303,307 重定向状态码都可以。
要打开响应值的 cache 机制很简单:
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; 
这样做以后,所有的 request 都会自动使用 cache 。如果你愿意,你可以让不同的 request 使用共享的 cache
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
[request setDownloadCache:[ASIDownloadCache sharedCache]]; 
你不会被局限于使用单一的 cache ,你可以想创建多少 cache 就创建多少 cache ,只要你喜欢  ^ ^ 。当你自己创建一个 cache ,你必须设定 cache 的路径——这路径必须是一个你拥有写权限的目录。
ASIDownloadCache *cache = [[[ASIDownloadCache alloc] init] autorelease]; 
[cache setStoragePath:@"/Users/ben/Documents/Cached-Downloads"]; 
// 别忘了  -  你必须自己 retaining 你自己的 cache! 
[self setMyCache:cache]; 
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
[request setDownloadCache:[self myCache]]; 
cache 策略
cache 策略是你控制 cache 中信息的主要方法,控制何时使用 cache 数据而非重新下载数据。
每个 request cache 策略可是由 request cachePolicy  属性来控制的。 cache 策略使用掩码来定义,所以你可以二进制“与”操作他们。
//  每次都向服务器询问是否有新的内容可用, 
//  如果请求失败 使用 cache 的数据,即使这个数据已经过期了 
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy]; 
你可以使用下列 cache 策略选项来控制 request 的缓存策略:
ASIUseDefaultCachePolicy
默认的 cache  策略。请勿将这一项与其他项结合使用。当你设置一个 request 使用 cache, 它会使用 cache defaultCachePolicy. ASIDownloadCache 的默认 cache 策略是‘ ASIAskServerIfModifiedWhenStaleCachePolicy
ASIDoNotReadFromCacheCachePolicy
使用这一项, request 将不会从 cache 中读取数据
ASIDoNotWriteToCacheCachePolicy
使用这一项, request 将不会把数据存入 cache
ASIAskServerIfModifiedWhen
StaleCachePolicy
这是 ASIDownloadCaches 的默认 cache 策略。使用这个策略时, request 会先查看 cache 中是否有可用的缓存数据。如果没有, request 会像普通 request 那样工作。
如果有缓存数据并且缓存数据没有过期,那么 request 会使用缓存的数据,而且不会向服务器通信。如果缓存数据过期了, request 会先进行 GET 请求来想服务器询问数据是否有新的版本。如果服务器说缓存的数据就是当前版本,那么缓存数据将被使用,不会下载新数据。在这种情况下, cache 的有效期将被设定为服务器端提供的新的有效期。如果服务器提供更新的内容,那么新内容会被下载,并且新的数据以及它的有效期将被写入 cache
ASIAskServerIfModifiedCachePolicy
这一项与 ASIAskServerIfModifiedWhenStaleCachePolicy 相同,除了一点: request 将会每次都询问服务器端数据是否有更新。
ASIOnlyLoadIfNotCachedCachePolicy
使用这一项, cache 数据将一直被使用,无视过期时间
ASIDontLoadCachePolicy
使用这一项时,只有当响应数据有缓存时, request 才会成功。如果一个 request 没有缓存的响应数据,那么这个 request 将会停止,并且不会有错误设置在 request 上。
ASIFallbackToCacheIf
LoadFailsCachePolicy
当使用这一项时,当 request 失败时, request 会回头请求 cache 数据。如果请求失败后, request 使用的 cache 数据,那么这个 request 会成功(没有错误)。你通常会将这一项与其他项结合使用,因为它适用于指定当发生错误时 request 的行为。
当你设定了一个 cache 对象的 defaultCachePolicy  属性,所有使用这个 cache 对象的 request 都会使用这个 cache 策略,除非你为 request 设置了另外的策略。
存储策略
存储策略允许你定义一个 cache 可以存储特定的相应数据多久。 ASIHTTPRequest 目前支持两种存储策略:
ASICacheForSessionDurationCacheStoragePolicy 是默认值。相应数据只会在会话期间被存储,在第一次使用 cache 时,或者在调用  [ASIHTTPRequest clearSession] 时,数据会被清除。
使用 ASICachePermanentlyCacheStoragePolicy ,缓存的相应数据会被永久存储。要使用这个存储策略,向 request 设置:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; 
要手动清除 cache ,调用函数 clearCachedResponsesForStoragePolicy:, 传入要清除的 cache 数据的存储策略:
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICachePermanently
CacheStoragePolicy]; 
其他 cache 相关的特性
//  当你关闭  shouldRespectCacheControlHeaders,cache 对象会存储响应数据,而无视 
//  服务器的显式 请勿缓存 声明  ( 例如: cache-control  或者 pragma: no-cache 
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO]; 
//  可以设定 request secondsToCache 来覆盖服务器设定的内容有效期 这时,响应数据 
//  会一直被缓存,直到经过 secondsToCache  
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
[request setSecondsToCache:60*60*24*30]; //  缓存 30   
// request 开始执行后 , 如果响应数据是从缓存中取得的, didUseCachedResponse  会返回 YES 
[request didUseCachedResponse]; 
//  cache 对象索取一个路径来存储相应数据 这是使用 download cache 的最有效率的方法
//  因为此时,当 request 完成后,数据不需要被复制到 cache
[request setDownloadDestinationPath: 
   [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request]]; 
编写自己的 cache
如果你已经持有一个 download cache 并且想将他插入 ASIHTTPRequest 中,或者你喜欢自己写自己的 download cache ,那么让你的 cache 实现 ASICacheDelegate 协议。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值