3

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?

16

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.

| improve this answer | |
  • 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). – Ulrik Oct 23 '12 at 8:12
  • Spring 3.2.8 it worked. Did not required me to delete any files – vsingh Mar 22 '19 at 16:20
5

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);
    }
}
| improve this answer | |
  • This solution doesn't work with spring 3.2.3. Can someone update the answer. – Anuruddha Jun 13 '13 at 6:53
0

I encountered a problem with Spring Frameworks default Cache KeyGenerator. It seems to often encounter conflicts and it appears to have been recorded on this issue

I know this question has already been marked as answered, but I thought I would share how I resolved this...

<cache:annotation-driven cache-manager="cacheManager" key-generator="entityKeyGenerator" />

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="ehcache" />
</bean>

<bean id="ehcache"
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
    p:configLocation="classpath:/ehcache-dciComponent.xml" p:shared="true" />

Basically, we created and used our own Cache KeyGenerator in place of the default one.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.