问题
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