mirror of
https://github.com/apache/httpcomponents-client.git
synced 2025-02-16 15:07:27 +00:00
HTTPCLIENT-1384: Expose CacheInvalidator interface
Contributed by Nicolas Richeton <nicolas.richeton at free.fr> git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1515814 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
196da4249d
commit
0101c2f949
@ -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>
|
||||
|
||||
|
58
httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheInvalidator.java
vendored
Normal file
58
httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheInvalidator.java
vendored
Normal file
@ -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);
|
||||
|
||||
}
|
@ -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,40 @@ class BasicHttpCache implements HttpCache {
|
||||
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 @@ boolean isIncompleteResponse(final HttpResponse resp, final Resource resource) {
|
||||
if (hdr == null) {
|
||||
return false;
|
||||
}
|
||||
int contentLength;
|
||||
final int contentLength;
|
||||
try {
|
||||
contentLength = Integer.parseInt(hdr.getValue());
|
||||
} catch (final NumberFormatException nfe) {
|
||||
|
@ -37,9 +37,10 @@
|
||||
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 @@
|
||||
*
|
||||
* @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;
|
||||
|
@ -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;
|
||||
@ -46,6 +47,7 @@ public class CachingHttpClientBuilder extends HttpClientBuilder {
|
||||
private File cacheDir;
|
||||
private CacheConfig cacheConfig;
|
||||
private SchedulingStrategy schedulingStrategy;
|
||||
private HttpCacheInvalidator httpCacheInvalidator;
|
||||
|
||||
public static CachingHttpClientBuilder create() {
|
||||
return new CachingHttpClientBuilder();
|
||||
@ -85,6 +87,12 @@ public final CachingHttpClientBuilder setSchedulingStrategy(
|
||||
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 @@ protected ClientExecChain decorateMainExec(final ClientExecChain mainExec) {
|
||||
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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user