3

I want save and get data from redis use spring. I have redis only for profile SOME. I save data ok. But when I try to get data by method findByIdAndName, I always have empty Optional.

My pojo:

@RedisHash("Some")
@Data
@AllArgsConstructor
public class SomeInfo implements Serializable {

    @Id
    private String id;

    @Indexed
    private String name;

    @TimeToLive(unit = TimeUnit.DAYS)
    private Long expiration;
}

My repository:

public interface SomeInfoRepository extends CrudRepository<SomeInfo, String> {

    Optional<SomeInfo> findByIdAndName(String id, String name);
}

And I have redis config:

@Profile(Profiles.SOME)
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(RedisProperties.class)
@EnableRedisRepositories(basePackageClasses = { SomeInfoRepository.class })
public class RedisConfig {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(ObjectMapper objectMapper, RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer(objectMapper));
        return template;
    }

    @Bean(name = "someTopic")
    public ChannelTopic someTopic(@Value("${app.topics.some}") String someChannel) {
        return new ChannelTopic(someChannel);
    }

    @Bean
    public RedisMessageListenerContainer roundRedisContainer(RedisConnectionFactory connectionFactory,
                                                             SomeMessageListener someMessageListener,
                                                             @Qualifier("someTopic") ChannelTopic someTopic) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(someMessageListener, someTopic);
        return container;
    }

}

And application.yml

spring:
  redis:
    host: localhost
    port: 6379
    ssl: false
    password: some
  data.redis.repositories.enabled: false

I know that data save ok:

keys *
1) "Some:36:idx"
2) "Some"
3) "Some:name:af0b83a2b15f018bdd404aef33bfb11f"
4) "Some:name:f5f8c3a9119bb822b0c2fde3265d37fa"
5) "Some:41:idx"
6) "Some:41:phantom"
7) "Some:36:phantom"
8) "Some:41"
9) "Some:36"

And

hgetall Some:41
1) "_class"
2) "ru.some.SomeInfo"
3) "id"
4) "41"
5) "name"
6) "af0b83a2b15f018bdd404aef33bfb11f"
7) "expiration"
8) "30"

So, why is my method findByIdAndName not work? If I use method findById(id), it work correct and return data.

AnnKont
  • 424
  • 4
  • 17
  • Is it returning an empty response or you're getting some error? You can use redis monitor command to debug and check what key is the application trying to perform a get operation on. Run redis monitor from redis-cli and check if any GET operation was performed and on which key was it performed when you executed `findByIdAndName`. Don't run redis monitor for a long time though on a production system. – nimbudew Jun 16 '20 at 10:58
  • @nimbudew No? I haven't any errors, I'm getting empty response. I turn on monitoring and get this request ` "SINTER" "Some:id:41" "Some:name:af0b83a2b15f018bdd404aef33bfb11f"` – AnnKont Jun 16 '20 at 11:14
  • I do this comand: `> SINTER Some:id:41` and get: `(empty list or set)` do this `> SINTER Some:name:af0b83a2b15f018bdd404aef33bfb11f` and get `1) "41"` – AnnKont Jun 16 '20 at 11:17
  • 1
    I think you should also use @Indexed on id field. See this: https://stackoverflow.com/questions/53121627/unable-to-get-result-from-the-redis-using-crud-repository-in-spring-boot – nimbudew Jun 16 '20 at 11:23

1 Answers1

8

You're missing the @Indexed annotation on the id field.
Also see this question for reference: Unable to get result from the Redis using Crud Repository in Spring Boot?

nimbudew
  • 958
  • 11
  • 28