Redis-关键HASH和SET和ZSET在CrudRepository保存中如何关联?

时间:2018-11-07 17:10:21

标签: java redis jedis spring-data-redis

我是Redis的新手,正在使用Spring Boot + Spring Data Redis示例开发代码。保存记录时,我看到已创建密钥,并且这些密钥4 are HASH1 ZSETall others are SET中没有密钥。

我没有在Spring文档中看到,这意味着每个KEY都已保存。

127.0.0.1:6379> KEYS *
 1) "persons:c5cfd49d-6688-4b83-a9b7-be55dd1c36ad"
 2) "persons:firstname:bran"
 3) "persons:39e14dae-fa23-4935-948f-1922d668d1c2"
 4) "persons:f0b6dd26-8922-4a36-bd2a-792a17dddff7"
 5) "persons:address.city:Achalpur"
 6) "persons:e493385a-64ae-42be-8398-51757153d273:idx"
 7) "persons:053cdfea-e430-4e1c-acbd-ac40050b10cd:idx"
 8) "persons:firstname:rickon"
 9) "persons:e493385a-64ae-42be-8398-51757153d273"
10) "persons:address.country:India"
11) "persons:e7fc3ebe-9b48-48a8-a5f4-33a0e21f782f:idx"
12) "persons:firstname:sansa"
13) "persons:address:location"
14) "persons:firstname:robb"
15) "persons:firstname:jon"
16) "persons:lastname:snow"
17) "persons:e7fc3ebe-9b48-48a8-a5f4-33a0e21f782f"
18) "persons:c5cfd49d-6688-4b83-a9b7-be55dd1c36ad:idx"
19) "persons:lastname:stark"
20) "persons:f0b6dd26-8922-4a36-bd2a-792a17dddff7:idx"
21) "persons:053cdfea-e430-4e1c-acbd-ac40050b10cd"
22) "persons:39e14dae-fa23-4935-948f-1922d668d1c2:idx"
23) "persons:firstname:arya"
24) "persons:83cd4f58-c272-4d81-9023-8c66c8ac34b0:idx"
25) "persons:83cd4f58-c272-4d81-9023-8c66c8ac34b0"
26) "persons:address.city:Nagpur"
27) "persons:firstname:eddard"
28) "persons"

enter image description here

Person.java

@Data
@EqualsAndHashCode(exclude = { "children" })
@NoArgsConstructor
@AllArgsConstructor
@Builder
@RedisHash("persons")
public class Person {

    private @Id String id;
    private @Indexed String firstname;
    private @Indexed String lastname;

    private Gender gender;
    private Address address;

    private @Reference List<Person> children;
}

Address.java

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Address {

    private @Indexed String city;
    private @Indexed String country;
    private @GeoIndexed Point location;
}

Gender.java

public enum Gender {
    FEMALE, MALE
}

RedisExampleBootApplication.java

@SpringBootApplication
public class RedisExampleBootApplication implements CommandLineRunner{

    @Autowired PersonRepository repository;

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

    @Override
    public void run(String... args) throws Exception {  
        Address address1 = Address.builder().city("the north").country("winterfell").location(new Point(52.9541053, -1.2401016)).build();
        Address address2 = Address.builder().city("Casterlystein").country("Westerland").location(new Point(51.5287352, -0.3817819)).build();

        Person eddard = Person.builder().firstname("eddard").lastname("stark").gender(Gender.MALE).address(address1).build();
        Person robb = Person.builder().firstname("robb").lastname("stark").gender(Gender.MALE).address(address2).build();
        Person sansa = Person.builder().firstname("sansa").lastname("stark").gender(Gender.FEMALE).address(address1).build();
        Person arya = Person.builder().firstname("arya").lastname("stark").gender(Gender.FEMALE).address(address2).build();
        Person bran = Person.builder().firstname("bran").lastname("stark").gender(Gender.MALE).address(address1).build();
        Person rickon = Person.builder().firstname("rickon").lastname("stark").gender(Gender.MALE).address(address2).build();
        Person jon = Person.builder().firstname("jon").lastname("snow").gender(Gender.MALE).address(address1).build();

        repository.save(eddard);
        repository.save(robb);
        repository.save(sansa);
        repository.save(arya);
        repository.save(bran);
        repository.save(rickon);
        repository.save(jon);

        List<Person> starks = repository.findByLastname(eddard.getLastname());
        System.out.println("Person ="+starks.size());
    }
}

1 个答案:

答案 0 :(得分:1)

在回答之前,您介意共享您的RedisTemplate实现代码吗? (或者这是由@RedisHash注释生成的?)我本人还是Spring-Data-Redis的新手,不知道@RedisHash注释,想对其进行检查。

无论如何,实质上,这里发生的是Spring-Data-Redis存储库将Person对象插入到Redis出于不同目的原生支持的不同数据结构中。

Redis支持不同的数据结构,例如:

  1. 哈希 Redis创建一个字符串字段和字符串值的映射,以表示整个Person对象。 如果您执行HGETALL persons:{your person id},它将显示与您的人物Object相关的所有不同字段和值

    HASH holding property values for id "c5cfd49d-6688-4b83-a9b7-be55dd1c36ad" in keyspace "persons"

  2. 设置 Redis会插入基本的原始字符串,并根据其字段为实体建立索引。因此,您的Redis数据库中有很多SET操作。您可以在数据集中看到firstNamelastName的索引

    SET holding all ids known in the keyspace "persons"

  3. ZSet 这是Sorted Sets数据结构的Redis操作。这是字符串的有序集合。 来自Redis文档

    In short with sorted sets you can do a lot of tasks with great performance that are really hard to model in other kind of databases.

好像Spring Data会自动将位置数据作为排序集插入以优化CRUD操作。

您可以在这里阅读更多内容:

https://github.com/spring-projects/spring-data-examples/blob/master/redis/repositories/README.md

https://redis.io/topics/data-types