Avoid fetching the cached entity twice on cache hit.

Closes PR #79
https://github.com/apache/httpcomponents-client/pull/79
This commit is contained in:
Leandro Nunes 2017-05-19 12:28:20 +01:00 committed by Oleg Kalnichevski
parent e07fd9abed
commit 45b23c7e0a
3 changed files with 26 additions and 2 deletions

View File

@ -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);
}

View File

@ -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(

View File

@ -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();
}
}