* Disambiguated the contract of HttpCache#updatEntry / HttpCacheUpdateCallback
Contributed by Jonathan Moore <jonathan_moore at comcast.com> * Fixed method synchronization in BasicHttpCache git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@963849 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6aad9ccf99
commit
76e37db58a
|
@ -37,7 +37,7 @@ public interface HttpCache<E> {
|
|||
|
||||
void removeEntry(String url) throws HttpCacheOperationException;
|
||||
|
||||
void updateCacheEntry(
|
||||
void updateEntry(
|
||||
String url, HttpCacheUpdateCallback<E> callback) throws HttpCacheOperationException;
|
||||
|
||||
}
|
||||
|
|
|
@ -41,6 +41,6 @@ public interface HttpCacheUpdateCallback<E> {
|
|||
*
|
||||
* @since 4.1
|
||||
*/
|
||||
E getUpdatedEntry(E existing) throws HttpCacheOperationException;
|
||||
E update(E existing) throws HttpCacheOperationException;
|
||||
|
||||
}
|
|
@ -26,7 +26,6 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -43,8 +42,8 @@ import org.apache.http.client.cache.HttpCacheUpdateCallback;
|
|||
@ThreadSafe
|
||||
public class BasicHttpCache implements HttpCache<CacheEntry> {
|
||||
|
||||
private final LinkedHashMap<String, CacheEntry> baseMap = new LinkedHashMap<String, CacheEntry>(20,
|
||||
0.75f, true) {
|
||||
private final LinkedHashMap<String, CacheEntry> baseMap = new LinkedHashMap<String, CacheEntry>(
|
||||
20, 0.75f, true) {
|
||||
|
||||
private static final long serialVersionUID = -7750025207539768511L;
|
||||
|
||||
|
@ -52,15 +51,13 @@ public class BasicHttpCache implements HttpCache<CacheEntry> {
|
|||
protected boolean removeEldestEntry(Map.Entry<String, CacheEntry> eldest) {
|
||||
return size() > maxEntries;
|
||||
}
|
||||
};
|
||||
|
||||
private final Map<String, CacheEntry> syncMap;
|
||||
};
|
||||
|
||||
private final int maxEntries;
|
||||
|
||||
public BasicHttpCache(int maxEntries) {
|
||||
this.maxEntries = maxEntries;
|
||||
syncMap = Collections.synchronizedMap(baseMap);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,8 +68,8 @@ public class BasicHttpCache implements HttpCache<CacheEntry> {
|
|||
* @param entry
|
||||
* CacheEntry to place in the cache
|
||||
*/
|
||||
public void putEntry(String url, CacheEntry entry) {
|
||||
syncMap.put(url, entry);
|
||||
public synchronized void putEntry(String url, CacheEntry entry) {
|
||||
baseMap.put(url, entry);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -82,8 +79,8 @@ public class BasicHttpCache implements HttpCache<CacheEntry> {
|
|||
* Url that is the cache key
|
||||
* @return CacheEntry if one exists, or null for cache miss
|
||||
*/
|
||||
public CacheEntry getEntry(String url) {
|
||||
return syncMap.get(url);
|
||||
public synchronized CacheEntry getEntry(String url) {
|
||||
return baseMap.get(url);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -92,15 +89,15 @@ public class BasicHttpCache implements HttpCache<CacheEntry> {
|
|||
* @param url
|
||||
* Url that is the cache key
|
||||
*/
|
||||
public void removeEntry(String url) {
|
||||
syncMap.remove(url);
|
||||
public synchronized void removeEntry(String url) {
|
||||
baseMap.remove(url);
|
||||
}
|
||||
|
||||
public synchronized void updateCacheEntry(
|
||||
public synchronized void updateEntry(
|
||||
String url,
|
||||
HttpCacheUpdateCallback<CacheEntry> callback) throws HttpCacheOperationException {
|
||||
CacheEntry existingEntry = syncMap.get(url);
|
||||
CacheEntry updated = callback.getUpdatedEntry(existingEntry);
|
||||
syncMap.put(url, updated);
|
||||
CacheEntry existingEntry = baseMap.get(url);
|
||||
baseMap.put(url, callback.update(existingEntry));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -488,15 +488,7 @@ public class CachingHttpClient implements HttpClient {
|
|||
|
||||
protected void storeInCache(HttpHost target, HttpRequest request, CacheEntry entry) {
|
||||
if (entry.hasVariants()) {
|
||||
try {
|
||||
String uri = uriExtractor.getURI(target, request);
|
||||
HttpCacheUpdateCallback<CacheEntry> callback = storeVariantEntry(
|
||||
target, request, entry);
|
||||
responseCache.updateCacheEntry(uri, callback);
|
||||
} catch (HttpCacheOperationException ex) {
|
||||
log.debug("Was unable to PUT/UPDATE an entry into the cache based on the uri provided",
|
||||
ex);
|
||||
}
|
||||
storeVariantEntry(target, request, entry);
|
||||
} else {
|
||||
storeNonVariantEntry(target, request, entry);
|
||||
}
|
||||
|
@ -511,28 +503,35 @@ public class CachingHttpClient implements HttpClient {
|
|||
}
|
||||
}
|
||||
|
||||
protected HttpCacheUpdateCallback<CacheEntry> storeVariantEntry(
|
||||
protected void storeVariantEntry(
|
||||
final HttpHost target,
|
||||
final HttpRequest req,
|
||||
final CacheEntry entry) {
|
||||
final String variantURI = uriExtractor.getVariantURI(target, req, entry);
|
||||
try {
|
||||
responseCache.putEntry(variantURI, entry);
|
||||
} catch (HttpCacheOperationException e) {
|
||||
log.debug("Was unable to PUT a variant entry into the cache based on the uri provided", e);
|
||||
}
|
||||
|
||||
return new HttpCacheUpdateCallback<CacheEntry>() {
|
||||
public CacheEntry getUpdatedEntry(CacheEntry existing) throws HttpCacheOperationException {
|
||||
HttpCacheUpdateCallback<CacheEntry> callback = new HttpCacheUpdateCallback<CacheEntry>() {
|
||||
|
||||
return doGetUpdatedParentEntry(existing, target, req, entry);
|
||||
public CacheEntry update(CacheEntry existing) throws HttpCacheOperationException {
|
||||
return doGetUpdatedParentEntry(existing, entry, variantURI);
|
||||
}
|
||||
|
||||
};
|
||||
String parentURI = uriExtractor.getURI(target, req);
|
||||
try {
|
||||
responseCache.updateEntry(parentURI, callback);
|
||||
} catch (HttpCacheOperationException e) {
|
||||
log.debug("Was unable to UPDATE a parent entry for a variant", e);
|
||||
}
|
||||
}
|
||||
|
||||
protected CacheEntry doGetUpdatedParentEntry(
|
||||
CacheEntry existing,
|
||||
HttpHost target,
|
||||
HttpRequest req,
|
||||
CacheEntry entry) throws HttpCacheOperationException {
|
||||
|
||||
String variantURI = uriExtractor.getVariantURI(target, req, entry);
|
||||
responseCache.putEntry(variantURI, entry);
|
||||
|
||||
CacheEntry entry, String variantURI) throws HttpCacheOperationException {
|
||||
if (existing != null) {
|
||||
return existing.addVariantURI(variantURI);
|
||||
} else {
|
||||
|
|
|
@ -323,12 +323,9 @@ public class TestCachingHttpClient {
|
|||
final CacheEntry entry = new CacheEntry(new Date(), new Date(), HTTP_1_1,
|
||||
new Header[] {}, new ByteArrayEntity(new byte[] {}), 200, "OK");
|
||||
|
||||
extractVariantURI(variantURI, entry);
|
||||
putInCache(variantURI, entry);
|
||||
|
||||
replayMocks();
|
||||
|
||||
CacheEntry updatedEntry = impl.doGetUpdatedParentEntry(null, host, mockRequest, entry);
|
||||
CacheEntry updatedEntry = impl.doGetUpdatedParentEntry(null, entry, variantURI);
|
||||
|
||||
verifyMocks();
|
||||
|
||||
|
|
|
@ -147,9 +147,9 @@ public class TestResponseCache {
|
|||
cache.putEntry("foo", entry);
|
||||
cache.putEntry("bar", entry2);
|
||||
|
||||
cache.updateCacheEntry("foo", new HttpCacheUpdateCallback<CacheEntry>() {
|
||||
cache.updateEntry("foo", new HttpCacheUpdateCallback<CacheEntry>() {
|
||||
|
||||
public CacheEntry getUpdatedEntry(CacheEntry existing) {
|
||||
public CacheEntry update(CacheEntry existing) {
|
||||
CacheEntry updated = new CacheEntry(
|
||||
existing.getRequestDate(),
|
||||
existing.getRequestDate(),
|
||||
|
|
Loading…
Reference in New Issue