Skip to content

Commit 2c7acdd

Browse files
committed
add execute request method with cache key parameter
1 parent faaca72 commit 2c7acdd

File tree

3 files changed

+74
-29
lines changed

3 files changed

+74
-29
lines changed

src/main/java/com/wego/httpcache/services/AsyncHttpCacheService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ Optional<ListenableFuture<Response>> executeRequest(
1212

1313
Optional<ListenableFuture<Response>> executeRequest(
1414
Request request, AsyncCompletionHandlerBase handler, long ttl) throws Exception;
15+
16+
Optional<ListenableFuture<Response>> executeRequest(
17+
Request request, AsyncCompletionHandlerBase handler, long ttl, String cacheKey)
18+
throws Exception;
1519
}

src/main/java/com/wego/httpcache/services/impl/AsyncHttpCacheServiceImpl.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,33 @@ public AsyncHttpCacheServiceImpl(
3434
@Override
3535
public Optional<ListenableFuture<Response>> executeRequest(
3636
Request request, AsyncCompletionHandlerBase handler) throws Exception {
37-
return executeRequest(request, handler, ttl);
37+
return executeRequest(request, handler, this.ttl);
3838
}
3939

4040
@Override
4141
public Optional<ListenableFuture<Response>> executeRequest(
4242
Request request, AsyncCompletionHandlerBase handler, long ttl) throws Exception {
43+
return executeRequest(request, handler, ttl, null);
44+
}
45+
46+
@Override
47+
public Optional<ListenableFuture<Response>> executeRequest(
48+
Request request, AsyncCompletionHandlerBase handler, long ttl, String cacheKey)
49+
throws Exception {
4350

4451
ListenableFuture<Response> responseListenableFuture = null;
45-
String responseId = buildResponseId(request);
52+
if (cacheKey == null) {
53+
cacheKey = buildResponseId(request);
54+
}
4655

47-
Optional<CachedResponse> cachedResponse = cachedResponseService.findById(responseId);
56+
Optional<CachedResponse> cachedResponse = cachedResponseService.findById(cacheKey);
4857

4958
if (cachedResponse.isPresent()) {
5059
handler.onCompleted(cachedResponse.get());
5160
} else {
5261
responseListenableFuture =
5362
this.asyncHttpClient.executeRequest(
54-
request, buildCachingHandler(handler, responseId, ttl));
63+
request, buildCachingHandler(handler, cacheKey, ttl));
5564
}
5665

5766
return Optional.ofNullable(responseListenableFuture);

src/test/java/com/wego/httpcache/services/impl/TestAsyncHttpCacheServiceImpl.java

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,16 @@ public class TestAsyncHttpCacheServiceImpl {
4747
private static final String SERVICE_NAME = "Service Name";
4848

4949
private static long CACHING_TTL = 60;
50-
@Rule
51-
public WireMockRule wireMockRule = new WireMockRule(8089);
50+
@Rule public WireMockRule wireMockRule = new WireMockRule(8089);
5251

53-
@Spy
54-
private AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
52+
@Spy private AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
5553

5654
@InjectMocks
5755
private AsyncHttpCacheService asyncHttpCacheService =
5856
new AsyncHttpCacheServiceImpl(SERVICE_NAME, asyncHttpClient, CACHING_TTL);
5957

60-
@Mock
61-
private CachedResponseService cachedResponseService;
62-
@Mock
63-
private Request request;
58+
@Mock private CachedResponseService cachedResponseService;
59+
@Mock private Request request;
6460

6561
@Test
6662
public void executeRequest_whenWasCached_getResponseFromCacheAndCallOnComplete()
@@ -154,6 +150,44 @@ public void executeRequest_withTtl_whenWasNotCached_executeHttpRequestAndCacheNe
154150
assertThat(savedCachedResponses.get(0).getId()).isNotEmpty();
155151
}
156152

153+
@Test
154+
public void
155+
executeRequest_withTtlAndCacheKey_whenWasNotCached_executeHttpRequestAndCacheNewResponse()
156+
throws Exception {
157+
final Request request =
158+
new RequestBuilder().setMethod("GET").setUrl("http://localhost:8089/resources/").build();
159+
final List<CachedResponse> savedCachedResponses = Lists.newArrayList();
160+
final long customTtl = 10;
161+
162+
stubFor(
163+
get(urlEqualTo("/resources/"))
164+
.willReturn(
165+
aResponse()
166+
.withStatus(200)
167+
.withHeader("Content-Type", "text/json")
168+
.withBody("This is body")));
169+
170+
when(cachedResponseService.findById(anyString())).thenReturn(Optional.empty());
171+
when(cachedResponseService.save(any(), eq(customTtl)))
172+
.thenAnswer(
173+
invocation -> {
174+
CachedResponse cr = invocation.getArgumentAt(0, CachedResponse.class);
175+
savedCachedResponses.add(cr);
176+
return Optional.of(cr);
177+
});
178+
179+
Optional<ListenableFuture<Response>> responseListenableFuture =
180+
asyncHttpCacheService.executeRequest(
181+
request, new AsyncCompletionHandlerBase(), customTtl, "test");
182+
183+
responseListenableFuture.get().get();
184+
185+
verify(asyncHttpClient).executeRequest(any(), any());
186+
assertThat(savedCachedResponses.size()).isEqualTo(1);
187+
assertThat(savedCachedResponses.get(0).getId()).isEqualTo("test");
188+
assertThat(savedCachedResponses.get(0).getId()).isNotEmpty();
189+
}
190+
157191
@Test
158192
public void buildResponseId_returnsDifferentIdsForDifferentRequest() throws Exception {
159193

@@ -198,32 +232,30 @@ public void buildResponseId_returnsDifferentIdsForDifferentRequest() throws Exce
198232
"POST", "http://localhost:8089/resources/", "param", "test2");
199233

200234
Request requestWithBody =
201-
RequestFixture.createWithBody(
202-
"POST", "http://localhost:8089/resources/", "test1");
235+
RequestFixture.createWithBody("POST", "http://localhost:8089/resources/", "test1");
203236

204237
Request requestWithDifferentBody =
205-
RequestFixture.createWithBody(
206-
"POST", "http://localhost:8089/resources/", "test2");
238+
RequestFixture.createWithBody("POST", "http://localhost:8089/resources/", "test2");
207239

208240
Method method =
209241
AsyncHttpCacheServiceImpl.class.getDeclaredMethod("buildResponseId", Request.class);
210242
method.setAccessible(true);
211243

212244
final List<String> responseIds =
213245
Stream.of(
214-
request,
215-
requestWithDifferentUrl,
216-
requestWithDifferentMethod,
217-
requestWithParams,
218-
requestWithDifferentParams,
219-
requestWithCookie,
220-
requestWithDifferentCookie,
221-
requestWithHeader,
222-
requestWithDifferentHeader,
223-
requestWithQueryParams,
224-
requestWithDifferentQueryParams,
225-
requestWithBody,
226-
requestWithDifferentBody)
246+
request,
247+
requestWithDifferentUrl,
248+
requestWithDifferentMethod,
249+
requestWithParams,
250+
requestWithDifferentParams,
251+
requestWithCookie,
252+
requestWithDifferentCookie,
253+
requestWithHeader,
254+
requestWithDifferentHeader,
255+
requestWithQueryParams,
256+
requestWithDifferentQueryParams,
257+
requestWithBody,
258+
requestWithDifferentBody)
227259
.map(
228260
rq -> {
229261
try {

0 commit comments

Comments
 (0)