【问题标题】:Retrofit response : Cache Control. What does it mean?改造响应:缓存控制。这是什么意思?
【发布时间】:2017-04-29 11:12:32
【问题描述】:

我使用改造和毕加索图书馆。 Picasso 自己管理缓存。但是当我查看 logcat 时,我会看到下面的日志。这是什么意思?网络服务和后端没有正确发送缓存信息吗?我该如何解决这个问题才能正确地进行改造缓存。那么,这些数据有意义吗?

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

【问题讨论】:

标签: android caching picasso retrofit2


【解决方案1】:

您可以使用拦截器将缓存选项设置为 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();

并且 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();
}
}

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

【讨论】:

  • 这正是我需要让我的 squid 服务器识别需要为 Fresco 缓存的请求(通过 OkHttp)!!!!!!!
猜你喜欢
  • 2017-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-12
  • 2023-03-12
相关资源
最近更新 更多