ASIHTTPRequest 下载缓存问题 - 无法在缓存中保存/加载日期
Posted
技术标签:
【中文标题】ASIHTTPRequest 下载缓存问题 - 无法在缓存中保存/加载日期【英文标题】:ASIHTTPRequest Download cache problem - unable to save/load date in cache 【发布时间】:2010-08-12 19:55:56 【问题描述】:我正在尝试使用 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"));
谁能给我一些代码sn-p来告诉我如何做到这一点?
谢谢!
----------------------------------------------- ------------------------------------
新代码修订:
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
【问题讨论】:
我的意思是说数据,而不是标题中的日期:P 如果你能合理地格式化你的代码就好了。 【参考方案1】:你错过了这样的东西,
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];
...
这应该可以解决问题。
【讨论】:
【参考方案2】:nytimes.com 设置了很多“不要缓存我”类型的标题:
$头 http://www.nytimes.com/ 200 好 缓存控制:无缓存 日期:格林威治标准时间 2010 年 8 月 13 日星期五 07:14:46 编译指示:无缓存 服务器:Sun-ONE-Web-Server/6.1 内容长度:121266 内容类型:文本/html 到期:格林威治标准时间 1994 年 12 月 1 日星期四 16:00:00所以我认为这些是问题所在。
您可以尝试设置:
[[ASIDownloadCache sharedCache] shouldRespectCacheControlHeaders:NO];
【讨论】:
是的,我试过了,但仍然没有缓存...我使用了 [[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO]; 为那个错字道歉 - 你所拥有的是正确的(我现在已经修正了我的答案)。我建议您尝试单步执行 ASIDownloadCache.m 中的 storeResponseForRequest,如果这似乎正确存储了响应,则尝试单步执行 cachedHeadersForRequest,然后再传给它的调用者。 所以我试图在 "" 中放置断点,它一直都进入这个 if 条件: if (policy == ASIIgnoreCachePolicy) [[self accessLock] unlock];返回; 我说的是“storeResponseForRequest”方法 干得好!我不确定为什么会这样;你应该得到你正在设置的策略,ASIOnlyLoadIfNotCachedCachePolicy。我认为您需要进一步调试。以上是关于ASIHTTPRequest 下载缓存问题 - 无法在缓存中保存/加载日期的主要内容,如果未能解决你的问题,请参考以下文章
ASIHttpRequest或者SDWebImage给UIImageView加载图片的逻辑是什么样子的