From a3a0e5165cf86838a1a534ce03f00b06ff7e7d6f Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Mon, 28 Mar 2011 12:10:26 +0000 Subject: [PATCH] SPR-8007 + add more logging --- .../cache/interceptor/CacheAspectSupport.java | 40 ++++++++++++++++++- .../cache/interceptor/CacheInterceptor.java | 1 - 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java index 25e5a3f383..211f11d23d 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java @@ -154,6 +154,8 @@ public abstract class CacheAspectSupport implements InitializingBean { targetClass = target.getClass(); } + boolean log = logger.isTraceEnabled(); + final CacheDefinition cacheDef = getCacheDefinitionSource().getCacheDefinition(method, targetClass); Object retVal = null; @@ -168,8 +170,14 @@ public abstract class CacheAspectSupport implements InitializingBean { if (cacheDef instanceof CacheUpdateDefinition) { Object key = context.generateKey(); + if (log) { + logger.trace("Computed cache key " + key + " for definition " + cacheDef); + } + if (key == null) { - throw new IllegalArgumentException("Null key returned for cache definition " + cacheDef); + throw new IllegalArgumentException( + "Null key returned for cache definition (maybe you are using named params on classes without debug info?) " + + cacheDef); } // @@ -185,8 +193,16 @@ public abstract class CacheAspectSupport implements InitializingBean { retVal = cache.get(key); // to avoid race-conditions of entries being removed between contains/get calls if (cache.containsKey(key)) { + if (log) { + logger.trace("Key " + key + " found in cache, returning value " + retVal); + } return retVal; } else { + if (log) { + logger.trace("Key " + key + " NOT found in cache, invoking target method for caching " + + method); + } + retVal = invocation.call(); cache.put(key, retVal); } @@ -225,8 +241,17 @@ public abstract class CacheAspectSupport implements InitializingBean { } if (!cacheHit) { + if (log) { + logger.trace("Key " + key + " NOT found in cache(s), invoking cached target method " + + method); + } retVal = invocation.call(); } + else { + if (log) { + logger.trace("Key " + key + " found in cache, returning value " + retVal); + } + } // update all caches (if needed) for (Cache cache : caches) { @@ -247,11 +272,19 @@ public abstract class CacheAspectSupport implements InitializingBean { // flush the cache (ignore arguments) if (invalidateDef.isCacheWide()) { cache.clear(); + if (log) { + logger.trace("Invalidating entire cache for definition " + cacheDef + " on method " + + method); + } } else { // check key if (key == null) { key = context.generateKey(); } + if (log) { + logger.trace("Invalidating cache key " + key + " for definition " + cacheDef + + " on method " + method); + } cache.remove(key); } } @@ -259,6 +292,11 @@ public abstract class CacheAspectSupport implements InitializingBean { return retVal; } + else { + if (log) { + logger.trace("Cache condition failed on method " + method + " for definition " + cacheDef); + } + } } return invocation.call(); diff --git a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheInterceptor.java b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheInterceptor.java index 4e9d23cc6b..4fc5b6bcb0 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheInterceptor.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheInterceptor.java @@ -40,7 +40,6 @@ import org.aopalliance.intercept.MethodInvocation; @SuppressWarnings("serial") public class CacheInterceptor extends CacheAspectSupport implements MethodInterceptor, Serializable { - @SuppressWarnings("unchecked") public Object invoke(final MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod();