我使用redis作为我的缓存层,在这方面,我在不同的服务和层中有很多地方使用@Cacheable。
这里是示例代码:
@Cacheable(value = "my_detail", key = "'m_detail_'+#entityIdd")
public InstituteDetail getMyDetail(Long entityId) {
call1();
call2()
}
@Cacheable(value = "call_1", key = "'call_1'+#entityIdd")
public InstituteDetail getMyDetail1(Long entityId) {
//some code
}
@Cacheable(value = "call_2", key = "'call_2'+#entityIdd")
public InstituteDetail getMyDetail2(Long entityId) {
//some code
}
应用程序和缓存工作正常,但是当我使用以下命令获取 redis-cli 中的所有 key 时:
keys *
它仅显示 call_1 和 call_2 的按键。 并且不显示“my_detail”的键。
这很奇怪并且没有得到解决。任何建议这里有什么问题。
最佳答案
生成一个代理类,它会拦截所有请求并使用缓存值进行响应,只有通过代理传入的外部方法调用才会被拦截。这意味着在目标对象内调用同一目标对象的另一个方法,即使调用的方法标记有 @Cacheable
,也不会在运行时导致实际的缓存拦截。
因此,如果该方法调用同一类的其他方法,您无法将值“my_detail”存储在缓存中。
关于java - Redis key 命令未显示所有 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58691721/