1
votes

I'm trying to implement caching using Retrofit and OkHttp. Here what I've already done:

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

And then I'm adding this interceptor to the HTTP client:

.addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)

I've got files inside my responses directory, however when I try to load content without internet connection, it gives me the following error:

Unable to resolve host "example.com": No address associated with hostname

I just want to store my http response somewhere and load it if there are no internet connection.

If there is an Internet connection, I want to rewrite cache.

2
I just don't know if I can do this way. There are not much info about caching mechanisms.nutella_eater

2 Answers

4
votes

I have meet the same problem with u , now i find a article and solve my problem , i think it's can help u.

Because out of the time of get cache , okhttp will empty cache and request server again , now cache was empty , so it will send error Exception.

We can set some request setting with you'r Interceptor.

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 : Controll the cache lifetime maximum

maxStale : Controll the cache Out of date time

Full code :

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

i can solve my problem.

but i didn't know why setting header in Response of Interceptor Useless , some people say " Interceptor must setting set those two request and Response "

I hope I can help you .

3
votes

Setting header this way (exactly 50000) solved the issue.

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