java 使用 Spring KeyGenerator 生成唯一缓存键不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27574786/
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
Generating unique cache key with Spring KeyGenerator not working
提问by blacktide
I'm having the issue when my cache keys are colliding in Spring using the @Cacheable
annotation. For instance, with the following two methods:
当我的缓存键在 Spring 中使用@Cacheable
注释发生冲突时,我遇到了问题。例如,使用以下两种方法:
@Cacheable("doOneThing")
public void doOneThing(String name) {
// do something with name
}
@Cacheable("doAnotherThing")
public void doAnotherThing(String name) {
// do some other thing with name
}
Here is my cache configuration, in which I've added a keyGenerator
and a cacheManager
bean:
这是我的缓存配置,我在其中添加了一个keyGenerator
和一个cacheManager
bean:
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public JedisConnectionFactory redisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(cf);
return redisTemplate;
}
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
return new RedisCacheManager(redisTemplate);
}
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getName());
sb.append(method.getName());
for (Object param : params) {
sb.append(param.toString());
}
return sb.toString();
}
};
}
}
For some reason, the cache key always gets set to the name
parameter in the method, not the result of the keyGenerator.generate(..)
method, causing both methods to return the same cache result.
出于某种原因,缓存键总是被设置为name
方法中的参数,而不是方法的结果keyGenerator.generate(..)
,导致两种方法返回相同的缓存结果。
I know that I can specify the key manually on each @Cacheable
annotation, but that seems a little extensive for every method I'd like to cache.
我知道我可以在每个@Cacheable
注释上手动指定键,但是对于我想要缓存的每个方法来说这似乎有点广泛。
Edit
编辑
I've noticed that setting the keyGenerator
option inside of the @Cacheable
annotation to the name of my bean resolves the issue, like so:
我注意到keyGenerator
将@Cacheable
注释中的选项设置为我的 bean 的名称可以解决这个问题,如下所示:
@Cacheable(value = "doOneThing", keyGenerator = "keyGenerator")
public void doOneThing(String name) {
// do something with name
}
And setting the keyGenerator
option in the @CacheConfig
annotation on the class also resolves the issue. It seems that this shouldn't be necessary though. Am I missing something?
并且在类keyGenerator
的@CacheConfig
注释中设置选项也解决了这个问题。不过,这似乎没有必要。我错过了什么吗?
采纳答案by Stephane Nicoll
Your configuration should implement CachingConfigurer
(usually you would extend from CachingConfigurerSupport
) to customize how caching works.
您的配置应该实现CachingConfigurer
(通常您会从 扩展CachingConfigurerSupport
)以自定义缓存的工作方式。