5

ASIHTTP(http://allseeing-i.com/ASIHTTPRequest/How-to-use#using_a_download_cache)を使用してデータをキャッシュに保存しようとしています。何が間違っているのかわかりませんが、データをキャッシュに保存していないようです。次のコードの出力は次のとおりです。

2010-08-12 15:44:13.801 TabBar[51728:207] Success is YES
2010-08-12 15:44:13.802 TabBar[51728:207] Success is NO
2010-08-12 15:44:13.804 TabBar[51728:207] Success is YES
2010-08-12 15:44:13.805 TabBar[51728:207] S-----------
2010-08-12 15:44:13.874 TabBar[51728:207] Success is YES
2010-08-12 15:44:13.874 TabBar[51728:207] Success is NO
2010-08-12 15:44:13.876 TabBar[51728:207] Success is YES

コードは次のとおりです。

[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:
                              ASICacheForSessionDurationCacheStoragePolicy];
[[ASIDownloadCache sharedCache] 
                    setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL 
                                URLWithString:@"http://www.nytimes.com/"]];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request startSynchronous];

request = [ASIHTTPRequest requestWithURL:[NSURL 
                                URLWithString:@"http://www.nytimes.com/"]];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request startSynchronous];
BOOL success = ([request responseStatusCode] == 200);
NSLog(@"Success is %@\n", (success ? @"YES" : @"NO"));

success = [request didUseCachedResponse];
NSLog(@"Success is %@\n", (success ? @"YES" : @"NO"));

success = ([[request responseData] length]);
NSLog(@"Success is %@\n", (success ? @"YES" : @"NO"));

NSLog(@"S-----------");
request = [ASIHTTPRequest requestWithURL:[NSURL 
                    URLWithString:@"http://www.nytimes.com/"]];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request startSynchronous];
success = ([request responseStatusCode] == 200);
NSLog(@"Success is %@\n", (success ? @"YES" : @"NO"));

success = [request didUseCachedResponse];
NSLog(@"Success is %@\n", (success ? @"YES" : @"NO"));

success = ([[request responseData] length]);
NSLog(@"Success is %@\n", (success ? @"YES" : @"NO"));

誰かがこれを達成する方法についてのコードスニペットを教えてもらえますか?

ありがとう!

-------------------------------------------------- ---------------------

新しいコードの改訂:

NSURL *url = [NSURL URLWithString:imageURL];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
[request setSecondsToCache:60*60*24*30]; // Cache for 30 days

[request startSynchronous];//startAsynchronous];

BOOL success = [request didUseCachedResponse];
NSLog(@"------------>>>>>>> Success is %@\n", (success ? @"YES" : @"NO"));

NSData *responseData = [request responseData];
UIImage *image = [[UIImage alloc] initWithData:responseData];

これは[requeststartSynchronous]でのみ機能します。非同期メソッドを試してみると、この関数は呼び出されません。

- (void)requestFinished:(ASIHTTPRequest *)request
4

2 に答える 2

4

あなたはこのようなものが欠けています、

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
[request setSecondsToCache:60*60*24*30]; // Cache for 30 days
[request setDelegate:self]; // A delegate must be specified
[request setDidFinishSelector:@selector(requestFinished:)]; // And an appropriate selector specified

そして、あなたも持っている必要があります

- (void)requestFinished:(ASIHTTPRequest *)request {
  BOOL success = [request didUseCachedResponse];
  NSLog(@"------------>>>>>>> Success is %@\n", (success ? @"YES" : @"NO"));

  NSData *responseData = [request responseData];
  UIImage *image = [[UIImage alloc] initWithData:responseData];

  ...
}

これでうまくいくはずです。

于 2011-04-07T23:19:01.807 に答える
1

nytimes.comは、多くの「キャッシュしない」タイプのヘッダーを設定します。

    $ HEAD http://www.nytimes.com/           
    200 OK
    キャッシュ制御:キャッシュなし
    日付:2010年8月13日金曜日07:14:46 GMT
    プラグマ:キャッシュなし
    サーバー:Sun-ONE-Web-Server / 6.1
    コンテンツ-長さ:121266
    コンテンツタイプ:text / html
    有効期限:1994年12月1日木曜日16:00:00 GMT

だから私はこれらが問題だと思います。

次の設定を試すことができます:

[[ASIDownloadCache sharedCache] shouldRespectCacheControlHeaders:NO];
于 2010-08-13T07:19:07.703 に答える