Skip to content

Commit e48df2d

Browse files
authored
Merge pull request micronaut-projects#2545 from micronaut-projects/cache_interceptor_logging
Some trace logging for the cache interceptor
2 parents 8366dfa + 3b5b292 commit e48df2d

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

runtime/src/main/java/io/micronaut/cache/interceptor/CacheInterceptor.java

+32-2
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ protected Object interceptSync(MethodInvocationContext context, ReturnType retur
240240
}
241241

242242
/**
243-
* Intercept the aync method invocation.
243+
* Intercept the async method invocation.
244244
*
245245
* @param context Contains information about method invocation
246246
* @param returnTypeObject The return type of the method in Micronaut
@@ -273,7 +273,7 @@ protected Object interceptCompletableFuture(MethodInvocationContext<Object, Obje
273273
return;
274274
}
275275
}
276-
CompletableFuture<?> completableFuture = (CompletableFuture) context.proceed();
276+
CompletableFuture<?> completableFuture = (CompletableFuture<?>) context.proceed();
277277
if (completableFuture == null) {
278278
thisFuture.complete(null);
279279
} else {
@@ -290,8 +290,14 @@ protected Object interceptCompletableFuture(MethodInvocationContext<Object, Obje
290290
}
291291
};
292292
if (o1 != null) {
293+
if (LOG.isTraceEnabled()) {
294+
LOG.trace("Storing in the cache [{}] with key [{}] the result of invocation [{}]: {}", asyncCache.getName(), key, context, o1);
295+
}
293296
asyncCache.put(key, o1).whenComplete(completionHandler);
294297
} else {
298+
if (LOG.isTraceEnabled()) {
299+
LOG.trace("Invalidating the key [{}] of the cache [{}] since the result of invocation [{}] was null", key, asyncCache.getName(), context);
300+
}
295301
asyncCache.invalidate(key).whenComplete(completionHandler);
296302
}
297303

@@ -376,6 +382,9 @@ private Publisher<Object> buildCacheInvalidatePublisher(
376382
if (invalidateAll) {
377383
for (String cacheName : cacheNames) {
378384
AsyncCache<?> asyncCache = cacheManager.getCache(cacheName).async();
385+
if (LOG.isTraceEnabled()) {
386+
LOG.trace("Invalidating all the entries of the cache [{}]", asyncCache.getName());
387+
}
379388
asyncCache.invalidateAll().whenCompleteAsync((aBoolean, throwable) -> {
380389
if (throwable != null) {
381390
asyncCacheErrorHandler.handleInvalidateError(asyncCache, asRuntimeException(throwable));
@@ -389,6 +398,9 @@ private Publisher<Object> buildCacheInvalidatePublisher(
389398
Object key = keyGenerator.generateKey(context, parameterValues);
390399
for (String cacheName : cacheNames) {
391400
AsyncCache<?> asyncCache = cacheManager.getCache(cacheName).async();
401+
if (LOG.isTraceEnabled()) {
402+
LOG.trace("Invalidating the key [{}] of the cache [{}]", key, asyncCache.getName());
403+
}
392404
asyncCache.invalidate(key).whenCompleteAsync((aBoolean, throwable) -> {
393405
if (throwable != null) {
394406
asyncCacheErrorHandler.handleInvalidateError(asyncCache, asRuntimeException(throwable));
@@ -538,8 +550,14 @@ private Publisher<Object> buildCacheablePublisher(
538550
}
539551
};
540552
if (o != null) {
553+
if (LOG.isTraceEnabled()) {
554+
LOG.trace("Storing in the cache [{}] with key [{}] the result of invocation [{}]: {}", asyncCache.getName(), key, context, o);
555+
}
541556
asyncCache.put(key, o).whenComplete(completionHandler);
542557
} else {
558+
if (LOG.isTraceEnabled()) {
559+
LOG.trace("Invalidating the key [{}] of the cache [{}] since the result of invocation [{}] was null", key, asyncCache.getName(), context);
560+
}
543561
asyncCache.invalidate(key).whenComplete(completionHandler);
544562
}
545563
}).toFlowable();
@@ -787,6 +805,9 @@ private void processCacheEvict(
787805
if (async) {
788806
AsyncCache<?> asyncCache = syncCache.async();
789807
if (invalidateAll) {
808+
if (LOG.isTraceEnabled()) {
809+
LOG.trace("Invalidating all the entries of the cache [{}]", asyncCache.getName());
810+
}
790811
CompletableFuture<Boolean> future = asyncCache.invalidateAll();
791812
future.whenCompleteAsync((aBoolean, throwable) -> {
792813
if (throwable != null) {
@@ -795,6 +816,9 @@ private void processCacheEvict(
795816
}, ioExecutor);
796817
} else {
797818
Object finalKey = key;
819+
if (LOG.isTraceEnabled()) {
820+
LOG.trace("Invalidating the key [{}] of the cache [{}]", key, asyncCache.getName());
821+
}
798822
CompletableFuture<Boolean> future = asyncCache.invalidate(key);
799823
future.whenCompleteAsync((aBoolean, throwable) -> {
800824
if (throwable != null) {
@@ -805,6 +829,9 @@ private void processCacheEvict(
805829
} else {
806830
if (invalidateAll) {
807831
try {
832+
if (LOG.isTraceEnabled()) {
833+
LOG.trace("Invalidating all the entries of the cache [{}]", syncCache.getName());
834+
}
808835
syncCache.invalidateAll();
809836
} catch (RuntimeException e) {
810837
if (errorHandler.handleInvalidateError(syncCache, e)) {
@@ -813,6 +840,9 @@ private void processCacheEvict(
813840
}
814841
} else {
815842
try {
843+
if (LOG.isTraceEnabled()) {
844+
LOG.trace("Invalidating the key [{}] of the cache [{}]", key, syncCache.getName());
845+
}
816846
syncCache.invalidate(key);
817847
} catch (RuntimeException e) {
818848
if (errorHandler.handleInvalidateError(syncCache, key, e)) {

0 commit comments

Comments
 (0)