Skip to content

Commit 6bc14cc

Browse files
committed
Add cache-related logs
Add a trace log for cache hit and cache miss events. Issue: SPR-11654
1 parent 6f3570a commit 6bc14cc

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,11 @@ private Cache.ValueWrapper findCachedItem(Collection<CacheOperationContext> cont
435435
if (cached != null) {
436436
return cached;
437437
}
438+
else {
439+
if (logger.isTraceEnabled()) {
440+
logger.trace("No cache entry for key '" + key + "' in cache(s) " + context.getCacheNames());
441+
}
442+
}
438443
}
439444
}
440445
return null;
@@ -462,6 +467,9 @@ private Cache.ValueWrapper findInCaches(CacheOperationContext context, Object ke
462467
for (Cache cache : context.getCaches()) {
463468
Cache.ValueWrapper wrapper = doGet(cache, key);
464469
if (wrapper != null) {
470+
if (logger.isTraceEnabled()) {
471+
logger.trace("Cache entry for key '" + key + "' found in cache '" + cache.getName() + "'");
472+
}
465473
return wrapper;
466474
}
467475
}
@@ -484,7 +492,7 @@ private Object generateKey(CacheOperationContext context, Object result) {
484492
"using named params on classes without debug info?) " + context.metadata.operation);
485493
}
486494
if (logger.isTraceEnabled()) {
487-
logger.trace("Computed cache key " + key + " for operation " + context.metadata.operation);
495+
logger.trace("Computed cache key '" + key + "' for operation " + context.metadata.operation);
488496
}
489497
return key;
490498
}
@@ -548,13 +556,16 @@ protected class CacheOperationContext implements CacheOperationInvocationContext
548556

549557
private final Collection<? extends Cache> caches;
550558

559+
private final Collection<String> cacheNames;
560+
551561
private final AnnotatedElementKey methodCacheKey;
552562

553563
public CacheOperationContext(CacheOperationMetadata metadata, Object[] args, Object target) {
554564
this.metadata = metadata;
555565
this.args = extractArgs(metadata.method, args);
556566
this.target = target;
557567
this.caches = CacheAspectSupport.this.getCaches(this, metadata.cacheResolver);
568+
this.cacheNames = createCacheNames(this.caches);
558569
this.methodCacheKey = new AnnotatedElementKey(metadata.method, metadata.targetClass);
559570
}
560571

@@ -633,6 +644,18 @@ private EvaluationContext createEvaluationContext(Object result) {
633644
protected Collection<? extends Cache> getCaches() {
634645
return this.caches;
635646
}
647+
648+
protected Collection<String> getCacheNames() {
649+
return this.cacheNames;
650+
}
651+
652+
private Collection<String> createCacheNames(Collection<? extends Cache> caches) {
653+
Collection<String> names = new ArrayList<String>();
654+
for (Cache cache : caches) {
655+
names.add(cache.getName());
656+
}
657+
return names;
658+
}
636659
}
637660

638661

0 commit comments

Comments
 (0)