public class Cache 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);
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, setDefault
public Cache(File directory, long maxSize) throws IOException
IOException
public CacheResponse get(URI uri, String s, Map<String,List<String>> stringListMap) throws IOException
get
in class ResponseCache
IOException
public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException
put
in class ResponseCache
IOException
public com.squareup.okhttp.internal.http.Response get(com.squareup.okhttp.internal.http.Request request)
get
in interface OkResponseCache
public CacheRequest put(com.squareup.okhttp.internal.http.Response response) throws IOException
put
in interface OkResponseCache
IOException
public boolean maybeRemove(com.squareup.okhttp.internal.http.Request request)
OkResponseCache
uri
. Returns true if the
supplied requestMethod
potentially invalidates an entry in the
cache.maybeRemove
in interface OkResponseCache
public void update(com.squareup.okhttp.internal.http.Response cached, com.squareup.okhttp.internal.http.Response network)
OkResponseCache
network
. The cached response body is not
updated. If the stored response has changed since cached
was
returned, this does nothing.update
in interface OkResponseCache
public void delete() throws IOException
IOException
public int getWriteAbortCount()
public int getWriteSuccessCount()
public long getSize()
public long getMaxSize()
public void flush() throws IOException
IOException
public void close() throws IOException
IOException
public File getDirectory()
public boolean isClosed()
public void trackResponse(ResponseSource source)
OkResponseCache
source
.trackResponse
in interface OkResponseCache
public void trackConditionalCacheHit()
OkResponseCache
trackConditionalCacheHit
in interface OkResponseCache
public int getNetworkCount()
public int getHitCount()
public int getRequestCount()
Copyright © 2014. All Rights Reserved.