ASIDownloadCache缓存例子

/*

    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

{

    // didUseCachedResponseYES,为从缓存读取

    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];

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值