13
13
import com .wego .httpcache .services .CachedResponseService ;
14
14
import java .util .Optional ;
15
15
import org .apache .commons .lang3 .StringUtils ;
16
+ import org .slf4j .Logger ;
17
+ import org .slf4j .LoggerFactory ;
16
18
import redis .clients .util .MurmurHash ;
17
19
18
20
public class AsyncHttpCacheServiceImpl implements AsyncHttpCacheService {
19
-
21
+ private static final Logger LOGGER = LoggerFactory . getLogger ( AsyncHttpCacheServiceImpl . class );
20
22
private static final String DELIMITER = ":" ;
21
23
@ Inject private CachedResponseService cachedResponseService ;
22
24
private String serviceName ;
@@ -49,13 +51,17 @@ public Optional<ListenableFuture<Response>> executeRequest(
49
51
throws Exception {
50
52
51
53
ListenableFuture <Response > responseListenableFuture = null ;
54
+
52
55
if (cacheKey == null ) {
53
56
cacheKey = buildResponseId (request );
54
57
}
55
58
56
59
Optional <CachedResponse > cachedResponse = cachedResponseService .findById (cacheKey );
57
60
61
+ LOGGER .info ("HTTP cache Provider: {} has ResponseID: {}" , serviceName , cacheKey );
62
+
58
63
if (cachedResponse .isPresent ()) {
64
+ LOGGER .info ("Found HTTP cache for Provider: {} with ResponseID: {}" , serviceName , cacheKey );
59
65
handler .onCompleted (cachedResponse .get ());
60
66
} else {
61
67
responseListenableFuture =
@@ -70,6 +76,7 @@ private String buildResponseId(Request request) {
70
76
String requestStringId =
71
77
StringUtils .join (
72
78
request , request .getStringData (), Lists .newArrayList (request .getCookies ()).toString ());
79
+
73
80
return StringUtils .joinWith (
74
81
DELIMITER , serviceName , String .valueOf (MurmurHash .hash64A (requestStringId .getBytes (), 0 )));
75
82
}
@@ -82,6 +89,7 @@ private AsyncCompletionHandlerBase buildCachingHandler(
82
89
public Response onCompleted (Response response ) throws Exception {
83
90
CachedResponse cachedResponse =
84
91
new CachedResponse .Builder (response ).setId (responseId ).build ();
92
+ LOGGER .info ("Save HTTP cache for provider {} with ResponseID {}" , serviceName , responseId );
85
93
cachedResponseService .save (cachedResponse , cachingTtl );
86
94
87
95
return handler .onCompleted (response );
0 commit comments