Package com.jcabi.http.wire
Class CachingWire
- java.lang.Object
-
- com.jcabi.http.wire.CachingWire
-
- All Implemented Interfaces:
Wire
@Immutable public final class CachingWire extends Object implements Wire
Wire that caches GET requests.This decorator can be used when you want to avoid duplicate GET requests to load-sensitive resources, for example:
String html = new JdkRequest("http://goggle.com") .through(CachingWire.class) .header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN) .fetch() .body();
Since 1.5, you can also configure it to flush the entire cache on certain request URI's, for example:
new JdkRequest(uri) .through(CachingWire.class, "GET /save/.*") .uri().path("/save/123").back() .fetch();
Since 1.17.3, you can pass a {@see LoadingCache} alongside the wire.
final LoadingCache<Callable<Response>, Response> cache = ...; new JdkRequest(uri) .through(CachingWire.class, cache) .uri().path("/save/123").back() .fetch();
The regular expression provided will be used against a string constructed as an HTTP method, space, path of the URI together with query part.
The class is immutable and thread-safe.
- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description CachingWire(Wire wire)
Public ctor.CachingWire(Wire wire, com.google.common.cache.LoadingCache<Callable<Response>,Response> storage)
Public ctor.CachingWire(Wire wire, String flsh)
Public ctor.CachingWire(Wire wire, String flsh, com.google.common.cache.LoadingCache<Callable<Response>,Response> storage)
Public ctor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
invalidate()
Invalidate the entire cache.Response
send(Request req, String home, String method, Collection<Map.Entry<String,String>> headers, InputStream content, int connect, int read)
Send request and return response.
-
-
-
Constructor Detail
-
CachingWire
public CachingWire(Wire wire)
Public ctor.- Parameters:
wire
- Original wire
-
CachingWire
public CachingWire(Wire wire, String flsh)
Public ctor.- Parameters:
wire
- Original wireflsh
- Flushing regular expression- Since:
- 1.5
-
CachingWire
public CachingWire(Wire wire, com.google.common.cache.LoadingCache<Callable<Response>,Response> storage)
Public ctor.- Parameters:
wire
- Original wirestorage
- Cache- Since:
- 1.17.4
-
-
Method Detail
-
send
public Response send(Request req, String home, String method, Collection<Map.Entry<String,String>> headers, InputStream content, int connect, int read) throws IOException
Description copied from interface:Wire
Send request and return response.- Specified by:
send
in interfaceWire
- Parameters:
req
- Requesthome
- URI to fetchmethod
- HTTP methodheaders
- Headerscontent
- HTTP bodyconnect
- The connect timeoutread
- The read timeout- Returns:
- Response obtained
- Throws:
IOException
- if fails
-
invalidate
public static void invalidate()
Invalidate the entire cache.- Since:
- 1.15
-
-