简体   繁体   English

改造:服务器返回ETag和Cache-Control:max-age = 60。 如果请求<60s,是否应该使用缓存?

[英]Retrofit: server returns both ETag and Cache-Control: max-age=60. Shouldn't the cache be used if request < 60s?

I am using Retrofit, configured to used OkHttp with a cache. 我正在使用Retrofit,配置为使用带缓存的OkHttp。 I am calling this api: https://api.github.com/users/bod/repos which returns both an Etag and a Cache-Control: public, max-age=60, s-maxage=60 headers. 我称之为api: https Cache-Control: public, max-age=60, s-maxage=60它返回EtagCache-Control: public, max-age=60, s-maxage=60 header。

I make two requests, in less than 60 seconds, so I was expecting the second one to not perform any network at all and to use the cache, per the Cache-Control directive. 我在不到60秒的时间内发出两个请求,所以我希望第二个请求根本不执行任何网络并根据Cache-Control指令使用缓存。 But that's not what I see. 但那不是我所看到的。

I am guessing this is because the Etag directive takes precedence? 我猜这是因为Etag指令优先?

Is that correct / normal / expected behavior? 这是正确/正常/预期的行为吗?

RFC2068 Hypertext Transfer Protocol -- HTTP/1.1 published in 1997 details both ETag and Cache-Control Headers. RFC2068超文本传输​​协议 - 1997年发布的HTTP / 1.1详细说明了ETagCache-Control Headers。 Later documents, RFC2616 and RFC7232 both expand on the ETag header and how it can be used with If-None-Match . 后来的文档RFC2616RFC7232都扩展了ETag标头以及它如何与If-None-Match

RFC2616, 13.3 Validation Model contains the answer to your question: RFC2616,13.3验证模型包含您的问题的答案:

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. 我们称之为“验证”缓存条目。

It then goes on to list validation models, including Entity Tag (ETag) Cache validators along with Last-Modified dates. 然后继续列出验证模型,包括实体标签(ETag)缓存验证器以及最后修改日期。 A stale cache entry is one where the maxage or other expiry mechanism has occurred for that resource. 过时的缓存条目是该资源发生maxage或其他到期机制的条目。

So the behavior of your system is unexpected. 所以系统的行为是出乎意料的。 It may be worth testing content with and without the ETag header to verify if the local cache is working at all. 使用和不使用ETag标头测试内容可能值得验证本地缓存是否正常工作。

Did you set up caching properly in retrofit? 您是否在改造中正确设置了缓存? Something like: 就像是:

    // create the cache
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.cache(new Cache(new File(context.getCacheDir(), "ok-http-cache"),
            1024 * 1024 * 5)); // 5 MB cache


    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(builder.build()) // set the cache created above
            .build();
    api = retrofit.create(Api.class); // Api is the interface with the @GET, @POST annotations

This is trivial if you know it but you didn't mention your implementation 如果你知道它,这是微不足道的,但你没有提到你的实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM