简体   繁体   English

URLResponse已缓存,尽管响应的缓存控制标头设置为no-cache

[英]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.

相关问题 是否可以防止将带有响应标头`Cache-Control:Private`的文件缓存在NSURLCache中? - Would a file with a response header `Cache-Control:Private` be prevented from being cached in a NSURLCache? 绕过http响应头Cache-Control:如何设置缓存过期? - Bypassing http response header Cache-Control: how to set cache expiration? NSURLCache不起作用(使用Cache-Control = no-cache) - NSURLCache doesn't work (with Cache-Control = no-cache) Cache-Control:max-age不会使AFNetworking缓存响应 - Cache-Control: max-age does not make AFNetworking cache the response 使用storeCachedResponse存储在缓存中后,未检索到URLresponse - URLresponse is not retrieved after storing in cache using storeCachedResponse Alamofire / NSURLSession缓存,带有专用的Cache-Control和max-age - Alamofire/NSURLSession caching with private Cache-Control and max-age 在UIWebView中,如何强制缓存“ Pragma:no-cache”资源? - In UIWebView, how to forcibly cache resources that are 'Pragma:no-cache'? 即使没有条件请求,设置缓存控制是否也不会自动启用缓存? - Does not setting cache-control automatically enable caching even without conditional request? NSURLCache与NSURLSession一起不尊重:Cache-Control:max-age:86000,private,must-revalidate - NSURLCache, together with NSURLSession, does not respect: Cache-Control: max-age:86000, private, must-revalidate Swift SwiftUI - LazyVStack AVPlayer 设置应缓存的最大视频数或设置最大缓存大小 - Swift SwiftUI - LazyVStack AVPlayer set the max number of videos that should be cached or set max cache size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM