revised cache abstraction

+ remove generic signature on key generator (as the type is not used anywhere)
+ add a small improvement to CacheAspect to nicely handle the cases where the aspect is pulled in but not configured

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4407 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Costin Leau 2011-06-02 10:01:14 +00:00
parent 569e6a4c37
commit 11b4525f5a
3 changed files with 20 additions and 9 deletions

View File

@ -24,7 +24,7 @@ import java.lang.reflect.Method;
* *
* @author Costin Leau * @author Costin Leau
*/ */
public interface KeyGenerator<K> { public interface KeyGenerator {
K extract(Object target, Method method, Object... params); Object extract(Object target, Method method, Object... params);
} }

View File

@ -68,7 +68,9 @@ public abstract class CacheAspectSupport implements InitializingBean {
private final ExpressionEvaluator evaluator = new ExpressionEvaluator(); private final ExpressionEvaluator evaluator = new ExpressionEvaluator();
private KeyGenerator<?> keyGenerator = new DefaultKeyGenerator(); private KeyGenerator keyGenerator = new DefaultKeyGenerator();
private volatile boolean initialized = false;
public void afterPropertiesSet() { public void afterPropertiesSet() {
if (this.cacheManager == null) { if (this.cacheManager == null) {
@ -78,6 +80,8 @@ public abstract class CacheAspectSupport implements InitializingBean {
throw new IllegalStateException("Either 'cacheDefinitionSource' or 'cacheDefinitionSources' is required: " throw new IllegalStateException("Either 'cacheDefinitionSource' or 'cacheDefinitionSources' is required: "
+ "If there are no cacheable methods, then don't use a cache aspect."); + "If there are no cacheable methods, then don't use a cache aspect.");
} }
initialized = true;
} }
/** /**
@ -110,7 +114,7 @@ public abstract class CacheAspectSupport implements InitializingBean {
return keyGenerator; return keyGenerator;
} }
public <K> void setKeyGenerator(KeyGenerator<K> keyGenerator) { public void setKeyGenerator(KeyGenerator keyGenerator) {
this.keyGenerator = keyGenerator; this.keyGenerator = keyGenerator;
} }
@ -146,19 +150,26 @@ public abstract class CacheAspectSupport implements InitializingBean {
} }
protected Object execute(Callable<Object> invocation, Object target, Method method, Object[] args) throws Exception { protected Object execute(Callable<Object> invocation, Object target, Method method, Object[] args) throws Exception {
// check whether aspect is enabled
// to cope with cases where the AJ is pulled in automatically
if (!initialized) {
return invocation.call();
}
boolean log = logger.isTraceEnabled();
// get backing class // get backing class
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(target); Class<?> targetClass = AopProxyUtils.ultimateTargetClass(target);
if (targetClass == null && target != null) { if (targetClass == null && target != null) {
targetClass = target.getClass(); targetClass = target.getClass();
} }
boolean log = logger.isTraceEnabled();
final CacheOperation cacheOp = getCacheDefinitionSource().getCacheOperation(method, targetClass); final CacheOperation cacheOp = getCacheDefinitionSource().getCacheOperation(method, targetClass);
Object retVal = null; Object retVal = null;
// analyze caching information // analyze caching information
if (cacheOp != null) { if (cacheOp != null) {
CacheOperationContext context = getOperationContext(cacheOp, method, args, target, targetClass); CacheOperationContext context = getOperationContext(cacheOp, method, args, target, targetClass);
@ -262,7 +273,7 @@ public abstract class CacheAspectSupport implements InitializingBean {
// context passed around to avoid multiple creations // context passed around to avoid multiple creations
private final EvaluationContext evalContext; private final EvaluationContext evalContext;
private final KeyGenerator<?> keyGenerator = CacheAspectSupport.this.keyGenerator; private final KeyGenerator keyGenerator = CacheAspectSupport.this.keyGenerator;
public CacheOperationContext(CacheOperation operation, Method method, Object[] args, Object target, public CacheOperationContext(CacheOperation operation, Method method, Object[] args, Object target,
Class<?> targetClass) { Class<?> targetClass) {

View File

@ -26,7 +26,7 @@ import org.springframework.cache.KeyGenerator;
* *
* @author Costin Leau * @author Costin Leau
*/ */
public class DefaultKeyGenerator implements KeyGenerator<Object> { public class DefaultKeyGenerator implements KeyGenerator {
public Object extract(Object target, Method method, Object... params) { public Object extract(Object target, Method method, Object... params) {
if (params.length == 1) { if (params.length == 1) {