OGeek|极客世界-中国程序员成长平台

标题: android - 通过 React Native 使用 must-revalidate 和 NSURLSession [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 16:37
标题: android - 通过 React Native 使用 must-revalidate 和 NSURLSession

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

public, max-age=10, must-revalidate

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

基于 14.9.4 Cache Revalidation and Reload Controls部分这不应该是预期的行为,但也许我错了。

Because a cache MAY be configured to ignore a server'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's Expires or max-age value, the cached response is stale.)

我发现了一个关于这个主题的有用讨论:HTTP Cache Control max-age, must-revalidate .根据讨论,协议(protocol)似乎有点

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

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

有什么线索吗?



Best Answer-推荐答案


我做了一些测试,似乎在 iOS(使用 NSURLSession)中,只要设置了 must-revalidate,就不会考虑 max-age。对于可以同时使用这两个值的 Android,情况就不同了。

附:如果 HTTP 协议(protocol)方面的专家对此主题有任何线索,请告诉我。

关于android - 通过 React Native 使用 must-revalidate 和 NSURLSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37543129/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4