7

我在 iPhone 上使用 NSURLConnection 异步连接进行请求缓存时遇到了一个小问题。我不知道我是否理解错误,或者 Cocoa 是否在做与它应该做的相反的事情......

NSURLRequest 的文档说:

NSURLRequestReloadIgnoringLocalCacheData

指定应从原始源加载 URL 加载的数据。不应使用现有缓存数据来满足 URL 加载请求。

和:

NSURLRequestReloadIgnoringLocalAndRemoteCacheData

指定不仅应忽略本地缓存数据,而且应指示代理和其他中间体在协议允许的范围内忽略其缓存。

现在,如果我发送一个带有 NSURLRequestReloadIgnoringLocalCacheData 的 NSURLRequest(它应该忽略本地缓存,但如果可用,则使用远程缓存),发送的标头是:

获取 /仪表板 HTTP/1.1
用户代理:XBlip1.0 CFNetwork/422.15.2 Darwin/9.6.0 (i386) (iMac8%2C1)
X-Blip-Api: 0.02
接受:应用程序/json
授权:基本(...)
接受语言:en-us
接受编码:gzip,放气
连接:保持活动
主机:api.blip.pl

状态为 200 OK。但是,如果我使用 NSURLRequestReloadIgnoringLocalAndRemoteCacheData,顾名思义,它应该忽略本地和远程缓存,添加一个额外的标头:

如果无匹配:“d751713988987e9331980363e24189ce”

响应是 304 Not Modified。我检查了 HTTP RFC,对于“If-None-Match”,它说:

如果任何实体标签与该资源上的类似 GET 请求(没有 If-None-Match 标头)的响应中返回的实体的实体标签匹配,(...)那么服务器不得执行请求的方法 (...) 相反,如果请求方法是 GET 或 HEAD,服务器应该以 304(未修改)响应进行响应

所以似乎如果我使用 NSURLRequestReloadIgnoringLocalAndRemoteCacheData,而不是忽略远程缓存,Cocoa 明确告诉远程服务器它应该使用远程缓存,如果我使用 NSURLRequestReloadIgnoringLocalCacheData,它不会添加该行,实际上远程缓存不是用过的。

那么这里到底发生了什么?我错过了什么,还是 Cocoa 设置了错误的标题?

4

3 回答 3

10

从文档中:

指定不仅应忽略本地缓存数据,而且应指示代理和其他中间体在协议允许的范围内忽略其缓存。

来自 NSURLRequest.h (10.5 SDK)

指定不仅应忽略本地缓存数据,而且应指示代理和其他中间体在协议允许的范围内忽略其缓存。未实现。

注意区别:未实现

错误报告时间...

于 2009-12-18T15:35:37.720 回答
2

By adding the NSURLRequestReloadIgnoringLocalAndRemoteCacheData parameter you are instructing the local cache and any proxy servers which may handle the request between your client and the target server that they are not to return their own version of the response data. I think the key-component here is that the RemoteCache is likely to be a proxy and you are just identifying that the request should always reach the real server and not a proxied copy.

Adding the "stupidly-long-named" parameter you are inferring that your application already has a previous copy of the request and so is only interested in actually receiving data from the server if it has changed, that is why you are getting a response of "304 Not Modified" from the server.

This behaviour does seem counter-intuitive as you are explicitly instructing the client not to use its own cache, which would imply that you would want to discard anything in there and certainly not use it as a reference for any subsequent requests. I believe the advanced cache parameters are provided to allow the developer to handle their own level of caching, ie. be notified if data hasn't been updated on the server so they can avoid needless re-processing.

于 2009-03-25T10:20:23.470 回答
1

Note the "proxies and other intermediates" part. You're only avoiding caches not residing on the original server. The original server can still return a 304.

于 2009-03-25T10:39:22.847 回答