Redis - 密钥 HASH 和 SET 以及 ZSET 如何与 CrudRepository 保存相关?
Redis - How the key HASH and SET and ZSET are related on the CrudRepository save?
我是 Redis 新手,使用 Spring Boot + Spring Data Redis
示例开发代码。当我保存记录时,我看到 KEYS 被创建并从这些键 4 are HASH
、1 ZSET
和 all 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"
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());
}
}
回答之前,介意分享一下你的RedisTemplate实现代码吗? (或者这是由 @RedisHash 注释生成的?)我自己是 Spring-Data-Redis 的新手,不知道 @RedisHash 注释并想检查一下。
无论如何,基本上这里发生的是 Spring-Data-Redis 存储库将 Person 对象插入到 Redis 原生支持的不同数据结构中,用于不同的目的。
Redis 支持不同的数据结构如:
哈希
Redis 创建一个字符串字段和字符串值的映射来表示整个 Person 对象。
如果你这样做 HGETALL persons:{your person id}
它将显示与你的对象相关联的所有不同字段和值 Object
HASH holding property values for id "c5cfd49d-6688-4b83-a9b7-be55dd1c36ad" in keyspace "persons"
设置
Redis 插入基本的原始字符串并根据实体的字段对其进行索引。因此,您的 Redis 数据库中有很多 SET
操作。您可以在数据集中看到 firstName
和 lastName
的索引
SET holding all ids known in the keyspace "persons"
Z集
这是对 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数据自动插入位置数据作为排序集来优化CRUD操作。
您可以在此处阅读更多内容:
https://github.com/spring-projects/spring-data-examples/blob/master/redis/repositories/README.md
我是 Redis 新手,使用 Spring Boot + Spring Data Redis
示例开发代码。当我保存记录时,我看到 KEYS 被创建并从这些键 4 are HASH
、1 ZSET
和 all 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"
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());
}
}
回答之前,介意分享一下你的RedisTemplate实现代码吗? (或者这是由 @RedisHash 注释生成的?)我自己是 Spring-Data-Redis 的新手,不知道 @RedisHash 注释并想检查一下。
无论如何,基本上这里发生的是 Spring-Data-Redis 存储库将 Person 对象插入到 Redis 原生支持的不同数据结构中,用于不同的目的。
Redis 支持不同的数据结构如:
哈希 Redis 创建一个字符串字段和字符串值的映射来表示整个 Person 对象。 如果你这样做
HGETALL persons:{your person id}
它将显示与你的对象相关联的所有不同字段和值 ObjectHASH holding property values for id "c5cfd49d-6688-4b83-a9b7-be55dd1c36ad" in keyspace "persons"
设置 Redis 插入基本的原始字符串并根据实体的字段对其进行索引。因此,您的 Redis 数据库中有很多
SET
操作。您可以在数据集中看到firstName
和lastName
的索引SET holding all ids known in the keyspace "persons"
Z集 这是对
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数据自动插入位置数据作为排序集来优化CRUD操作。
您可以在此处阅读更多内容:
https://github.com/spring-projects/spring-data-examples/blob/master/redis/repositories/README.md