简体   繁体   English

改造响应:缓存控制。 这是什么意思?

[英]Retrofit response : Cache Control. What does it mean?

I use retrofit and picasso library. 我使用改造和毕加索图书馆。 Picasso manages caching itself. 毕加索自行管理缓存。 But when I view logcat, I see log below. 但是,当我查看logcat时,会看到下面的日志。 What does it mean? 这是什么意思? Doesn't the webservice and backend send cache info correctly? Web服务和后端不能正确发送缓存信息吗? How can I solve this to make Retrofit cache correctly. 我如何解决此问题,以使Retrofit缓存正确。 So, Does this data make sense? 那么,这些数据有意义吗?

Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Content-Type: application/json
Date: Wed, 14 Dec 2016 07:15:48 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
OkHttp-Received-Millis: 1481597410805
OkHttp-Response-Source: NETWORK 200
OkHttp-Selected-Protocol: http/1.1
OkHttp-Sent-Millis: 1481597409021
Pragma: no-cache
Server: Apache
Transfer-Encoding: chunked

You can set cache option to OkHttp with using Interceptor. 您可以使用Interceptor将缓存选项设置为OkHttp。

OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new CachingControlInterceptor());
Retrofit restAdapter = new Retrofit.Builder()
        .client(client)
        .baseUrl(Constants.BASE_URL)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

and CachingControlInterceptor is: 和CachingControlInterceptor是:

public class CachingControlInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    // Add Cache Control only for GET methods
    if (request.method().equals("GET")) {
        if (ConnectivityUtil.checkConnectivity(YaootaApplication.getContext())) {
            // 1 day
           request = request.newBuilder()
                    .header("Cache-Control", "only-if-cached")
                    .build();
        } else {
            // 4 weeks stale
           request = request.newBuilder()
                    .header("Cache-Control", "public, max-stale=2419200")
                    .build();
        }
    }

    Response originalResponse = chain.proceed(request);
    return originalResponse.newBuilder()
        .header("Cache-Control", "max-age=600")
        .build();
}
}

See this: https://stackoverflow.com/a/34401686/850347 看到这个: https : //stackoverflow.com/a/34401686/850347

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

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