ASIHTTPRequest和ASIDownloadCache实现本地缓存
来源:互联网 发布:冬天男帽子 知乎 编辑:程序博客网 时间:2024/06/08 16:34
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的缓存策略:
默认的cache 策略。请勿将这一项与其他项结合使用。当你设置一个request使用cache,它会使用cache的defaultCachePolicy. ASIDownloadCache的默认cache策略是‘ASIAskServerIfModifiedWhenStaleCachePolicy’.
使用这一项,request将不会从cache中读取数据
使用这一项,request将不会把数据存入cache
这是ASIDownloadCaches的默认cache策略。使用这个策略时,request会先查看cache中是否有可用的缓存数据。如果没有,request会像普通request那样工作。
如果有缓存数据并且缓存数据没有过期,那么request会使用缓存的数据,而且不会向服务器通信。如果缓存数据过期了,request会先进行GET请求来想服务器询问数据是否有新的版本。如果服务器说缓存的数据就是当前版本,那么缓存数据将被使用,不会下载新数据。在这种情况下,cache的有效期将被设定为服务器端提供的新的有效期。如果服务器提供更新的内容,那么新内容会被下载,并且新的数据以及它的有效期将被写入cache。
这一项与ASIAskServerIfModifiedWhenStaleCachePolicy相同,除了一点:request将会每次都询问服务器端数据是否有更新。
使用这一项,cache数据将一直被使用,无视过期时间
使用这一项时,只有当响应数据有缓存时,request才会成功。如果一个request没有缓存的响应数据,那么这个request将会停止,并且不会有错误设置在request上。
当使用这一项时,当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
:ASICachePermanentlyCacheStoragePolicy];
其他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协议。
需要注意的是,要做缓存的Http请求必须用get方法来获取数据。
1、设置全局的Cache
在AppDelegate.h中添加一个全局变量
@interface AppDelegate : UIResponder <UIApplicationDelegate> { ASIDownloadCache *myCache; } @property (strong, nonatomic) UIWindow *window; @property (nonatomic,retain) ASIDownloadCache *myCache;
在AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加如下代码
//自定义缓存 ASIDownloadCache *cache = [[ASIDownloadCache alloc] init]; self.myCache = cache; [cache release]; //设置缓存路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [paths objectAtIndex:0]; [self.myCache setStoragePath:[documentDirectory stringByAppendingPathComponent:@"resource"]]; [self.myCache setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
在AppDelegate.m中的dealloc方法中添加如下语句
[myCache release];
2、设置缓存策略
在实现ASIHTTPRequest请求的地方设置request的存储方式,代码如下
NSString *str = @"http://....../getPictureNews.aspx"; NSURL *url = [NSURL URLWithString:str]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; //获取全局变量 AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; //设置缓存方式 [request setDownloadCache:appDelegate.myCache]; //设置缓存数据存储策略,这里采取的是如果无更新或无法联网就读取缓存数据 [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; request.delegate = self; [request startAsynchronous];
3、清理缓存数据
我在这里采用的是手动清理数据的方式,在适当的地方添加如下代码,我将清理缓存放在了应用的设置模块:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; [appDelegate.myCache clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
这里清理的是ASICachePermanentlyCacheStoragePolicy这种存储策略的缓存数据,如果更换其他的参数的话,即可清理对应存储策略的缓存数据。
- 使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- ASIHTTPRequest和ASIDownloadCache实现本地缓存
- ASIHTTPRequest和ASIDownloadCache实现本地缓存
- 使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- ASIHTTPRequest和ASIDownloadCache实现本地缓存
- 使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- 使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- iPhone开发笔记(16)使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- iOS学习笔记22—ASIHTTPRequest和ASIDownloadCache实现本地缓存
- iPhone开发笔记(16)使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- iPhone开发笔记(16)使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- ASIHTTPRequest的使用—— ASIDownloadCache缓存
- ASIHTTPRequest ASIDownloadCache
- ASIHttpRequest 本地缓存
- 用ASIHttpRequest实现数据缓存和队列
- 用ASIHttpRequest实现数据缓存和队列
- ASIDownloadCache缓存例子
- ASIDownloadCache 设置下载缓存
- 《视频解密》中文版(第四版) 第一章
- Eboot 中给nandflash分区实现
- 7种形式的Android Dialog使用举例
- Winform ListView控件用法
- Linux 驱动开发人员所需要做的工作
- ASIHTTPRequest和ASIDownloadCache实现本地缓存
- C# 中 特殊类
- 学习笔记-轻量级Java EE:Java EE应用的分层模型
- spring 中applicationContext的三种实现方式
- MDL
- VP6805高清视频处理模块
- JS HTML转义字符
- 继承问题
- linux c读取配置文件