草庐IT

android - Retrofit : server returns both ETag and Cache-Control: max-age=60. request < 60s 是否应该使用缓存?

coder 2023-11-23 原文

我正在使用 Retrofit,配置为使用带缓存的 OkHttp。 我正在调用这个 api:https://api.github.com/users/bod/repos它返回一个 Etag 和一个 Cache-Control: public, max-age=60, s-maxage=60 header 。

我在不到 60 秒的时间内发出了两个请求,因此我希望第二个请求完全不执行任何网络并根据 Cache-Control 指令使用缓存。但这不是我所看到的。

我猜这是因为 Etag 指令优先?

这是正确/正常/预期的行为吗?

最佳答案

RFC2068 Hypertext Transfer Protocol -- HTTP/1.1 1997 年发布的详细信息 ETagCache-Control header 。后来的文件,RFC2616RFC7232两者都扩展了 ETag header 以及它如何与 If-None-Match 一起使用。

RFC2616, 13.3 Validation Model包含您问题的答案:

When a cache has a stale entry that it would like to use as a response to a client's request, it first has to check with the origin server (or possibly an intermediate cache with a fresh response) to see if its cached entry is still usable. We call this "validating" the cache entry.

然后它继续列出验证模型,包括实体标签 (ETag) 缓存验证器以及上次修改日期。过时的缓存条目是该资源发生 maxage 或其他过期机制的条目。

所以你的系统行为是出乎意料的。可能值得测试带有和不带有 ETag header 的内容,以验证本地缓存是否正常工作。

关于android - Retrofit : server returns both ETag and Cache-Control: max-age=60. request < 60s 是否应该使用缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39750834/

有关android - Retrofit : server returns both ETag and Cache-Control: max-age=60. request < 60s 是否应该使用缓存?的更多相关文章

随机推荐