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:
parent
e07fd9abed
commit
45b23c7e0a
|
@ -246,10 +246,9 @@ public class CachingExec implements ExecChainHandler {
|
||||||
requestCompliance.makeRequestCompliant(request);
|
requestCompliance.makeRequestCompliant(request);
|
||||||
request.addHeader("Via",via);
|
request.addHeader("Via",via);
|
||||||
|
|
||||||
flushEntriesInvalidatedByRequest(target, request);
|
|
||||||
|
|
||||||
if (!cacheableRequestPolicy.isServableFromCache(request)) {
|
if (!cacheableRequestPolicy.isServableFromCache(request)) {
|
||||||
log.debug("Request is not servable from cache");
|
log.debug("Request is not servable from cache");
|
||||||
|
flushEntriesInvalidatedByRequest(target, request);
|
||||||
return callBackend(target, request, scope, chain);
|
return callBackend(target, request, scope, chain);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -386,6 +386,26 @@ public class TestCachingExec extends TestCachingExecChain {
|
||||||
verifyMocks();
|
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(
|
private IExpectationSetters<ClassicHttpResponse> implExpectsAnyRequestAndReturn(
|
||||||
final ClassicHttpResponse response) throws Exception {
|
final ClassicHttpResponse response) throws Exception {
|
||||||
final ClassicHttpResponse resp = impl.callBackend(
|
final ClassicHttpResponse resp = impl.callBackend(
|
||||||
|
|
|
@ -1673,4 +1673,9 @@ public abstract class TestCachingExecChain {
|
||||||
.andReturn(mockCachedResponse);
|
.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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue