java 如何为 Spring Cache 设置自定义 KeyGenerator?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6730641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set a custom KeyGenerator for Spring Cache?
提问by Vincent Devillers
I'm using Spring 3.1 and I want to use the new cache features. Then, I tried:
我正在使用 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" />
But I didn't find the way to configure my custom KeyGenerator. Any idea?
但是我没有找到配置我的自定义 KeyGenerator 的方法。任何的想法?
回答by abc
There is a better way in Spring 3.1 RC1:
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.
截至今天,只需从下载 spring 时获得的 jar 文件中删除 org.springframework.context.support-3.1.0.RC1.jar\org\springframework\cache\config\spring-cache-3.1.xsd 并且它可以工作美好的。
回答by Vincent Devillers
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!
如您所见,我使用了 AnnotationDrivenCacheBeanDefinitionParser,我将配置放在了我的 xml 中,它可以工作:) 完成了!
edit:
编辑:
For Spring > 3.2, you can use a simple Java class configuration implementing CachingConfigurer:
对于 Spring > 3.2,您可以使用一个简单的 Java 类配置来实现 CachingConfigurer:
@EnableCaching(mode = AdviceMode.ASPECTJ)
public class CacheConfig implements CachingConfigurer {
public KeyGenerator keyGenerator() {
return new ReflectionBasedKeyGenerator();
}
public CacheManager cacheManager() {
return new RedisCacheManager(redisCacheTemplate);
}
}
回答by Brett VanderVeen
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
我遇到了 Spring Frameworks 默认缓存 KeyGenerator 的问题。好像经常遇到冲突,好像已经记录在这个问题上
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.
基本上,我们创建并使用了我们自己的 Cache KeyGenerator 来代替默认的。