2011-06-19 54 views
0

我在我的应用中使用ASIHttpRequest库,我试图为特定请求设置缓存,以便它可以在一段时间内使用(if没有数据可以从服务器或没有互联网连接),除此之外它会拒绝/删除缓存,所以不会使用它。ASIHttpRequest - 在特定时间间隔后删除缓存

所以我试图得到的请求是,首先检查它是否可以在线检索数据,如果它无法访问它(无论出于何种原因,服务器关闭或没有Internet连接可用),然后使用缓存,只要它没有过期。我希望设置一定的时间,比如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 

这是从网站上的示例获取的。

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

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

+0

你会看到什么发生? – JosephH

+0

没有效果 - 我用2分钟的时间间隔测试了它,所以我将setSecondsToCache中的秒数设置为60 * 2,并等待3分钟,并且当我将iPhone设置为飞行模式时仍然使用缓存的数据。所以它似乎没有任何影响。 – SMSidat

+0

你在请求上设置了什么cachePolicy? – JosephH

回答

0

尝试设置canUseCachedDataForRequest中的断点ASIDownloadCache.m;单步通过,看看代码需要什么路线。

它应该遵循其中一条调用isCachedDataCurrentForRequest的路径,如果数据已过期,应该返回NO。

从查看code看来,'setSecondsToCache'应该在缓存对象中设置“X-ASIHTTPRequest-Expires”头,这应该使isCachedDataCurrentForRequest返回NO - 我认为只有通过调试才会告诉你哪个点你的情况出错了。

0

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

[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy]; 

缓存将在会话关闭后被删除。

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