public final class HttpResponseCache extends ResponseCache implements OkResponseCache
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.
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");
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 and Description |
|---|
HttpResponseCache(File directory,
long maxSize) |
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
void |
delete()
Closes the cache and deletes all of its stored values.
|
void |
flush() |
com.squareup.okhttp.internal.http.Response |
get(com.squareup.okhttp.internal.http.Request request) |
CacheResponse |
get(URI uri,
String s,
Map<String,List<String>> stringListMap) |
File |
getDirectory() |
int |
getHitCount() |
long |
getMaxSize() |
int |
getNetworkCount() |
int |
getRequestCount() |
long |
getSize() |
int |
getWriteAbortCount() |
int |
getWriteSuccessCount() |
boolean |
isClosed() |
boolean |
maybeRemove(com.squareup.okhttp.internal.http.Request request)
Remove any cache entries for the supplied
uri. |
CacheRequest |
put(com.squareup.okhttp.internal.http.Response response) |
CacheRequest |
put(URI uri,
URLConnection urlConnection) |
void |
trackConditionalCacheHit()
Track an conditional GET that was satisfied by this cache.
|
void |
trackResponse(ResponseSource source)
Track an HTTP response being satisfied by
source. |
void |
update(com.squareup.okhttp.internal.http.Response cached,
com.squareup.okhttp.internal.http.Response network)
Handles a conditional request hit by updating the stored cache response
with the headers from
network. |
getDefault, setDefaultpublic HttpResponseCache(File directory, long maxSize) throws IOException
IOExceptionpublic CacheResponse get(URI uri, String s, Map<String,List<String>> stringListMap) throws IOException
get in class ResponseCacheIOExceptionpublic CacheRequest put(URI uri, URLConnection urlConnection) throws IOException
put in class ResponseCacheIOExceptionpublic com.squareup.okhttp.internal.http.Response get(com.squareup.okhttp.internal.http.Request request)
get in interface OkResponseCachepublic CacheRequest put(com.squareup.okhttp.internal.http.Response response) throws IOException
put in interface OkResponseCacheIOExceptionpublic boolean maybeRemove(com.squareup.okhttp.internal.http.Request request)
OkResponseCacheuri. Returns true if the
supplied requestMethod potentially invalidates an entry in the
cache.maybeRemove in interface OkResponseCachepublic void update(com.squareup.okhttp.internal.http.Response cached,
com.squareup.okhttp.internal.http.Response network)
OkResponseCachenetwork. The cached response body is not
updated. If the stored response has changed since cached was
returned, this does nothing.update in interface OkResponseCachepublic void delete()
throws IOException
IOExceptionpublic int getWriteAbortCount()
public int getWriteSuccessCount()
public long getSize()
public long getMaxSize()
public void flush()
throws IOException
IOExceptionpublic void close()
throws IOException
IOExceptionpublic File getDirectory()
public boolean isClosed()
public void trackResponse(ResponseSource source)
OkResponseCachesource.trackResponse in interface OkResponseCachepublic void trackConditionalCacheHit()
OkResponseCachetrackConditionalCacheHit in interface OkResponseCachepublic int getNetworkCount()
public int getHitCount()
public int getRequestCount()
Copyright © 2014. All Rights Reserved.