简体   繁体   English

使用 spring 的 Redis 存储库中的 FindBy

[英]FindBy in Redis Repository with spring

I want save and get data from redis use spring.我想使用 spring 从 redis 保存和获取数据。 I have redis only for profile SOME .我有 redis 仅用于配置文件SOME I save data ok.我保存数据ok。 But when I try to get data by method findByIdAndName , I always have empty Optional.但是当我尝试通过方法findByIdAndName获取数据时,我总是有空的 Optional。

My pojo:我的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:我有 redis 配置:

@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和 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?那么,为什么我的方法findByIdAndName不起作用? If I use method findById(id) , it work correct and return data.如果我使用方法findById(id) ,它可以正常工作并返回数据。

You're missing the @Indexed annotation on the id field.您缺少id字段上的@Indexed注释。
Also see this question for reference: Unable to get result from the Redis using Crud Repository in Spring Boot?另请参阅此问题以供参考: Unable to get result from the Redis using Crud Repository in Spring Boot?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 存储库 findBy 连接表 - Spring repository findBy join table Spring 引导应用程序中的 Jpa 存储库按问题查找 - Jpa Repository in Spring boot app findBy issue 如何在Spring Boot中使存储库findBy()具有多个字段? - How to make repository findBy() with more than one field in Spring Boot? 如何使用findBy Spring存储库在Java中的Map中检索数据 - How to retrieve data within a Map in java using findBy spring repository 使用Dynomite定制Spring会话存储库Redis - Custom spring-session repository Redis with Dynomite Spring JPA存储库findBy直接通过外键使用POJO代替类的数据成员 - Spring JPA Repository findBy foreign key directly with POJO instead of data member of class 如何使用包含关键字的属性创建 Spring JPA 存储库 findBy 查询? - How do you create a Spring JPA repository findBy query using a property that contains a keyword? Spring存储库找不到`findBy*`方法,将事务标记为回滚,导致持久化失败 - Spring repository does not find a `findBy*` method and marks the transaction for rollback, causing persistence failures Spring 数据存储库 findById 与派生 findBy[id-column-name] - Spring data repository findById vs derived findBy[id-column-name] Spring数据CrudRepository findBy关系 - Spring data CrudRepository findBy relationships
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM