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
This commit is contained in:
Costin Leau 2011-06-02 10:01:14 +00:00
parent 8227cb6243
commit 7be1b5c3f5
3 changed files with 20 additions and 9 deletions

View File

@ -24,7 +24,7 @@ import java.lang.reflect.Method;
*
* @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 KeyGenerator<?> keyGenerator = new DefaultKeyGenerator();
private KeyGenerator keyGenerator = new DefaultKeyGenerator();
private volatile boolean initialized = false;
public void afterPropertiesSet() {
if (this.cacheManager == null) {
@ -78,6 +80,8 @@ public abstract class CacheAspectSupport implements InitializingBean {
throw new IllegalStateException("Either 'cacheDefinitionSource' or 'cacheDefinitionSources' is required: "
+ "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;
}
public <K> void setKeyGenerator(KeyGenerator<K> keyGenerator) {
public void setKeyGenerator(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 {
// 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
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(target);
if (targetClass == null && target != null) {
targetClass = target.getClass();
}
boolean log = logger.isTraceEnabled();
final CacheOperation cacheOp = getCacheDefinitionSource().getCacheOperation(method, targetClass);
Object retVal = null;
// analyze caching information
if (cacheOp != null) {
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
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,
Class<?> targetClass) {

View File

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