0

In my android application I am caching responses from the server using OkHttp. for that I have implemented code as follows

     private class CacheInterceptor implements Interceptor {

        Context mContext;

        public CacheInterceptor(Context context) {
            this.mContext = context;
        }

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
                request = request.newBuilder()
                        .header(HEADER_MOBILE_OS, Constants.MOBILE_OS)
                        .header(HEADER_APP_VERSION, BuildConfig.VERSION_NAME)
                        .build();
            Response response = chain.proceed(request);

            if (!mShouldUpdateCache) {
                response.newBuilder()
                        .header("Cache-Control", String.format("max-age=%d", CACHE_MAX_AGE)).build();
            } else {
//update cache in this case
                response.newBuilder()
                        .header("Cache-Control", "no-cache").build();
                mShouldUpdateCache = false;
            }
            return response;
        }
    }

this is my interceptor class and I am setting this to OkClient as follows

okHttpClient.networkInterceptors().add(new CacheInterceptor(context));

    File httpCacheDirectory = new File(context.getCacheDir(), "response_cache");
    Cache cache = new Cache(httpCacheDirectory, CACHE_SIZE);

    if (cache != null) {
        okHttpClient.setCache(cache);
    }

but the problem is, when the boolean mShouldUpdateCache becomes true I have to update the cache. right now I have wrote response.newBuilder().header("Cache-Control", "no-cache").build(); but it is neither updating the cache nor fetching from server , how do I resolve this issue ?

droidev
  • 7,352
  • 11
  • 62
  • 94

1 Answers1

1

I suspect your network interceptor doesn't execute for responses served from the cache, because the network is not used for those requests. From the interceptors doc, network interceptors are “Not invoked for cached responses that short-circuit the network.”

Hacking the response headers on the client is a fragile way to do HTTP caching. You should use appropriate request headers and get your webserver go set appropriate response headers.

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
  • I didn't understood actually, can you explain with an example ? – droidev Nov 18 '15 at 13:50
  • @VividVervet 1) Your `Interceptor` is probably not being called because it is added to the network interceptors (which aren't called when the network is not used for request). 2) You really shouldn't be fiddling with HTTP caching at the application layer. Fix your webserver. – Eric Cochran Nov 19 '15 at 01:17
  • @NightlyNexus interceptor is calling and caching works perfectly, but I cannot update the cache manually – droidev Nov 19 '15 at 03:52
  • @VividVervet What do you want to update? Do you want to clear the `Cache`? – Eric Cochran Nov 19 '15 at 04:19
  • clearing cache is the last option, I have to replace the cache with network response, see in my code there is a flag,mShouldUpdateCache, is it is true then I have to either remove or update cache entries – droidev Nov 19 '15 at 04:34
  • If you want to skip the cache, you can use an application interceptor. – Jesse Wilson Nov 19 '15 at 19:31