Custom KeyGenerator

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-10629
This commit is contained in:
Stephane Nicoll
2014-01-10 16:24:51 +01:00
parent 906321dcdd
commit 81c208098f
25 changed files with 407 additions and 38 deletions

View File

@@ -1,11 +1,9 @@
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="apc" class="org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator"/>
@@ -44,4 +42,6 @@
<bean id="keyGenerator" class="org.springframework.cache.config.SomeKeyGenerator"/>
<bean id="customKeyGenerator" class="org.springframework.cache.config.SomeCustomKeyGenerator"/>
</beans>

View File

@@ -30,5 +30,7 @@
<bean id="classService" class="org.springframework.cache.config.AnnotatedClassCacheableService"/>
<bean id="keyGenerator" class="org.springframework.cache.config.SomeKeyGenerator"/>
<bean id="customKeyGenerator" class="org.springframework.cache.config.SomeCustomKeyGenerator"/>
</beans>

View File

@@ -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>

View File

@@ -16,6 +16,8 @@
<cache:cacheable method="varArgsKey"/>
<cache:cacheable method="nam*" key="#root.methodName"/>
<cache:cacheable method="rootVars" key="#root.methodName + #root.method.name + #root.targetClass + #root.target"/>
<cache:cacheable method="customKeyGenerator" key-generator="customKeyGenerator"/>
<cache:cacheable method="unknownCustomKeyGenerator" key-generator="unknownBeanName"/>
<cache:cacheable method="nullValue" cache="default"/>
</cache:caching>
<cache:caching>
@@ -107,6 +109,8 @@
<bean id="keyGenerator" class="org.springframework.cache.config.SomeKeyGenerator"/>
<bean id="customKeyGenerator" class="org.springframework.cache.config.SomeCustomKeyGenerator"/>
<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor"/>
<bean id="service" class="org.springframework.cache.config.DefaultCacheableService"/>