菜鸟教程小白 发表于 2022-12-11 16:37:35

android - 通过 React Native 使用 must-revalidate 和 NSURLSession


                                            <p><p>在一个示例演示应用程序中,我使用 React Native 进行网络调用。 React Native 使用 <code>NSURLSession</code> 和 <code>NSURLRequestUseProtocolCachePolicy</code> 作为默认缓存策略。当进行网络调用时,服务返回的响应包含这些缓存控制 header :</p>

<pre><code>public, max-age=10, must-revalidate
</code></pre>

<p>我能注意到的是,如果 <code>max-revalidate</code> 存在,<code>NSURLSession</code> 不会遵守 <code>10</code> 秒。相反,如果在 <code>10</code> 秒内进行新的网络调用时后者不存在,则返回缓存响应,否则进行新的调用。</p>

<p>基于 <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4" rel="noreferrer noopener nofollow">14.9.4 Cache Revalidation and Reload Controls</a>部分这不应该是预期的行为,但也许我错了。</p>

<blockquote>
<p>Because a cache MAY be configured to ignore a server&#39;s specified
expiration time, and because a client request MAY include a max- stale
directive (which has a similar effect), the protocol also includes a
mechanism for the origin server to require revalidation of a cache
entry on any subsequent use. When the must-revalidate directive is
present in a response received by a cache, that cache MUST NOT use the
entry after it becomes stale to respond to a subsequent request
without first revalidating it with the origin server. (I.e., the cache
MUST do an end-to-end revalidation every time, if, based solely on the
origin server&#39;s Expires or max-age value, the cached response is
stale.)</p>
</blockquote>

<p>我发现了一个关于这个主题的有用讨论:<a href="https://stackoverflow.com/questions/2932890/http-cache-control-max-age-must-revalidate" rel="noreferrer noopener nofollow">HTTP Cache Control max-age, must-revalidate</a> .根据讨论,协议(protocol)似乎有点</p>

<blockquote>
<p>ambiguous here, but in practice I have found that must-revalidate
means it must revalidate regardless of max-age.</p>
</blockquote>

<p>一个非常奇怪的事情是,如果我对 Android 做同样的事情,同时使用 <code>max-age</code> 和 <code>must-revalidate</code> 行为是正确的。 <code>10</code> 秒后将发出一个新请求。在 <code>10</code> 秒内没有发出任何请求。</p>

<p>有什么线索吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我做了一些测试,似乎在 iOS(使用 NSURLSession)中,只要设置了 <code>must-revalidate</code>,就不会考虑 <code>max-age</code>。对于可以同时使用这两个值的 Android,情况就不同了。</p>

<p>附:如果 HTTP 协议(protocol)方面的专家对此主题有任何线索,请告诉我。</p></p>
                                   
                                                <p style="font-size: 20px;">关于android - 通过 React Native 使用 must-revalidate 和 NSURLSession,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37543129/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37543129/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: android - 通过 React Native 使用 must-revalidate 和 NSURLSession