Redis-为什么使用Spring Data Redis将详细信息同时保存为HASH和SET?

时间:2018-11-06 08:49:24

标签: spring spring-boot redis jedis spring-data-redis

我是Redis的新手,正在开发Spring Boot + Spring Data Redis示例。我正在使用CrudRepositoryExampleExampleMatchers API从Redis键值存储数据库中进行搜索。

现在,当我只运行代码时,我看到人员数据也保存为SETHASH这正确吗?将“人员”详细信息另存为SET和HASH的用途是什么

enter image description here

显示我的所有代码

public enum Gender {
    MALE, FEMALE {
        @Override
        public String toString() {
            return "Superwoman";
        }
    }
}

Species.java

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Species {

    @Indexed
    private String name;
}

Person.java

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("persons")
public class Person {

    @Id
    private String id;

    @Indexed
    private String firstname;
    private String lastname;
    @Indexed
    private Gender gender;

    private List<String> nicknames;
    @Indexed
    private Integer age;

    private Map<String, String> physicalAttributes;

    @Reference
    private Person relative;

    private Species species;
}

PersonRepository.java

public interface PersonRepository extends CrudRepository<Person, String>, QueryByExampleExecutor<Person> {

}

RedisExampleDemoApplication.java

@SpringBootApplication
public class RedisExampleDemoApplication implements CommandLineRunner{
    RedisMappingContext mappingContext = new RedisMappingContext();
    ExampleQueryMapper mapper = new ExampleQueryMapper(mappingContext, new PathIndexResolver(mappingContext));

    @Autowired
    private PersonRepository personRepository;

    public static void main(String[] args) {
        SpringApplication.run(RedisExampleDemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Person person = Person.builder().firstname("Walter").gender(Gender.MALE).age(50).build();
        Person person1 = Person.builder().firstname("Savani").gender(Gender.FEMALE).age(35).build();

        personRepository.save(person);
        personRepository.save(person1);

        // [firstname:Walter, gender:MALE, age:50]
        RedisOperationChain operationChain = mapper.getMappedExample(Example.of(person, ExampleMatcher.matchingAny()));
        System.out.println(operationChain.getOrSismember());


        System.out.println("----------------------------------------------");
        Person p = Person.builder().lastname("Foo").build();
        RedisOperationChain roc = mapper.getMappedExample(Example.of(p));
        System.out.println(" == "+roc.getOrSismember());
        System.out.println("-- "+roc.getSismember());
    }
}

1 个答案:

答案 0 :(得分:0)

现在可能回答晚了,SET可见的原因是由于二级索引。即在您的示例中,名字被标注为索引。 Redis将此视为辅助索引,默认情况下为SET。