From 6bc14cc7ae2b13a69bb5578689b0c71ab56a106d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 7 Apr 2015 14:27:43 +0200 Subject: [PATCH] Add cache-related logs Add a trace log for cache hit and cache miss events. Issue: SPR-11654 --- .../cache/interceptor/CacheAspectSupport.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java index 77250c940c..ba07727422 100644 --- a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java +++ b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java @@ -435,6 +435,11 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker if (cached != null) { return cached; } + else { + if (logger.isTraceEnabled()) { + logger.trace("No cache entry for key '" + key + "' in cache(s) " + context.getCacheNames()); + } + } } } return null; @@ -462,6 +467,9 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker for (Cache cache : context.getCaches()) { Cache.ValueWrapper wrapper = doGet(cache, key); if (wrapper != null) { + if (logger.isTraceEnabled()) { + logger.trace("Cache entry for key '" + key + "' found in cache '" + cache.getName() + "'"); + } return wrapper; } } @@ -484,7 +492,7 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker "using named params on classes without debug info?) " + context.metadata.operation); } if (logger.isTraceEnabled()) { - logger.trace("Computed cache key " + key + " for operation " + context.metadata.operation); + logger.trace("Computed cache key '" + key + "' for operation " + context.metadata.operation); } return key; } @@ -548,6 +556,8 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker private final Collection caches; + private final Collection cacheNames; + private final AnnotatedElementKey methodCacheKey; public CacheOperationContext(CacheOperationMetadata metadata, Object[] args, Object target) { @@ -555,6 +565,7 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker this.args = extractArgs(metadata.method, args); this.target = target; this.caches = CacheAspectSupport.this.getCaches(this, metadata.cacheResolver); + this.cacheNames = createCacheNames(this.caches); this.methodCacheKey = new AnnotatedElementKey(metadata.method, metadata.targetClass); } @@ -633,6 +644,18 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker protected Collection getCaches() { return this.caches; } + + protected Collection getCacheNames() { + return this.cacheNames; + } + + private Collection createCacheNames(Collection caches) { + Collection names = new ArrayList(); + for (Cache cache : caches) { + names.add(cache.getName()); + } + return names; + } }