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 2017/05/24 18:24:00 UTC

[2/2] httpcomponents-client git commit: Avoid fetching the cached entity twice on cache hit.

Avoid fetching the cached entity twice on cache hit.

Closes PR #79


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/commit/ae2535ca
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/ae2535ca
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/ae2535ca

Branch: refs/heads/master
Commit: ae2535ca36661d9ed98d2986836a0002dfd9264e
Parents: e07fd9a
Author: Leandro Nunes <a-...@hotels.com>
Authored: Fri May 19 12:28:20 2017 +0100
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Wed May 24 20:23:15 2017 +0200

----------------------------------------------------------------------
 .../hc/client5/http/impl/cache/CachingExec.java |  3 +--
 .../http/impl/cache/TestCachingExec.java        | 20 ++++++++++++++++++++
 .../http/impl/cache/TestCachingExecChain.java   |  5 +++++
 3 files changed, 26 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/ae2535ca/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java
index 81b80f5..0507d85 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java
@@ -246,10 +246,9 @@ public class CachingExec implements ExecChainHandler {
         requestCompliance.makeRequestCompliant(request);
         request.addHeader("Via",via);
 
-        flushEntriesInvalidatedByRequest(target, request);
-
         if (!cacheableRequestPolicy.isServableFromCache(request)) {
             log.debug("Request is not servable from cache");
+            flushEntriesInvalidatedByRequest(target, request);
             return callBackend(target, request, scope, chain);
         }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/ae2535ca/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExec.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExec.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExec.java
index 727b338..281f859 100644
--- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExec.java
+++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExec.java
@@ -386,6 +386,26 @@ public class TestCachingExec extends TestCachingExecChain {
         verifyMocks();
     }
 
+    @Test
+    public void testDoesNotFlushCachesOnCacheHit() throws Exception {
+        requestPolicyAllowsCaching(true);
+        requestIsFatallyNonCompliant(null);
+
+        getCacheEntryReturns(mockCacheEntry);
+        doesNotFlushCache();
+        cacheEntrySuitable(true);
+        cacheEntryValidatable(true);
+
+        expect(mockResponseGenerator.generateResponse(isA(HttpRequest.class), isA(HttpCacheEntry.class)))
+                .andReturn(mockBackendResponse);
+
+        replayMocks();
+        final HttpResponse result = impl.execute(request, scope, mockExecChain);
+        verifyMocks();
+
+        Assert.assertSame(mockBackendResponse, result);
+    }
+
     private IExpectationSetters<ClassicHttpResponse> implExpectsAnyRequestAndReturn(
             final ClassicHttpResponse response) throws Exception {
         final ClassicHttpResponse resp = impl.callBackend(

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/ae2535ca/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
index 7a848cd..00d3747 100644
--- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
+++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
@@ -1673,4 +1673,9 @@ public abstract class TestCachingExecChain {
             .andReturn(mockCachedResponse);
     }
 
+    protected void doesNotFlushCache() throws IOException {
+        mockCache.flushInvalidatedCacheEntriesFor(isA(HttpHost.class), isA(HttpRequest.class));
+        EasyMock.expectLastCall().andThrow(new AssertionError("flushInvalidatedCacheEntriesFor should not have been called")).anyTimes();
+    }
+
 }