Class HttpResponseCache

java.lang.Object
java.net.ResponseCache
com.squareup.okhttp.HttpResponseCache

public final class HttpResponseCache
extends ResponseCache
Caches HTTP and HTTPS responses to the filesystem so they may be reused, saving time and bandwidth.

Cache Optimization

To measure cache effectiveness, this class tracks three statistics:
  • Request Count: the number of HTTP requests issued since this cache was created.
  • Network Count: the number of those requests that required network use.
  • Hit Count: the number of those requests whose responses were served by the cache.
Sometimes a request will result in a conditional cache hit. If the cache contains a stale copy of the response, the client will issue a conditional GET. The server will then send either the updated response if it has changed, or a short 'not modified' response if the client's copy is still valid. Such responses increment both the network count and hit count.

The best way to improve the cache hit rate is by configuring the web server to return cacheable responses. Although this client honors all HTTP/1.1 (RFC 2068) cache headers, it doesn't cache partial responses.

Force a Network Response

In some situations, such as after a user clicks a 'refresh' button, it may be necessary to skip the cache, and fetch data directly from the server. To force a full refresh, add the no-cache directive:
   
         connection.addRequestProperty("Cache-Control", "no-cache");
 
If it is only necessary to force a cached response to be validated by the server, use the more efficient max-age=0 instead:
   
         connection.addRequestProperty("Cache-Control", "max-age=0");
 

Force a Cache Response

Sometimes you'll want to show resources if they are available immediately, but not otherwise. This can be used so your application can show something while waiting for the latest data to be downloaded. To restrict a request to locally-cached resources, add the only-if-cached directive:
   
     try {
         connection.addRequestProperty("Cache-Control", "only-if-cached");
         InputStream cached = connection.getInputStream();
         // the resource was cached! show it
     } catch (FileNotFoundException e) {
         // the resource was not cached
     }
 
This technique works even better in situations where a stale response is better than no response. To permit stale cached responses, use the max-stale directive with the maximum staleness in seconds:
   
         int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
         connection.addRequestProperty("Cache-Control", "max-stale=" + maxStale);
 
  • Constructor Details

  • Method Details

    • get

      public CacheResponse get​(URI uri, String requestMethod, Map<String,​List<String>> requestHeaders)
      Description copied from class: ResponseCache
      Returns the cached response corresponding to the given request.
      Specified by:
      get in class ResponseCache
      Parameters:
      uri - the request URI.
      requestMethod - the request method.
      requestHeaders - a map of request headers.
      Returns:
      the CacheResponse object if the request is available in the cache or null otherwise.
    • put

      public CacheRequest put​(URI uri, URLConnection urlConnection) throws IOException
      Description copied from class: ResponseCache
      Allows the protocol handler to cache data after retrieving resources. The ResponseCache decides whether the resource data should be cached or not. If so, this method returns a CacheRequest to write the resource data to. Otherwise, this method returns null.
      Specified by:
      put in class ResponseCache
      Parameters:
      uri - the reference to the requested resource.
      urlConnection - the connection to fetch the response.
      Returns:
      a CacheRequest object with a WriteableByteChannel if the resource has to be cached, null otherwise.
      Throws:
      IOException - if an I/O error occurs while adding the resource.
    • delete

      public void delete() throws IOException
      Closes the cache and deletes all of its stored values. This will delete all files in the cache directory including files that weren't created by the cache.
      Throws:
      IOException
    • getWriteAbortCount

      public int getWriteAbortCount()
    • getWriteSuccessCount

      public int getWriteSuccessCount()
    • getSize

      public long getSize()
    • getMaxSize

      public long getMaxSize()
    • flush

      public void flush() throws IOException
      Throws:
      IOException
    • close

      public void close() throws IOException
      Throws:
      IOException
    • getDirectory

      public File getDirectory()
    • isClosed

      public boolean isClosed()
    • getNetworkCount

      public int getNetworkCount()
    • getHitCount

      public int getHitCount()
    • getRequestCount

      public int getRequestCount()