繁体   English   中英

使用Retrofit 2.0和okhttp3进行缓存

[英]Caching with Retrofit 2.0 and okhttp3

我正在尝试使用Retrofit和OkHttp实现缓存。 这是我已经完成的工作:

private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {

        @Override public Response intercept(Interceptor.Chain chain) throws IOException {
            Request originalRequest = chain.request();
            Request.Builder request = originalRequest.newBuilder();
            Response response = chain.proceed(request.build());
            return response.newBuilder()
                    .removeHeader("Pragma")
                    .removeHeader("Cache-Control")
                    .header("Cache-Control", "max-age=2419200")
                    .build();
        }
    };

    @Provides
    @Singleton
    OkHttpClient provideHttpClient(Context context) {


        File httpCacheDirectory = new File(context.getCacheDir(), "responses");
        int cacheSize = 10 * 1024 * 1024; // 10 MiB
        Cache cache = new Cache(httpCacheDirectory, cacheSize);


        HttpLoggingInterceptor oggingInterceptor = new HttpLoggingInterceptor();
        oggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return new OkHttpClient.Builder()
                .cache(cache)
                .connectTimeout(3, TimeUnit.SECONDS)
                .writeTimeout(3, TimeUnit.SECONDS)
                .readTimeout(3, TimeUnit.SECONDS)
                .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
                .addInterceptor(oggingInterceptor).build();
    }

然后将这个拦截器添加到HTTP客户端:

.addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)

我的响应目录中有文件,但是当我尝试在没有Internet连接的情况下加载内容时,出现以下错误:

无法解析主机“ example.com”:没有与主机名关联的地址

我只想将http响应存储在某个地方,如果没有互联网连接就将其加载。

如果存在Internet连接,我想重写缓存。

我和你遇到了同样的问题,现在我找到了一篇文章并解决了我的问题,我认为这可以为你提供帮助。

由于超出了获取缓存的时间,因此okhttp将清空缓存并再次请求服务器,现在缓存为空,因此它将发送错误Exception。

我们可以与您的拦截器一起设置一些请求设置。

CacheControl.Builder cacheBuilder = new CacheControl.Builder();
            cacheBuilder.maxAge(0, TimeUnit.SECONDS);
            cacheBuilder.maxStale(365,TimeUnit.DAYS);
            CacheControl cacheControl = cacheBuilder.build();

            Request request = chain.request();
            if(!StateUtils.isNetworkAvailable(MyApp.mContext)){
                request = request.newBuilder()
                        .cacheControl(cacheControl)
                        .build();
            }

maxAge:控制高速缓存生存期的最大值

maxStale:控制缓存过期时间

完整代码:

weiBoApiRetrofit() {

        File httpCacheDirectory = new File(MyApp.mContext.getCacheDir(), "responses");
        int cacheSize = 10 * 1024 * 1024; // 10 MiB
        Cache cache = new Cache(httpCacheDirectory, cacheSize);

        OkHttpClient client = new OkHttpClient.Builder()
                .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
                .cache(cache).build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        WeiBoApiService = retrofit.create(WeiBoApi.class);
    }

    //cache
    Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {

            CacheControl.Builder cacheBuilder = new CacheControl.Builder();
            cacheBuilder.maxAge(0, TimeUnit.SECONDS);
            cacheBuilder.maxStale(365,TimeUnit.DAYS);
            CacheControl cacheControl = cacheBuilder.build();

            Request request = chain.request();
            if(StateUtils.isNetworkAvailable(MyApp.mContext)){
                request = request.newBuilder()
                        .cacheControl(cacheControl)
                        .build();
            }
            Response originalResponse = chain.proceed(request);
            if (StateUtils.isNetworkAvailable(MyApp.mContext)) {
                int maxAge = 60  * 60; // read from cache
                return originalResponse.newBuilder()
                        .header("Cache-Control", "public, max-age=" + maxAge)
                        .build();
            } else {
                int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
                return originalResponse.newBuilder()
                        .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                        .build();
            }
        }
    };
}

我可以解决我的问题。

但是我不知道为什么在拦截器响应中设置标头没用,有人说“拦截器必须设置设置这两个请求和响应”。

希望能为您服务。

以这种方式设置标头(恰好50000)解决了该问题。

.header("Cache-Control", String.format("max-age=%d", 50000))

暂无
暂无

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

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