2012-03-15 46 views
0

以下是使用ASIHTTPRequest从远程服务器下载和缓存图像的源代码。如果服务器未使用新图像更新,则使用图像的缓存副本。它在初始请求中缓存所有图像,并且工作正常。ASIDownloadCache未更新为新内容

根据下面的代码,它应该检查服务器是否每次都有新的内容可用。我检查了服务器日志,它正在打印304状态码。但是当我更新最新的图像时,它不会显示新图像。而不是它显示缓存的图像。但服务器日志表示它正在通过状态码200发送新图像。为了避免这种行为,我必须删除该应用程序并重新安装它。

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
request.timeOutSeconds = kDefaultTimeInterval; 
request.validatesSecureCertificate = NO; 
[request setDownloadCache:[ASIDownloadCache sharedCache]]; 
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy]; 
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; 
[request setSecondsToCache:kSecondsToCache]; 
[request startSynchronous]; 

if (![request didUseCachedResponse]) { 
    NSLog(@" Cache miss. Loading resource %@", location); 
} else { 
    NSLog(@" Cache hit!"); 
} 
NSLog(@" *** image data size: %u bytes", [request.responseData length]); 
return ([request error]) ? nil : request.responseData; 

我在这里失踪了什么?

回答

0

最后我想出了自己在那里发生了什么。这只是一个愚蠢的错误和对API的错误理解。

通过秒缓存,我想这是为了保持缓存,直到设置值。但似乎不是。根据文档

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 

因此它是由服务器设置的重写值。因此,即使服务器更新了新内容,并且如果为请求设置的失效日期未过期,本地副本也将被使用。

作为修复我刚刚删除行[request setSecondsToCache:kSecondsToCache];