Skip to content

use Guava cache to return ListenableFuture #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.wego.httpcache.services.impl;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
Expand All @@ -11,18 +13,25 @@
import com.wego.httpcache.dao.models.CachedResponse;
import com.wego.httpcache.services.AsyncHttpCacheService;
import com.wego.httpcache.services.CachedResponseService;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;

import redis.clients.util.MurmurHash;

public class AsyncHttpCacheServiceImpl implements AsyncHttpCacheService {

private static final String DELIMITER = ":";
@Inject private CachedResponseService cachedResponseService;
private String serviceName;
private AsyncHttpClient asyncHttpClient;
private long ttl;

private final Cache<String, ListenableFuture<Response>> cache = CacheBuilder.newBuilder()
.expireAfterWrite(2, TimeUnit.MINUTES)
.build();

@Inject
public AsyncHttpCacheServiceImpl(
@Assisted String serviceName, @Assisted AsyncHttpClient asyncHttpClient, @Assisted long ttl) {
Expand All @@ -49,9 +58,11 @@ public Optional<ListenableFuture<Response>> executeRequest(
if (cachedResponse.isPresent()) {
handler.onCompleted(cachedResponse.get());
} else {
responseListenableFuture =
this.asyncHttpClient.executeRequest(
request, buildCachingHandler(handler, responseId, ttl));
responseListenableFuture = cache.get(responseId, () -> {
return this.asyncHttpClient.executeRequest(request,
buildCachingHandler(handler, responseId, ttl));
});
handler.onCompleted(responseListenableFuture.get());
}

return Optional.ofNullable(responseListenableFuture);
Expand All @@ -60,7 +71,8 @@ public Optional<ListenableFuture<Response>> executeRequest(
private String buildResponseId(Request request) {
String requestStringId =
StringUtils.join(
request, request.getStringData(), Lists.newArrayList(request.getCookies()).toString());
request, request.getStringData(), Lists.newArrayList(request.getCookies())
.toString());
return StringUtils.joinWith(
DELIMITER, serviceName, String.valueOf(MurmurHash.hash64A(requestStringId.getBytes(), 0)));
}
Expand All @@ -72,10 +84,11 @@ private AsyncCompletionHandlerBase buildCachingHandler(
@Override
public Response onCompleted(Response response) throws Exception {
CachedResponse cachedResponse =
new CachedResponse.Builder(response).setId(responseId).build();
new CachedResponse.Builder(response).setId(responseId)
.build();
cachedResponseService.save(cachedResponse, cachingTtl);

return handler.onCompleted(response);
return response;
}

@Override
Expand Down