HTTPCLIENT-1460: ManagedHttpCacheStorage to keep valid cache entries upon close

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1567935 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2014-02-13 14:11:18 +00:00
parent 93ae3c741b
commit 80c8fb4dfc
2 changed files with 39 additions and 3 deletions

View File

@ -26,7 +26,9 @@
*/ */
package org.apache.http.impl.client.cache; package org.apache.http.impl.client.cache;
import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.IOException;
import org.apache.http.client.cache.HttpCacheInvalidator; import org.apache.http.client.cache.HttpCacheInvalidator;
import org.apache.http.client.cache.HttpCacheStorage; import org.apache.http.client.cache.HttpCacheStorage;
@ -48,6 +50,7 @@ public class CachingHttpClientBuilder extends HttpClientBuilder {
private CacheConfig cacheConfig; private CacheConfig cacheConfig;
private SchedulingStrategy schedulingStrategy; private SchedulingStrategy schedulingStrategy;
private HttpCacheInvalidator httpCacheInvalidator; private HttpCacheInvalidator httpCacheInvalidator;
private boolean deleteCache;
public static CachingHttpClientBuilder create() { public static CachingHttpClientBuilder create() {
return new CachingHttpClientBuilder(); return new CachingHttpClientBuilder();
@ -55,6 +58,7 @@ public class CachingHttpClientBuilder extends HttpClientBuilder {
protected CachingHttpClientBuilder() { protected CachingHttpClientBuilder() {
super(); super();
this.deleteCache = true;
} }
public final CachingHttpClientBuilder setResourceFactory( public final CachingHttpClientBuilder setResourceFactory(
@ -93,6 +97,11 @@ public class CachingHttpClientBuilder extends HttpClientBuilder {
return this; return this;
} }
public CachingHttpClientBuilder setDeleteCache(final boolean deleteCache) {
this.deleteCache = deleteCache;
return this;
}
@Override @Override
protected ClientExecChain decorateMainExec(final ClientExecChain mainExec) { protected ClientExecChain decorateMainExec(final ClientExecChain mainExec) {
final CacheConfig config = this.cacheConfig != null ? this.cacheConfig : CacheConfig.DEFAULT; final CacheConfig config = this.cacheConfig != null ? this.cacheConfig : CacheConfig.DEFAULT;
@ -110,7 +119,18 @@ public class CachingHttpClientBuilder extends HttpClientBuilder {
storage = new BasicHttpCacheStorage(config); storage = new BasicHttpCacheStorage(config);
} else { } else {
final ManagedHttpCacheStorage managedStorage = new ManagedHttpCacheStorage(config); final ManagedHttpCacheStorage managedStorage = new ManagedHttpCacheStorage(config);
addCloseable(managedStorage); if (this.deleteCache) {
addCloseable(new Closeable() {
@Override
public void close() throws IOException {
managedStorage.shutdown();
}
});
} else {
addCloseable(managedStorage);
}
storage = managedStorage; storage = managedStorage;
} }
} }

View File

@ -49,9 +49,17 @@ import org.apache.http.util.Args;
* call {@link #cleanResources()} method to trigger resource deallocation. The cache can be * call {@link #cleanResources()} method to trigger resource deallocation. The cache can be
* permanently shut down using {@link #shutdown()} method. All resources associated with * permanently shut down using {@link #shutdown()} method. All resources associated with
* the entries used by the cache will be deallocated. * the entries used by the cache will be deallocated.
* * <p/>
* This {@link HttpCacheStorage} implementation is intended for use with {@link FileResource} * This {@link HttpCacheStorage} implementation is intended for use with {@link FileResource}
* and similar. * and similar.
* <p/>
* Compatibility note. Prior to version 4.4 this storage implementation used to dispose of
* all resource entries upon {@link #close()}. As of version 4.4 the {@link #close()} method
* disposes only of those resources that have been explicitly removed from the cache with
* {@link #removeEntry(String)} method.
* <p/>
* The {@link #shutdown()} ()} method can still be used to shut down the storage and dispose of
* all resources currently managed by it.
* *
* @since 4.1 * @since 4.1
*/ */
@ -162,7 +170,15 @@ public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
@Override @Override
public void close() { public void close() {
shutdown(); if (this.active.compareAndSet(true, false)) {
synchronized (this) {
ResourceReference ref;
while ((ref = (ResourceReference) this.morque.poll()) != null) {
this.resources.remove(ref);
ref.getResource().dispose();
}
}
}
} }
} }