Browse Source
This commit adds an extra parameter to the base @Cache method annotations: keyGenerator. This parameter holds the name of the KeyGenerator bean to use to compute the key for that specific caching endpoint. This gives therefore a third way to customize the key. These are: 1. Default KeyGenerator (global for all endpoints) 2. The 'key' attribute of the annotation, giving the SpEL expression to use 3. The 'keyGenerator' attribute of the annotation The annotation attributes are therefore exclusive. Trying to specify them both will result in an IllegalStateException. The KeyGenerator to use for a given operation is cached on startup so that multiple calls to it does not resolve the instance to use over and over again. Issue: SPR-10629pull/442/merge
25 changed files with 407 additions and 38 deletions
@ -0,0 +1,46 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2014 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.cache.config; |
||||||
|
|
||||||
|
import org.springframework.cache.interceptor.KeyGenerator; |
||||||
|
|
||||||
|
import java.lang.reflect.Method; |
||||||
|
|
||||||
|
/** |
||||||
|
* A custom {@link KeyGenerator} that exposes the algorithm used to compute the key |
||||||
|
* for convenience in tests scenario. |
||||||
|
* |
||||||
|
* @author Stephane Nicoll |
||||||
|
*/ |
||||||
|
final class SomeCustomKeyGenerator implements KeyGenerator { |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object generate(Object target, Method method, Object... params) { |
||||||
|
return generateKey(method.getName(), params); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @see #generate(Object, java.lang.reflect.Method, Object...) |
||||||
|
*/ |
||||||
|
static Object generateKey(String methodName, Object... params) { |
||||||
|
final StringBuilder sb = new StringBuilder(methodName); |
||||||
|
for (Object param : params) { |
||||||
|
sb.append(param); |
||||||
|
} |
||||||
|
return sb.toString(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,40 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2014 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.cache.config; |
||||||
|
|
||||||
|
import static org.junit.Assert.*; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.beans.factory.BeanDefinitionStoreException; |
||||||
|
import org.springframework.context.support.GenericXmlApplicationContext; |
||||||
|
|
||||||
|
/** |
||||||
|
* AOP advice specific parsing tests. |
||||||
|
* |
||||||
|
* @author Stephane Nicoll |
||||||
|
*/ |
||||||
|
public class CacheAdviceParserTests { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void keyAndKeyGeneratorCannotBeSetTogether() { |
||||||
|
try { |
||||||
|
new GenericXmlApplicationContext("/org/springframework/cache/config/cache-advice-invalid.xml"); |
||||||
|
fail("Should have failed to load context, one advise define both a key and a key generator"); |
||||||
|
} catch (BeanDefinitionStoreException e) { // TODO better exception handling
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,46 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2014 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.cache.config; |
||||||
|
|
||||||
|
import org.springframework.cache.interceptor.KeyGenerator; |
||||||
|
|
||||||
|
import java.lang.reflect.Method; |
||||||
|
|
||||||
|
/** |
||||||
|
* A custom {@link KeyGenerator} that exposes the algorithm used to compute the key |
||||||
|
* for convenience in tests scenario. |
||||||
|
* |
||||||
|
* @author Stephane Nicoll |
||||||
|
*/ |
||||||
|
final class SomeCustomKeyGenerator implements KeyGenerator { |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object generate(Object target, Method method, Object... params) { |
||||||
|
return generateKey(method.getName(), params); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @see #generate(Object, java.lang.reflect.Method, Object...) |
||||||
|
*/ |
||||||
|
static Object generateKey(String methodName, Object... params) { |
||||||
|
final StringBuilder sb = new StringBuilder(methodName); |
||||||
|
for (Object param : params) { |
||||||
|
sb.append(param); |
||||||
|
} |
||||||
|
return sb.toString(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,14 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xmlns:cache="http://www.springframework.org/schema/cache" |
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
||||||
|
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> |
||||||
|
|
||||||
|
<import resource="cache-advice.xml"/> |
||||||
|
|
||||||
|
<cache:advice id="cacheAdviceInvalid" cache-manager="cacheManager"> |
||||||
|
<cache:caching cache="default"> |
||||||
|
<cache:cacheable method="someFakeMethod" key="#root.methodName" key-generator="unknownBeanName"/> |
||||||
|
</cache:caching> |
||||||
|
</cache:advice> |
||||||
|
</beans> |
||||||
Loading…
Reference in new issue