Ehcache with Spring Cache assigns wrong key

爷,独闯天下 提交于 2019-12-11 05:54:19

问题


I've got a method in UserService:

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

And it caches. But then I try to get all keys from 'user' cache:

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

Output:

com.cache.domain.User#1

Instead, for example, 'John Doe'.


回答1:


See the Javadoc of getKeys

Returns a list of all elements in the cache, whether or not they are expired.

That's actually returning the elements, no the ids. You may want to change your code to cast o to Element and output getObjectKey() instead.

You don't need to specify the key attribute. Since what you want is to use the single argument of your method (name) the cache abstraction will use that by default.



来源:https://stackoverflow.com/questions/38581039/ehcache-with-spring-cache-assigns-wrong-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!