diff --git a/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCache.java b/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCache.java index 81455f548..0cf22a119 100644 --- a/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCache.java +++ b/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCache.java @@ -37,7 +37,7 @@ public interface HttpCache { void removeEntry(String url) throws HttpCacheOperationException; - void updateCacheEntry( + void updateEntry( String url, HttpCacheUpdateCallback callback) throws HttpCacheOperationException; } diff --git a/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheUpdateCallback.java b/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheUpdateCallback.java index 4aa11741e..36a1de226 100644 --- a/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheUpdateCallback.java +++ b/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheUpdateCallback.java @@ -41,6 +41,6 @@ public interface HttpCacheUpdateCallback { * * @since 4.1 */ - E getUpdatedEntry(E existing) throws HttpCacheOperationException; + E update(E existing) throws HttpCacheOperationException; } \ No newline at end of file diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java index 4e1cf3805..78172f044 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java @@ -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 { - private final LinkedHashMap baseMap = new LinkedHashMap(20, - 0.75f, true) { + private final LinkedHashMap baseMap = new LinkedHashMap( + 20, 0.75f, true) { private static final long serialVersionUID = -7750025207539768511L; @@ -52,15 +51,13 @@ public class BasicHttpCache implements HttpCache { protected boolean removeEldestEntry(Map.Entry eldest) { return size() > maxEntries; } - }; - private final Map 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 { * @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 { * 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 { * @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 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)); } + } diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java index 7807210c8..3a283c9ef 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java @@ -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 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 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() { - public CacheEntry getUpdatedEntry(CacheEntry existing) throws HttpCacheOperationException { + HttpCacheUpdateCallback callback = new HttpCacheUpdateCallback() { - 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 { diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java index db93a44d1..6472327b5 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java @@ -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(); diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java index e5e5256ae..5bd79a95d 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java @@ -147,9 +147,9 @@ public class TestResponseCache { cache.putEntry("foo", entry); cache.putEntry("bar", entry2); - cache.updateCacheEntry("foo", new HttpCacheUpdateCallback() { + cache.updateEntry("foo", new HttpCacheUpdateCallback() { - public CacheEntry getUpdatedEntry(CacheEntry existing) { + public CacheEntry update(CacheEntry existing) { CacheEntry updated = new CacheEntry( existing.getRequestDate(), existing.getRequestDate(),