Package com.squareup.okhttp
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.
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 theno-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 Summary
Constructors Constructor Description HttpResponseCache(File directory, long maxSize) -
Method Summary
Modifier and Type Method Description voidclose()voiddelete()Closes the cache and deletes all of its stored values.voidflush()CacheResponseget(URI uri, String requestMethod, Map<String,List<String>> requestHeaders)Returns the cached response corresponding to the given request.FilegetDirectory()intgetHitCount()longgetMaxSize()intgetNetworkCount()intgetRequestCount()longgetSize()intgetWriteAbortCount()intgetWriteSuccessCount()booleanisClosed()CacheRequestput(URI uri, URLConnection urlConnection)Allows the protocol handler to cache data after retrieving resources.Methods inherited from class java.net.ResponseCache
getDefault, setDefault
-
Constructor Details
-
HttpResponseCache
- Throws:
IOException
-
-
Method Details
-
get
Description copied from class:ResponseCacheReturns the cached response corresponding to the given request.- Specified by:
getin classResponseCache- Parameters:
uri- the request URI.requestMethod- the request method.requestHeaders- a map of request headers.- Returns:
- the
CacheResponseobject if the request is available in the cache ornullotherwise.
-
put
Description copied from class:ResponseCacheAllows the protocol handler to cache data after retrieving resources. TheResponseCachedecides whether the resource data should be cached or not. If so, this method returns aCacheRequestto write the resource data to. Otherwise, this method returnsnull.- Specified by:
putin classResponseCache- 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,
nullotherwise. - Throws:
IOException- if an I/O error occurs while adding the resource.
-
delete
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
- Throws:
IOException
-
close
- Throws:
IOException
-
getDirectory
-
isClosed
public boolean isClosed() -
getNetworkCount
public int getNetworkCount() -
getHitCount
public int getHitCount() -
getRequestCount
public int getRequestCount()
-