-1

I am trying to made spring boot app that is reading data from redis cache. But I can not.

Firstly I am calling post api to create user and then I am calling get api to getUserById but I am getting error like this:

java.lang.ClassNotFoundException: com.umut.redis.redisExample.Model.UserEntity at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) ~[na:na] at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na] at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na] at java.base/java.lang.Class.forName0(Native Method) ~[na:na] at java.base/java.lang.Class.forName(Class.java:398) ~[na:na] at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:145) ~[spring-boot-devtools-2.5.6.jar:2.5.6] at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na] at java.base/java.lang.Class.forName0(Native Method) ~[na:na]

I am running docker container to redis.

@Configuration
@EnableCaching
public class RedisCacheConfiguration {

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory jedisConFactory
                = new JedisConnectionFactory();
        jedisConFactory.setHostName("localhost");
        jedisConFactory.setPort(6379);
        return jedisConFactory;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template =new RedisTemplate<>();
        template.setHashValueSerializer(new StringRedisSerializer());
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }

}



@Service
public class UserServiceImpl implements IUserService{

    private final UserRepository userRepository;

    public UserServiceImpl(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public UserDto createUser(CreateUserRequest createUserRequest){
         UserEntity userEntity = new UserEntity();
         userEntity.setName(createUserRequest.getName());
         userEntity.setSurname(createUserRequest.getSurname());
         userRepository.save(userEntity);
         return UserMapper.INSTANCE.userEntityToUserDto(userEntity);
    }

    @Cacheable(value= "user", key = "#userId")
    public UserDto getUserById(Long userId){
        System.out.println("getuserbyid içerisi");
        UserEntity userEntity = userRepository.getById(userId);
        return UserMapper.INSTANCE.userEntityToUserDto(userEntity);
    }
}
Umut
  • 31
  • 1
  • 4

1 Answers1

0

Annotate with @EnableRedisRepositories and UserEntity should implement serializable.

@Configuration
@EnableCaching
@EnableRedisRepositories
public class RedisCacheConfiguration {

@Bean
JedisConnectionFactory jedisConnectionFactory() {
    JedisConnectionFactory jedisConFactory
            = new JedisConnectionFactory();
    jedisConFactory.setHostName("localhost");
    jedisConFactory.setPort(6379);
    return jedisConFactory;
}

@Bean
public RedisTemplate<String, Object> redisTemplate() {
    RedisTemplate<String, Object> template =new RedisTemplate<>();
    template.setHashValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(jedisConnectionFactory());
    return template;
}

}

@RedisHash("UserEntity")
public class UserEntity implements Serializable{
}
Aditya
  • 31
  • 2
  • UserEntity already implements Serializable. But I am using h2 database. I want to use h2 database. When i call the getUserById method, firstly user should come from h2 database, when i call the second time this method, user should come from redis cache – Umut Oct 29 '21 at 06:17
  • when i changed the value from user to users it's working. But ı dont know why :) @Cacheable(value = "users", key = "#userId") – Umut Oct 29 '21 at 07:47
  • While saving user we put a value keyword in @CachePut and the same we use while fetching. Go through [this](https://www.mindbowser.com/spring-boot-with-redis-cache-using-annotation) for detail. – Aditya Oct 29 '21 at 08:59