在什么位置我可以使用@cacheable在 Spring 启动与redis缓存

ct2axkht  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(358)

在什么位置我可以使用@cacheable-in-spring-boot with redis-cache,我可以用任何方法吗?

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缓存,有人能给我建议问题或任何解决方案吗?

ipakzgxi

ipakzgxi1#

不,不能将它放在同一服务的私有方法上,因为spring不处理对同一类的私有方法的调用。您应该将finduser或findbyuserid移到其他服务。

相关问题