Search code examples
spring-bootcachingredisredis-cache

At what positions I can use @Cacheable in spring boot with redis cache


At what positions I can use @Cacheable in spring boot with redis cache, Can I use it with any method?

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;
}

I have used it with method findUser because findByUserID returns the DTO which is obviously not an entity, so to get rid of it I created two methods that returns domain, but problem is that it is not saving or use redis cache, can anybody suggest me the problem or any solution?


Solution

  • No, you can't have it on private method of same service, because Spring does not handle calls to private methods of same class. You should move findUser or findByUserId to other service.