title: Is there an easy way to manually force a caching HttpClient 4.3.x to bypass the cache?
body: <p>I am using <a href="https://hc.apache.org/httpcomponents-client-ga/httpclient-cache/apidocs/org/apache/http/impl/client/cache/CachingHttpClientBuilder.html" rel="nofollow"><code>CachingHttpClientBuilder</code></a> to create a <a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/CloseableHttpClient.html?is-external=true" rel="nofollow"><code>CloseableHttpClient</code></a> in by an <code>HttpServlet</code> to fetch internal resources. In most cases I want it to use its cache, and that seems to be working fine. However, periodically the client is used to check for updates to a special remote file (basically it has configuration data). In that case, I would like to be able to tell the client to fetch the resource via HTTP even if the cache contains it and it isn't marked as stale yet.</p>
  
  <p>When I've run into situations like this in JavaScript I have often appended a bogus query string parameter with a timestamp in it so that the URL would not match a cached entry. However, I would think there is a better solution in this case since we have direct, programmatic access to the HTTP client.</p>
  
  <p>Lest someone suggest just changing the remote server so that it sets a <code>Cache-Control: no-cache</code> header to prevent this, please understand that this is outside of my control. The goal is to bypass the cache in this instance regardless of whether or not the remote server says it can/should be cached.</p>
  
  <p>Someone might also suggest not using a caching <code>HttpClient</code> in this case, which would be OK, but it seems less than ideal to me because then I would need to make a second <code>HttpClient</code> (other portions of this application require caching of these HTTP resources in order to perform sufficiently well).</p>
  
  <p><strong>EDIT</strong>: user3360944 suggested using a <a href="http://hc.apache.org/httpcomponents-client-ga/httpclient-cache/apidocs/org/apache/http/client/cache/HttpCacheInvalidator.html" rel="nofollow"><code>HttpCacheInvalidator</code></a>, which may be the right solution, but I am not sure yet how to do this. Could someone give an example of what to put in the <code>flushInvalidatedCacheEntries</code> method to remove a cached entry for a given URL? (I have particular URLs that I never want to cache)</p>
  
  <pre><code>new HttpCacheInvalidator() {
      @Override
      public void flushInvalidatedCacheEntries(HttpHost host, HttpRequest req) {
          // What do I need to do here to invalidate a cached entry for
          // say, http://www.example.com/path/file.txt?
      }
  
      @Override
      public void flushInvalidatedCacheEntries(HttpHost host, HttpRequest request, HttpResponse response) {        
          // Do nothing here since I don't need to invalidate anything
          // based on the response received
      }
  }
  </code></pre>