title: How to update OkHttp cache
body: <p>In my android application I am caching responses from the server using OkHttp. for that I have implemented code as follows</p>
  
  <pre><code>     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;
          }
      }
  </code></pre>
  
  <p>this is my interceptor class and I am setting this to OkClient as follows</p>
  
  <pre><code>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);
      }
  </code></pre>
  
  <p>but the problem is, when the boolean <code>mShouldUpdateCache</code> becomes true I have to update the cache. right now I have wrote <code>response.newBuilder().header("Cache-Control", "no-cache").build();</code> but it is neither updating the cache nor fetching from server , how do I resolve this issue ?</p>