How can I set a custom KeyGenerator for Spring Cache?

18,067

Solution 1

Ok, I just find a way to do this...

<!-- <cache:annotation-driven /> -->

<bean id="annotationCacheOperationSource"
    class="org.springframework.cache.annotation.AnnotationCacheOperationSource" />

<bean id="cacheInterceptor" class="org.springframework.cache.interceptor.CacheInterceptor"
    p:cacheDefinitionSources-ref="annotationCacheOperationSource"
    p:cacheManager-ref="cacheManager" p:keyGenerator-ref="keyGenerator" />

<bean id="beanFactoryCacheOperationSourceAdvisor"
    class="org.springframework.cache.interceptor.BeanFactoryCacheOperationSourceAdvisor"
    p:adviceBeanName="cacheInterceptor" p:cacheDefinitionSource-ref="annotationCacheOperationSource" />

<bean id="keyGenerator"
    class="my.company.cache.ReflectionBasedKeyGenerator" />

As you can see, I use the AnnotationDrivenCacheBeanDefinitionParser, I put the configuration in my xml, and it works :) Done!

edit:

For Spring > 3.2, you can use a simple Java class configuration implementing CachingConfigurer:

@EnableCaching(mode = AdviceMode.ASPECTJ)
public class CacheConfig implements CachingConfigurer {

    public KeyGenerator keyGenerator() {
        return new ReflectionBasedKeyGenerator();
    }

    public CacheManager cacheManager() {
        return new RedisCacheManager(redisCacheTemplate);
    }
}

Solution 2

There is a better way in Spring 3.1 RC1:

<cache:annotation-driven key-generator="myKeyGenerator"/>
<bean id="myKeyGenerator" class="com.abc.MyKeyGenerator" />

import org.springframework.cache.interceptor.KeyGenerator;
public class MyKeyGenerator implements KeyGenerator {

    public Object generate(Object target, Method method, Object... params) {
}}

As of today just delete the org.springframework.context.support-3.1.0.RC1.jar\org\springframework\cache\config\spring-cache-3.1.xsd from the jar file you get when you download spring and it works fine.

Share:
18,067
Vincent Devillers
Author by

Vincent Devillers

Updated on August 22, 2022

Comments

  • Vincent Devillers
    Vincent Devillers over 1 year

    I'm using Spring 3.1 and I want to use the new cache features. Then, I tried:

    <cache:annotation-driven />
    
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
        p:cache-manager-ref="ehcache" />
    
    <!-- Ehcache library setup -->
    <bean id="ehcache"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
        p:config-location="classpath:ehcache.xml" />
    

    But I didn't find the way to configure my custom KeyGenerator. Any idea?

  • Ulrik
    Ulrik over 11 years
    If this config and implementation is used, and a certain method has an annotation that specifies a custom key using SpEL, which will be used then? My guess is that the configured custom keys will not be used (unless MyKeyGenerator is implemented to read the annotation-parameter through the Method argument and take it into account).
  • Anuruddha
    Anuruddha almost 11 years
    This solution doesn't work with spring 3.2.3. Can someone update the answer.
  • vsingh
    vsingh about 5 years
    Spring 3.2.8 it worked. Did not required me to delete any files