public final class HttpResponseCache extends ResponseCache
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() |
CacheResponse |
get(URI uri,
String requestMethod,
Map<String,List<String>> requestHeaders) |
File |
getDirectory() |
int |
getHitCount() |
long |
getMaxSize() |
int |
getNetworkCount() |
int |
getRequestCount() |
long |
getSize() |
int |
getWriteAbortCount() |
int |
getWriteSuccessCount() |
boolean |
isClosed() |
CacheRequest |
put(URI uri,
URLConnection urlConnection) |
getDefault, setDefault
public HttpResponseCache(File directory, long maxSize) throws IOException
IOException
public CacheResponse get(URI uri, String requestMethod, Map<String,List<String>> requestHeaders)
get
in class ResponseCache
public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException
put
in class ResponseCache
IOException
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 int getNetworkCount()
public int getHitCount()
public int getRequestCount()
Copyright © 2014. All rights reserved.