我想设置一个 60 秒到期的缓存。xml 文件已成功缓存,但即使服务器更新数据,缓存也会在时间到期后继续使用。因此,我想弄清楚我犯了什么错误,并寻求任何解决方案来解决它。
我为发送异步 http 请求编写的代码:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestFinished:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request setSecondsToCache:60];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy | ASIAskServerIfModifiedWhenStaleCachePolicy];
[request setNumberOfTimesToRetryOnTimeout:2];
[request setTimeOutSeconds:30];
[[self queue] addOperation:request];
我为接收请求编写的代码:
- (void)requestFinished:(ASIHTTPRequest *)request {
BOOL success = [request didUseCachedResponse];
NSLog(@"Success is %@\n", (success ? @"YES" : @"NO"));
NSData *data = [request responseData];
}
这是 xml 的标头:
Request Method:GET
Status Code:200 OK
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Type:application/xml;charset=utf-8
Date:Mon, 15 Apr 2013 08:49:59 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8b mod_jk/1.2.26 PHP/5.2.5
Set-Cookie:Expires=Mon, 15-Apr-2013 08:50:29 GMT;
这是我在 /iPhone Simulator/5.0/Applications/XXX/Library/Caches/ASIHTTPRequestCache/PermanentStore 中找到的 .cachedheaders 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Connection</key>
<string>Keep-Alive</string>
<key>Content-Length</key>
<string>9296</string>
<key>Content-Type</key>
<string>application/xml;charset=utf-8</string>
<key>Date</key>
<string>Fri, 12 Apr 2013 09:49:33 GMT</string>
<key>Keep-Alive</key>
<string>timeout=5, max=99</string>
<key>Server</key>
<string>Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8b mod_jk/1.2.26 PHP/5.2.5</string>
<key>Set-Cookie</key>
<string>Expires=Fri, 12-Apr-2013 09:50:04 GMT;</string>
<key>X-ASIHTTPRequest-Expires</key>
<real>1365989738.766006</real>
<key>X-ASIHTTPRequest-Response-Status-Code</key>
<integer>200</integer>
</dict>
</plist>
提前感谢您的帮助!