ASIHttpRequest - 在一定时间间隔后删除缓存

时间:2011-06-19 14:22:48

标签: objective-c ios cocoa-touch asihttprequest

我正在使用我的应用程序中的ASIHttpRequest库,我正在尝试为特定请求设置缓存,以便它可以在一段时间内使用(如果没有可用的数据)服务器或没有互联网连接),除此之外它将拒绝/删除缓存,因此不会使用它。

所以我试图得到的请求是,首先检查它是否可以在线检索数据,如果它无法访问它(无论出于何种原因,服务器已关闭或没有互联网连接可用),那么使用缓存,只要它没有过期。我希望将缓存设置为一定时间,例如12小时后将数据保存到缓存中。

到目前为止我尝试的是:

// Set secondsToCache on the request to override any expiry date for the content set by the server, and store 
// this response in the cache until secondsToCache seconds have elapsed
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setSecondsToCache:60*60*24*30]; // Cache for 30 days 

这取自网站上的例子。

然而,如上所述,这似乎没有我想要的效果。

如何在一段时间内缓存数据,之后将其从缓存中删除?

3 个答案:

答案 0 :(得分:0)

尝试在canUseCachedDataForRequest的{​​{1}}中设置断点;单步执行并查看代码采用的路径。

它应该跟随一个调用ASIDownloadCache.m的路径,如果数据已经过期,它应该返回NO。

code看来,'setSecondsToCache'应该在缓存对象中设置“X-ASIHTTPRequest-Expires”标头,这应该使isCachedDataCurrentForRequest返回NO - 我认为只有通过调试才能告诉在这种情况下,你的情况会出现问题。

答案 1 :(得分:0)

我有同样的问题。方法[request setSecondsToCache:xxx]似乎不适用于过期,它也不适用于缓存。如果你没有设置

[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];

会话结束后,缓存将被删除。

答案 2 :(得分:0)

可能有点旧,但我的代码仍在使用ios7。

-(NSData*)getResponseAsDataFromUrl:(NSURL*)sourceUrl andWithCachePolicy:(ASICachePolicy)cachePolicy andWithExpireDurationInSeconds:(NSTimeInterval)expireDuration{
 ASIHTTPRequest* request = [[ASIHTTPRequest alloc] initWithURL:sourceUrl];
[request setCachePolicy:cachePolicy];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setTimeOutSeconds:30];

//We set the expireduration to a second we desire
[request setSecondsToCache:expireDuration];
NSError* error = [request error];
NSData* jsonData;
NSDictionary* cachedHeaderDict = [[ASIDownloadCache sharedCache] cachedResponseHeadersForURL:sourceUrl];
NSDate* expireDate = [NSDate date];
NSDate* now = [NSDate date];
if([[cachedHeaderDict allKeys] containsObject:@"X-ASIHTTPRequest-Expires"]){
    NSNumber* cacheInterval = [cachedHeaderDict objectForKey:@"X-ASIHTTPRequest-Expires"];
    expireDate = [[NSDate alloc] initWithTimeIntervalSince1970:cacheInterval.doubleValue];
}
switch ([now compare:expireDate]) {
    case -1:{
  //            NSLog(@"Cache has not been expired. Using cached response.");
        jsonData = [[NSData alloc] initWithContentsOfFile:[[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:sourceUrl]];

    }
        break;
    default:{
   //            NSLog(@"Cache expired. Cleaning cached data.");
        [[ASIDownloadCache sharedCache] removeCachedDataForRequest:request];
    }
        break;
}

if(!jsonData){
    [request startSynchronous];
    if(!error){ //If there is no error, use the data
        jsonData = [request responseData];
        if(!jsonData)//This is here for cases where we get empty response and we dont want to store empty responses.
            [[ASIDownloadCache sharedCache] removeCachedDataForRequest:request];
    }else{
        NSLog(@"no response: %@",error.description);
        [[ASIDownloadCache sharedCache] removeCachedDataForRequest:request];
    }
    [request clearDelegatesAndCancel];
    request = nil;
}
return jsonData;
}
相关问题