You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2013/08/20 15:10:55 UTC
svn commit: r1515814 - in /httpcomponents/httpclient/trunk: ./
httpclient-cache/src/main/java/org/apache/http/client/cache/
httpclient-cache/src/main/java/org/apache/http/impl/client/cache/
Author: olegk
Date: Tue Aug 20 13:10:55 2013
New Revision: 1515814
URL: http://svn.apache.org/r1515814
Log:
HTTPCLIENT-1384: Expose CacheInvalidator interface
Contributed by Nicolas Richeton <nicolas.richeton at free.fr>
Added:
httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheInvalidator.java (with props)
Modified:
httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheInvalidator.java
httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java
Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=1515814&r1=1515813&r2=1515814&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Tue Aug 20 13:10:55 2013
@@ -2,6 +2,12 @@
Changes since release 4.3 BETA2
-------------------
+* [HTTPCLIENT-1384] Expose CacheInvalidator interface.
+ Contributed by Nicolas Richeton <nicolas.richeton at free.fr>
+
+* [HTTPCLIENT-1385] Fixed path normalization in CacheKeyGenerator
+ Contributed by James Leigh <james at 3roundstones dot com>
+
* [HTTPCLIENT-1385] Fixed path normalization in CacheKeyGenerator
Contributed by James Leigh <james at 3roundstones dot com>
Added: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheInvalidator.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheInvalidator.java?rev=1515814&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheInvalidator.java (added)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheInvalidator.java Tue Aug 20 13:10:55 2013
@@ -0,0 +1,58 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+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
+ */
+ void flushInvalidatedCacheEntries(HttpHost host, HttpRequest req);
+
+ /**
+ * Flushes entries that were invalidated by the given response received for
+ * the given host/request pair.
+ */
+ void flushInvalidatedCacheEntries(HttpHost host, HttpRequest request, HttpResponse response);
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheInvalidator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheInvalidator.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheInvalidator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java?rev=1515814&r1=1515813&r2=1515814&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java Tue Aug 20 13:10:55 2013
@@ -44,6 +44,7 @@ import org.apache.http.HttpStatus;
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,40 @@ class BasicHttpCache implements HttpCach
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,
+ final CacheKeyGenerator uriExtractor,
+ final 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,
+ final 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) {
@@ -175,7 +197,7 @@ class BasicHttpCache implements HttpCach
if (hdr == null) {
return false;
}
- int contentLength;
+ final int contentLength;
try {
contentLength = Integer.parseInt(hdr.getValue());
} catch (final NumberFormatException nfe) {
Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheInvalidator.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheInvalidator.java?rev=1515814&r1=1515813&r2=1515814&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheInvalidator.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheInvalidator.java Tue Aug 20 13:10:55 2013
@@ -37,9 +37,10 @@ import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
-import org.apache.http.annotation.ThreadSafe;
+import org.apache.http.annotation.Immutable;
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;
@@ -50,8 +51,8 @@ import org.apache.http.protocol.HTTP;
*
* @since 4.1
*/
-@ThreadSafe // so long as the cache implementation is thread-safe
-class CacheInvalidator {
+@Immutable
+class CacheInvalidator implements HttpCacheInvalidator {
private final HttpCacheStorage storage;
private final CacheKeyGenerator cacheKeyGenerator;
Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java?rev=1515814&r1=1515813&r2=1515814&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java Tue Aug 20 13:10:55 2013
@@ -28,6 +28,7 @@ package org.apache.http.impl.client.cach
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;
@@ -46,6 +47,7 @@ public class CachingHttpClientBuilder ex
private File cacheDir;
private CacheConfig cacheConfig;
private SchedulingStrategy schedulingStrategy;
+ private HttpCacheInvalidator httpCacheInvalidator;
public static CachingHttpClientBuilder create() {
return new CachingHttpClientBuilder();
@@ -85,6 +87,12 @@ public class CachingHttpClientBuilder ex
return this;
}
+ public final CachingHttpClientBuilder setHttpCacheInvalidator(
+ final HttpCacheInvalidator cacheInvalidator) {
+ this.httpCacheInvalidator = cacheInvalidator;
+ return this;
+ }
+
@Override
protected ClientExecChain decorateMainExec(final ClientExecChain mainExec) {
final CacheConfig config = this.cacheConfig != null ? this.cacheConfig : CacheConfig.DEFAULT;
@@ -105,17 +113,28 @@ public class CachingHttpClientBuilder ex
addCloseable(managedStorage);
storage = managedStorage;
}
- storage = new BasicHttpCacheStorage(cacheConfig);
}
final AsynchronousValidator revalidator = createAsynchronousRevalidator(config);
+ final CacheKeyGenerator 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) {
if (config.getAsynchronousWorkersMax() > 0) {
final SchedulingStrategy configuredSchedulingStrategy = createSchedulingStrategy(config);
- final AsynchronousValidator revalidator = new AsynchronousValidator(configuredSchedulingStrategy);
+ final AsynchronousValidator revalidator = new AsynchronousValidator(
+ configuredSchedulingStrategy);
addCloseable(revalidator);
return revalidator;
}