【问题标题】:ASIDownloadCache is not updated with new contentASIownloadCache 未更新为新内容
【发布时间】:2012-03-15 13:10:44
【问题描述】:

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

根据下面的代码,它应该每次都检查服务器是否有新内容可用。我检查了服务器日志,它正在打印 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;

我在这里缺少什么?

【问题讨论】:

    标签: iphone objective-c ios4 asihttprequest


    【解决方案1】:

    终于我自己弄清楚了那里发生了什么。这只是一个愚蠢的错误和对 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];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 2023-02-13
      • 2020-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多