带有Spring Cache的Ehcache分配了错误的密钥

时间:2016-07-26 04:30:46

标签: spring caching ehcache spring-cache

我在UserService中有一个方法:

@Cacheable(value="user", key="#p0")
public User find(String name) {
    return userRepository.findOneByName(name);
}

它缓存了。但后来我尝试从“用户”中获取所有密钥。缓存:

CacheManager cacheManager = CacheManager.getInstance();
cacheManager.getCache("user").getKeys().forEach(o -> log.debug(o.toString()));

输出:

  

com.cache.domain.User#1

相反,例如,' John Doe'。

1 个答案:

答案 0 :(得分:0)

请参阅getKeys

的Javadoc
  

返回缓存中所有元素的列表,无论它们是否已过期。

实际上是返回元素,没有id。您可能希望更改代码以将o投射到Element并输出getObjectKey()

您无需指定key属性。因为你想要的是使用你的方法的单个参数(name),默认情况下缓存抽象将使用它。