【问题标题】:At what positions I can use @Cacheable in spring boot with redis cache在 Spring Boot 中我可以在哪些位置使用 @Cacheable 和 redis 缓存
【发布时间】:2021-02-21 09:01:08
【问题描述】:

在 Spring Boot 中我可以在哪些位置使用 @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 显然不是一个实体,所以为了摆脱它,我创建了两个返回域的方法,但 问题是它没有保存或使用 redis缓存谁能建议我这个问题或任何解决方案?

【问题讨论】:

    标签: spring-boot caching redis redis-cache


    【解决方案1】:

    不,你不能在同一个服务的私有方法上使用它,因为 Spring 不处理对同一个类的私有方法的调用。您应该将 findUser 或 findByUserId 移动到其他服务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      相关资源
      最近更新 更多