[英]URLResponse cached although response's cache-control header is set to no-cache
In my iOS app I want to cache images that are requested from different destinations. 在我的iOS应用中,我想缓存从不同目的地请求的图像。 For downloading images I use URLSessionDataTasks with the default caching mechanism provided by URLSession.shared, which makes use of the NSURLRequestUseProtocolCachePolicy.
为了下载图像,我使用URLSessionDataTasks和URLSession.shared提供的默认缓存机制,该机制利用了NSURLRequestUseProtocolCachePolicy。
The caching works basically fine. 缓存工作正常。 Responses are being cached and cache headers like etag and cache-control "max-age" are correctly being handled.
正在缓存响应,并且已正确处理了etag和缓存控件“ max-age”之类的缓存头。 But if the server responds with the cache-control header set to "no-cache", the URLCache of the URLSession is still caching the image.
但是,如果服务器在将缓存控制标头设置为“ no-cache”的情况下进行响应,则URLSession的URLCache仍将缓存图像。 I can access the cached response via URLCache.shared.cachedResponse(for: request) and also a new data task with the same request will return time image from the cache (which I validated by using Charles proxy and I'm not seeing the request I am awaiting).
我可以通过URLCache.shared.cachedResponse(for:request)访问缓存的响应,并且具有相同请求的新数据任务也会从缓存中返回时间图像(我通过使用Charles代理进行了验证,但没有看到请求)我正在等待)。
Why isn't it correctly handling the respond's cache header? 为什么它不能正确处理响应的缓存头? Do I need to manually check the respond's cache headers?
我需要手动检查响应的缓存头吗?
The no-cache
directive doesn't mean "don't store it in the cache". no-cache
指令并不意味着“不要将其存储在缓存中”。 Rather it instructs the cache not to serve a cached response without validating with the server first. 相反,它指示缓存在不先与服务器验证的情况下不提供缓存的响应。 The RFC7234 specification says the following regarding the
no-cache
directive. RFC7234规范对
no-cache
指令进行了以下说明。
The "no-cache" response directive indicates that the response MUST NOT be used to satisfy a subsequent request without successful validation on the origin server.
“ no-cache”响应指令指示在未经原始服务器成功验证的情况下,不得将响应用于满足后续请求。 This allows an origin server to prevent a cache from using it to satisfy a request without contacting it, even by caches that have been configured to send stale responses.
这样,即使已配置为发送过期响应的缓存,原始服务器也可以防止缓存使用它来满足请求而无需联系它。
If the no-cache response directive specifies one or more field-names, then a cache MAY use the response to satisfy a subsequent request, subject to any other restrictions on caching.
如果no-cache response指令指定了一个或多个字段名,则缓存可以使用该响应来满足后续请求,但要遵守其他对缓存的限制。 However, any header fields in the response that have the field-name(s) listed MUST NOT be sent in the response to a subsequent request without successful revalidation with the origin server.
但是,如果没有使用原始服务器成功进行重新验证,则响应中具有列出的字段名称的任何头字段都不得在对后续请求的响应中发送。 This allows an origin server to prevent the re-use of certain header fields in a response, while still allowing caching of the rest of the response.
这使源服务器可以防止在响应中重复使用某些标头字段,同时仍允许缓存其余响应。
So what will happen is that for "fresh" responses with the no-cache
directive, a conditional request will be sent to verify whether the stored response can be used. 因此,对于带有
no-cache
指令的“新”响应,将发生条件请求,以验证是否可以使用存储的响应。 If the response is still valid, the server will send a 304 - Not Modified
response. 如果响应仍然有效,则服务器将发送
304 - Not Modified
响应。 Upon receiving the 304
response, the cache will serve the stored response with the no-cache
directive. 收到
304
响应后,缓存将使用no-cache
指令为存储的响应提供服务。 If the stored response is no longer valid, the server will send a new response. 如果存储的响应不再有效,则服务器将发送新的响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.