如何解决我可以在什么位置使用带有Redis缓存的Spring Boot中的@Cacheable
我可以在春季启动时使用@Cacheable和Redis缓存, 我可以用任何方法使用它吗?
public UserDTO findByUserID(Long userID) {
User user = findUser(userID);
if (user != null) {
Password password = findPassword(userID);
return userMapper.mapToDTO(user,password);
}
return null;
}
private Password findPassword(Long userID) {
Password password = passwordRepository.findPasswordBasedOnUserID(userID);
return password;
}
@Cacheable("users")
private User findUser(Long userID) {
User user = userRepository.findByUserID(userID);
return user;
}
我将其与findUser方法一起使用,因为findByUserID返回的DTO显然不是实体,因此为了摆脱它,我创建了两个返回domain的方法,但是问题是它没有保存或使用redis缓存,有人可以向我提出问题或任何解决方案吗?
解决方法
不,您不能在相同服务的私有方法上使用它,因为Spring不会处理对相同类的私有方法的调用。您应该将findUser或findByUserId移至其他服务。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。