/*
ASIUseDefaultCachePolicy
這是一個默認的緩存策略“ASIAskServerIfModifiedWhenStaleCachePolicy”,這個很明白,見名知意(它不能與其它策略組合使用)
ASIDoNotReadFromCacheCachePolicy
所讀數據不使用緩存
ASIDoNotWriteToCacheCachePolicy
不對緩存數據進行寫操作
ASIAskServerIfModifiedWhenStaleCachePolicy
默認緩存行爲,request會先判斷是否存在緩存數據。
a.如果沒有再進行網絡請求。
b.如果存在緩存數據,並且數據沒有過期,則使用緩存。
c.如果存在緩存數據,但已經過期,request會先進行網絡請求,判斷服務器版本與本地版本是否一樣,如果一樣,則使用緩存。如果服務器有新版本,會進行網絡請求,並更新本地緩存
ASIAskServerIfModifiedCachePolicy
與默認緩存大致一樣,區別僅是每次請求都會 去服務器判斷是否有更新
ASIOnlyLoadIfNotCachedCachePolicy
如果有緩存在本地,不管其過期與否,總會拿來使用
ASIDontLoadCachePolicy
僅當有緩存的時候纔會被正確執行,如果沒有緩存,request將被取消(沒有錯誤信息)
ASIFallbackToCacheIfLoadFailsCachePolicy
這個選項經常被用來與其它選項組合使用。請求失敗時,如果有緩存當網絡則返回本地緩存信息(這個在處理異常時非常有用)
如果設置了“defaultCachePolicy”則所有的請求都會使用此緩存。
*/
/*
設置緩存的數據需要保存多長時間,ASIHTTPRequest提供了兩種策略:
a.ASICacheForSessionDurationCacheStoragePolicy
默認策略,基於session的緩存數據存儲。當下次運行或[ASIHTTPRequest clearSession]時,緩存將失效。
b.ASICachePermanentlyCacheStoragePolicy
把緩存數據永久保存在本地
*/
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, retain) ASIDownloadCache *cache;
@end
@implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[_cache release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// [self clearAllCached];
// [self testFirst];
// [self testCustomeCache];
}
- (void)testFirst
{
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy];
NSString *urlString = @"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away";
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlString]];
request.downloadCache = [ASIDownloadCache sharedCache];
request.cachePolicy = ASIAskServerIfModifiedCachePolicy | ASIFallbackToCacheIfLoadFailsCachePolicy;
request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
request.delegate = self;
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// didUseCachedResponse爲YES,爲從緩存讀取
BOOL success = [request didUseCachedResponse];
NSLog(@"success %d", success);
NSLog(@"responseString %@", [request responseString]);
}
- (void)testCustomeCache
{
//設置緩存路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *path = [documentDirectory stringByAppendingPathComponent:@"resource"];
NSLog(@"path %@", path);
ASIDownloadCache *downloadCache = [[ASIDownloadCache alloc] init];
// 要自己持有cache
self.cache = downloadCache;
[downloadCache release];
// 設置默認緩存策略
self.cache.defaultCachePolicy = ASIUseDefaultCachePolicy;
// 設置自定義緩存路徑
self.cache.storagePath = path;
// 設置是否按服務器在Header裏指定的 是否可被緩存或過期策略進行緩存
[self.cache setShouldRespectCacheControlHeaders:NO];
NSString *urlString = @"http://allseeing-i.com/ASIHTTPRequest/tests/cache-away";
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlString]];
// 設置下載緩存
request.downloadCache = self.cache;
// 設置緩存策略
request.cachePolicy = ASIAskServerIfModifiedCachePolicy | ASIFallbackToCacheIfLoadFailsCachePolicy;
// 設置存儲策略
request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
// 設置緩存時間
// [request setSecondsToCache:60*60*24*30]; // Cache for 30 days
request.delegate = self;
[request startAsynchronous];
}
// 清除緩存
- (void)clearAllCached
{
dispatch_async(dispatch_get_main_queue(), ^{
[self.cache clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
[self.cache clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
});
}
- (IBAction)getData:(UIButton *)sender {
[self testCustomeCache];
}
- (IBAction)clearData:(UIButton *)sender {
[self clearAllCached];
}