Index: src/main/java/org/apache/http/client/cache/HttpCacheInvalidator.java =================================================================== --- src/main/java/org/apache/http/client/cache/HttpCacheInvalidator.java (revision 0) +++ src/main/java/org/apache/http/client/cache/HttpCacheInvalidator.java (working copy) @@ -0,0 +1,32 @@ +package org.apache.http.client.cache; + +import org.apache.http.HttpHost; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; + +/** + * Given a particular HttpRequest, flush any cache entries that this request + * would invalidate. + * + * @since 4.3 + */ +public interface HttpCacheInvalidator { + + /** + * Remove cache entries from the cache that are no longer fresh or have been + * invalidated in some way. + * + * @param host + * The backend host we are talking to + * @param req + * The HttpRequest to that host + */ + public void flushInvalidatedCacheEntries(HttpHost host, HttpRequest req); + + /** + * Flushes entries that were invalidated by the given response received for + * the given host/request pair. + */ + public void flushInvalidatedCacheEntries(HttpHost host, HttpRequest request, final HttpResponse response); + +} Index: src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java =================================================================== --- src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java (revision 1513187) +++ src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java (working copy) @@ -44,6 +44,7 @@ import org.apache.http.HttpVersion; import org.apache.http.client.cache.HeaderConstants; import org.apache.http.client.cache.HttpCacheEntry; +import org.apache.http.client.cache.HttpCacheInvalidator; import org.apache.http.client.cache.HttpCacheStorage; import org.apache.http.client.cache.HttpCacheUpdateCallback; import org.apache.http.client.cache.HttpCacheUpdateException; @@ -65,19 +66,27 @@ private final long maxObjectSizeBytes; private final CacheEntryUpdater cacheEntryUpdater; private final CachedHttpResponseGenerator responseGenerator; - private final CacheInvalidator cacheInvalidator; + private final HttpCacheInvalidator cacheInvalidator; private final HttpCacheStorage storage; private final Log log = LogFactory.getLog(getClass()); - public BasicHttpCache(final ResourceFactory resourceFactory, final HttpCacheStorage storage, final CacheConfig config) { + public BasicHttpCache(final ResourceFactory resourceFactory, final HttpCacheStorage storage, final CacheConfig config, CacheKeyGenerator uriExtractor, HttpCacheInvalidator cacheInvalidator) { this.resourceFactory = resourceFactory; - this.uriExtractor = new CacheKeyGenerator(); + this.uriExtractor = uriExtractor; this.cacheEntryUpdater = new CacheEntryUpdater(resourceFactory); this.maxObjectSizeBytes = config.getMaxObjectSize(); this.responseGenerator = new CachedHttpResponseGenerator(); this.storage = storage; - this.cacheInvalidator = new CacheInvalidator(this.uriExtractor, this.storage); + this.cacheInvalidator = cacheInvalidator; + } + + public BasicHttpCache(final ResourceFactory resourceFactory, final HttpCacheStorage storage, final CacheConfig config, CacheKeyGenerator uriExtractor) { + this( resourceFactory, storage, config, uriExtractor, new CacheInvalidator(uriExtractor, storage)); + } + + public BasicHttpCache(final ResourceFactory resourceFactory, final HttpCacheStorage storage, final CacheConfig config) { + this( resourceFactory, storage, config, new CacheKeyGenerator()); } public BasicHttpCache(final CacheConfig config) { Index: src/main/java/org/apache/http/impl/client/cache/CacheInvalidator.java =================================================================== --- src/main/java/org/apache/http/impl/client/cache/CacheInvalidator.java (revision 1513187) +++ src/main/java/org/apache/http/impl/client/cache/CacheInvalidator.java (working copy) @@ -40,6 +40,7 @@ import org.apache.http.annotation.ThreadSafe; import org.apache.http.client.cache.HeaderConstants; import org.apache.http.client.cache.HttpCacheEntry; +import org.apache.http.client.cache.HttpCacheInvalidator; import org.apache.http.client.cache.HttpCacheStorage; import org.apache.http.client.utils.DateUtils; import org.apache.http.protocol.HTTP; @@ -51,7 +52,7 @@ * @since 4.1 */ @ThreadSafe // so long as the cache implementation is thread-safe -class CacheInvalidator { +class CacheInvalidator implements HttpCacheInvalidator { private final HttpCacheStorage storage; private final CacheKeyGenerator cacheKeyGenerator; Index: src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java =================================================================== --- src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java (revision 1513187) +++ src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java (working copy) @@ -28,6 +28,7 @@ import java.io.File; +import org.apache.http.client.cache.HttpCacheInvalidator; import org.apache.http.client.cache.HttpCacheStorage; import org.apache.http.client.cache.ResourceFactory; import org.apache.http.impl.client.HttpClientBuilder; @@ -43,6 +44,8 @@ private File cacheDir; private CacheConfig cacheConfig; private SchedulingStrategy schedulingStrategy; + private CacheKeyGenerator cacheKeyGenerator; + private HttpCacheInvalidator httpCacheInvalidator; public static CachingHttpClientBuilder create() { return new CachingHttpClientBuilder(); @@ -81,6 +84,16 @@ this.schedulingStrategy = schedulingStrategy; return this; } + + public final CachingHttpClientBuilder setHttpCacheInvalidator(HttpCacheInvalidator cacheInvalidator) { + this.httpCacheInvalidator = cacheInvalidator; + return this; + } + + public final CachingHttpClientBuilder setCacheKeyGenerator(CacheKeyGenerator cacheKeyGenerator) { + this.cacheKeyGenerator = cacheKeyGenerator; + return this; + } @Override protected ClientExecChain decorateMainExec(final ClientExecChain mainExec) { @@ -105,8 +118,19 @@ storage = new BasicHttpCacheStorage(cacheConfig); } final AsynchronousValidator revalidator = createAsynchronousRevalidator(config); + + CacheKeyGenerator uriExtractor = this.cacheKeyGenerator; + if (uriExtractor == null) { + uriExtractor = new CacheKeyGenerator(); + } + + HttpCacheInvalidator cacheInvalidator = this.httpCacheInvalidator; + if (cacheInvalidator == null) { + cacheInvalidator = new CacheInvalidator(uriExtractor, storage); + } + return new CachingExec(mainExec, - new BasicHttpCache(resourceFactory, storage, config), config, revalidator); + new BasicHttpCache(resourceFactory, storage, config, uriExtractor, cacheInvalidator), config, revalidator); } private AsynchronousValidator createAsynchronousRevalidator(final CacheConfig config) {