带有Spring Cache的Ehcache分配了错误的密钥 [英] Ehcache with Spring Cache assigns wrong key

查看:134
本文介绍了带有Spring Cache的Ehcache分配了错误的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UserService中有一个方法:

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()));

输出:


com.cache.domain.User#1

com.cache.domain.User#1

例如, John Doe。

Instead, for example, 'John Doe'.

推荐答案

参见 getKeys


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

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

那实际上是在返回元素,没有id。您可能需要更改代码以将 o 转换为 Element 并输出 getObjectKey()

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.

您不需要指定 key 属性。由于您要使用的是方法的单个参数( name ),因此默认情况下,缓存抽象将使用该参数。

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.

这篇关于带有Spring Cache的Ehcache分配了错误的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆