【问题标题】:How can I set a custom KeyGenerator for Spring Cache?如何为 Spring Cache 设置自定义 KeyGenerator?
【发布时间】:2011-10-07 12:31:17
【问题描述】:

我正在使用 Spring 3.1,我想使用新的缓存功能。然后,我尝试了:

<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" />

但我没有找到配置自定义 KeyGenerator 的方法。有什么想法吗?

【问题讨论】:

    标签: java spring caching ehcache


    【解决方案1】:

    好的,我只是想办法做到这一点......

    <!-- <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" />
    

    如您所见,我使用 AnnotationDrivenCacheBeanDefinitionParser,将配置放入我的 xml 中,它可以工作 :) 完成!

    编辑:

    对于 Spring > 3.2,您可以使用实现 CachingConfigurer 的简单 Java 类配置:

    @EnableCaching(mode = AdviceMode.ASPECTJ)
    public class CacheConfig implements CachingConfigurer {
    
        public KeyGenerator keyGenerator() {
            return new ReflectionBasedKeyGenerator();
        }
    
        public CacheManager cacheManager() {
            return new RedisCacheManager(redisCacheTemplate);
        }
    }
    

    【讨论】:

    • 此解决方案不适用于 spring 3.2.3。有人可以更新答案吗?
    【解决方案2】:

    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) {
    }}
    

    从今天开始,只需从下载 spring 时获得的 jar 文件中删除 org.springframework.context.support-3.1.0.RC1.jar\org\springframework\cache\config\spring-cache-3.1.xsd而且效果很好。

    【讨论】:

    • 如果使用了这个配置和实现,并且某个方法有一个注解,指定了一个使用SpEL的自定义键,那么会使用哪个?我的猜测是不会使用配置的自定义键(除非实现 MyKeyGenerator 以通过 Method 参数读取注释参数并将其考虑在内)。
    • Spring 3.2.8 成功了。不需要我删除任何文件
    【解决方案3】:

    我遇到了 Spring Frameworks 默认 Cache KeyGenerator 的问题。好像经常遇到冲突,好像有记录在这个issue

    我知道这个问题已经被标记为已回答,但我想我会分享我是如何解决这个问题的......

    <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" />
    

    基本上,我们创建并使用了自己的 Cache KeyGenerator 来代替默认的。

    【讨论】:

      猜你喜欢
      • 2019-05-21
      • 1970-01-01
      • 2020-05-09
      • 2012-02-17
      • 2019-12-28
      • 2020-09-07
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      相关资源
      最近更新 更多