Avoid fetching the cached entity twice on cache hit.
This commit is contained in:
Leandro Nunes 2017-05-18 15:55:42 +01:00 committed by Oleg Kalnichevski
parent 10ab37b775
commit dac57c57f6
3 changed files with 26 additions and 2 deletions

View File

@ -263,10 +263,9 @@ public CloseableHttpResponse execute(
requestCompliance.makeRequestCompliant(request); requestCompliance.makeRequestCompliant(request);
request.addHeader("Via",via); request.addHeader("Via",via);
flushEntriesInvalidatedByRequest(context.getTargetHost(), 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(context.getTargetHost(), request);
return callBackend(route, request, context, execAware); return callBackend(route, request, context, execAware);
} }

View File

@ -402,6 +402,26 @@ public void testCallBackendMakesBackEndRequestAndHandlesResponse() throws Except
verifyMocks(); verifyMocks();
} }
@Test
public void testDoesNotFlushCachesOnCacheHit() throws Exception {
requestPolicyAllowsCaching(true);
requestIsFatallyNonCompliant(null);
getCacheEntryReturns(mockCacheEntry);
doesNotFlushCache();
cacheEntrySuitable(true);
cacheEntryValidatable(true);
expect(mockResponseGenerator.generateResponse(isA(HttpRequestWrapper.class), isA(HttpCacheEntry.class)))
.andReturn(mockBackendResponse);
replayMocks();
final HttpResponse result = impl.execute(route, request, context);
verifyMocks();
Assert.assertSame(mockBackendResponse, result);
}
private IExpectationSetters<CloseableHttpResponse> implExpectsAnyRequestAndReturn( private IExpectationSetters<CloseableHttpResponse> implExpectsAnyRequestAndReturn(
final CloseableHttpResponse response) throws Exception { final CloseableHttpResponse response) throws Exception {
final CloseableHttpResponse resp = impl.callBackend( final CloseableHttpResponse resp = impl.callBackend(

View File

@ -1777,4 +1777,9 @@ protected void responseIsGeneratedFromCache() {
.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();
}
} }