ASIHTTPRequest下载缓存问题 - 无法在缓存中保存/加载日期

时间:2010-08-12 19:55:57

标签: iphone asihttprequest

我正在尝试使用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];

这仅适用于[request startSynchronous];如果我尝试异步方法,它会调用此函数:

- (void)requestFinished:(ASIHTTPRequest *)request

2 个答案:

答案 0 :(得分: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];

  ...
}

这应该可以解决问题。

答案 1 :(得分:1)

nytimes.com设置了很多“不要缓存我”类型的标题:

    $ HEAD http://www.nytimes.com/           
    200 OK
    Cache-Control: no-cache
    Date: Fri, 13 Aug 2010 07:14:46 GMT
    Pragma: no-cache
    Server: Sun-ONE-Web-Server/6.1
    Content-Length: 121266
    Content-Type: text/html
    Expires: Thu, 01 Dec 1994 16:00:00 GMT

所以我希望这些都是问题所在。

您可以尝试设置:

[[ASIDownloadCache sharedCache] shouldRespectCacheControlHeaders:NO];