Expand var-args before passing to KeyGenerator
Update `CacheAspectSupport` to expand any var-arg parameters before calling `KeyGenerator` implementations. Prior to this commit var-args would be passed to `KeyGenerator` implementations as a nested array, often causing the same key to be generated regardless of the arguments. Issue: SPR-10870
This commit is contained in:
@@ -25,7 +25,6 @@ import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.aop.framework.AopProxyUtils;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.cache.Cache;
|
||||
@@ -349,15 +348,27 @@ public abstract class CacheAspectSupport implements InitializingBean {
|
||||
|
||||
private final Collection<Cache> caches;
|
||||
|
||||
public CacheOperationContext(CacheOperation operation, Method method, Object[] args, Object target, Class<?> targetClass) {
|
||||
public CacheOperationContext(CacheOperation operation, Method method,
|
||||
Object[] args, Object target, Class<?> targetClass) {
|
||||
this.operation = operation;
|
||||
this.method = method;
|
||||
this.args = args;
|
||||
this.args = extractArgs(method, args);
|
||||
this.target = target;
|
||||
this.targetClass = targetClass;
|
||||
this.caches = CacheAspectSupport.this.getCaches(operation);
|
||||
}
|
||||
|
||||
private Object[] extractArgs(Method method, Object[] args) {
|
||||
if (!method.isVarArgs()) {
|
||||
return args;
|
||||
}
|
||||
Object[] varArgs = (Object[]) args[args.length - 1];
|
||||
Object[] combinedArgs = new Object[args.length - 1 + varArgs.length];
|
||||
System.arraycopy(args, 0, combinedArgs, 0, args.length - 1);
|
||||
System.arraycopy(varArgs, 0, combinedArgs, args.length - 1, varArgs.length);
|
||||
return combinedArgs;
|
||||
}
|
||||
|
||||
protected boolean isConditionPassing(Object result) {
|
||||
if (StringUtils.hasText(this.operation.getCondition())) {
|
||||
EvaluationContext evaluationContext = createEvaluationContext(result);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,10 +24,18 @@ import java.lang.reflect.Method;
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author Chris Beams
|
||||
* @author Phillip Webb
|
||||
* @since 3.1
|
||||
*/
|
||||
public interface KeyGenerator {
|
||||
|
||||
/**
|
||||
* Generate a key for the given method and its parameters.
|
||||
* @param target the target instance
|
||||
* @param method the method being called
|
||||
* @param params the method parameters (with any var-args expanded)
|
||||
* @return a generated key
|
||||
*/
|
||||
Object generate(Object target, Method method, Object... params);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user