Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindBy in Redis Repository with spring

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.

like image 688
AnnKont Avatar asked Sep 04 '25 02:09

AnnKont


1 Answers

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?

like image 140
nimbudew Avatar answered Sep 07 '25 06:09

nimbudew