问题描述
我可以在春季启动时使用@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移至其他服务。