2011-06-19 6 views
0

私のアプリケーションに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 

これは、ウェブサイト上の例から取られています。

しかし、上記のように、これは私が望む効果がないようです。

どのようにしてキャッシュからデータを削除したのですか?

+0

何が起こっていますか? – JosephH

+0

実際には効果がありません - 2分間隔でテストしたので、setSecondsToCacheで秒数を60 * 2に設定して3分待ってから、iPhoneを飛行機モードにしてもキャッシュデータが使用されます。だから、それは何の効果もないようです。 – SMSidat

+0

要求にどのcachePolicyを設定していますか? – JosephH

答えて

0

canUseCachedDataForRequestにブレークポイントを設定してみてください(ASIDownloadCache.m)。一歩一歩進んでコードがどのような経路をとるかを見てください。

isCachedDataCurrentForRequestを呼び出すパスの1つに従う必要があります。データの有効期限が切れている場合は、NOを返します。

setSecondsToCache 'のisCachedDataCurrentForRequestはNOを返す作るべきではない、キャッシュされたオブジェクト、ヘッダの「X-ASIHTTPRequestは、有効期限」を設定すべきであることが表示されますcodeを見ているから - 私は介してのみ、デバッグすることはこれであなたを伝えるために起こっていると思いますあなたの場合は間違っていると指摘してください。

0

私は同じ問題があります。メソッド[request 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; 
} 
関連する問題