原文地址為:http://www.cnblogs.com/pengyingh/articles/2343062.html
asiHttprequest的用法 它對Get請求的響應數據進行緩存(被緩存的數據必需是成功的200請求):
ASIHTTPRequest會自動保存訪問過的URL信息,並備之后用。在以下幾個場景非常有用:
1,當沒有網絡連接的時候。
2,已下載的數據再次請求時,僅當它與本地版本不樣時才進行下載。
ASIDownloadCache 設置下載緩存
它對Get請求的響應數據進行緩存(被緩存的數據必需是成功的200請求):
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; 當設置緩存策略后,所有的請求都被自動的緩存起來。
另外,如果僅僅希望某次請求使用緩存操作,也可以這樣使用: ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDownloadCache:[ASIDownloadCache sharedCache]];
多種的緩存並存
僅僅需要創建不同的ASIDownloadCache,並設置緩存所使用的路徑,並設置到需要使用的request實例中:
ASIDownloadCache *cache = [[[ASIDownloadCache alloc] init] autorelease];
[cache setStoragePath:@"/Users/ben/Documents/Cached-Downloads"];
[self setMyCache:cache];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[self myCache]];
緩存策略
緩存策略是我們控制緩存行為的主要方式,如:什么時候進行緩存,緩存數據的利用方式。
以下是策略可選列表(可組合使用):
ASIUseDefaultCachePolicy 這是一個默認的緩存策略“ASIAskServerIfModifiedWhenStaleCachePolicy”,這個很明白,見名知意(它不能與其它策略組合使用)
ASIDoNotReadFromCacheCachePolicy 所讀數據不使用緩存
ASIDoNotWriteToCacheCachePolicy 不對緩存數據進行寫操作
ASIAskServerIfModifiedWhenStaleCachePolicy 默認緩存行為,request會先判斷是否存在緩存數據。a, 如果沒有再進行網絡請求。 b,如果存在緩存數據,並且數據沒有過期,則使用緩存。c,如果存在緩存數據,但已經過期,request會先進行網絡請求,判斷服務器版本與本地版本是否一樣,如果一樣,則使用緩存。如果服務器有新版本,會進行網絡請求,並更新本地緩存
ASIAskServerIfModifiedCachePolicy 與默認緩存大致一樣,區別僅是每次請求都會 去服務器判斷是否有更新
ASIOnlyLoadIfNotCachedCachePolicy 如果有緩存在本地,不管其過期與否,總會拿來使用
ASIDontLoadCachePolicy 僅當有緩存的時候才會被正確執行,如果沒有緩存,request將被取消(沒有錯誤信息)
ASIFallbackToCacheIfLoadFailsCachePolicy 這個選項經常被用來與其它選項組合使用。請求失敗時,如果有緩存當網絡則返回本地緩存信息(這個在處理異常時非常有用)
如果設置了“defaultCachePolicy”則所有的請求都會使用此緩存。
緩存存儲方式
你可以設置緩存的數據需要保存多長時間,ASIHTTPRequest提供了兩種策略:
a,ASICacheForSessionDurationCacheStoragePolicy,默認策略,基於session的緩存數據存儲。當下次運行或[ASIHTTPRequest clearSession]時,緩存將失效。
b,ASICachePermanentlyCacheStoragePolicy,把緩存數據永久保存在本地,
如:
ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL:url ];
[ request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy ];
另外,也可以使用clearCachedResponsesForStoragePolicy來清空指定策略下的緩存數據。
緩存其它特性
設置是否按服務器在Header里指定的是否可被緩存或過期策略進行緩存:
[[ ASIDownloadCache sharedCache ] setShouldRespectCacheControlHeaders:NO ];
設置request緩存的有效時間:
[ request setSecondsToCache:60*60*24*30 ]; // 緩存30天
可以判斷數據是否從緩存讀取:
[ request didUseCachedResponse ];
設置緩存所使用的路徑:
[ request setDownloadDestinationPath:[[ ASIDownloadCache sharedCache ] pathToStoreCachedResponseDataForRequest:request ]];
實現自定義的緩存
只要簡單的實現ASICacheDelegate接口就可以被用來使用。
#import <UIKit/UIKit.h>
#import "ASIDownloadCache.h"
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>{
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@property(nonatomic,retain)ASIDownloadCache *myCache;
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[_viewController release];
[_myCache release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//聲明一個全局的的緩存
//自定義一個緩存
ASIDownloadCache *cache=[[ASIDownloadCache alloc] init];
self.myCache=cache;
[cache release];
//設置緩存路徑
NSString *cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
[self.myCache setStoragePath:[cachePath stringByAppendingPathComponent:@"resource"]];
[self.myCache setDefaultCachePolicy:ASIDoNotReadFromCacheCachePolicy];
-(void)click{
NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];
//NSURLRequest *request=[NSURLRequest requestWithURL:url];
// [webview loadRequest:request];
ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];
//獲取全
AppDelegate *appdelegate=[UIApplication sharedApplication].delegate;
//設置緩存方式
[request setDownloadCache:appdelegate.myCache];
//設置緩存數據存儲策略,這里如果無更新和無法聯網就讀取緩存數據。
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
request.delegate=self;
[request startAsynchronous];
}
-(void)requestStarted:(ASIHTTPRequest *)request{
NSLog(@"%@",request.responseString);
}
#pragma mark -請求數據
-(void)requestFinished:(ASIHTTPRequest *)request{
NSLog(@"requestfinish---->%@",request.responseString);
[webview loadHTMLString:request.responseString baseURL:nil];
}