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是HASH
, 1 ZSET
和所有其他人都是SET
。
I am new to Redis and developing code using Spring Boot + Spring Data Redis
example. When I saved the records, I see KEYS gets created and out of these keys 4 are HASH
, 1 ZSET
and all others are SET
.
我没有在Spring文档中看到,每个KEY的含义都被保存了。 。
I did not see in the Spring docs, meaning of each KEY is getting saved. .
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
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
Address.java
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Address {
private @Indexed String city;
private @Indexed String country;
private @GeoIndexed Point location;
}
Gender.java
Gender.java
public enum Gender {
FEMALE, MALE
}
RedisExampleBootApplication.java
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注释并想要查看它。
Before answering, do you mind sharing your RedisTemplate implementation code? (Or this is generated by @RedisHash annotation?) I am new to Spring-Data-Redis myself and didn't know of the @RedisHash annotation and want to check it out.
无论如何,本质上发生的事情是Spring-Data-Redis存储库将Person对象插入Redis原生支持的不同数据结构中用于不同目的。
Anyways, essentially what is happening here is that Spring-Data-Redis repository is inserting the Person object in different data structures natively supported by Redis for different purposes.
Redis支持不同的数据结构,例如:
Redis supports different data structures such as:
-
哈希
Redis创建字符串字段和字符串值的映射来表示你的整个Person对象。
如果您执行HGETALL人员:{您的人ID}
它将显示与您的人物相关的所有不同字段和值对象
Hash Redis creates a map of string fields and string values to represent your entire Person object. If you do
HGETALL persons:{your person id}
it will show all the different fields and values associated with your person Object
HASH在密钥空间人员中持有idc5cfd49d-6688-4b83-a9b7-be55dd1c36ad的属性值
设置
Redis根据字段插入基本原始字符串并索引实体。因此,Redis DB中有很多 SET
操作。您可以在数据集中看到 firstName
和 lastName
的索引
Set
Redis inserts the basic raw string and indexes entities based on their field. Hence there were a lot SET
operations in your Redis DB. You can see indexes of firstName
and lastName
in your data set
SET保存键空间人员中已知的所有ID
ZSet
这是排序集
数据结构的Redis操作。哪个是有序的字符串集合。
来自Redis Documentations
ZSet
This is Redis operation for Sorted Sets
data structure. Which is an ordered collections of strings.
From Redis Documentations
简而言之,对于排序集,您可以执行很多具有出色性能的任务,这些任务很难在其他类型的数据库。
似乎Spring Data会自动将位置数据插入排序集以优化CRUD操作。
Seems like Spring Data automatically inserts the location data as a sorted set to optimize CRUD operations.
您可以在这里阅读更多内容:
You can read more here:
https://redis.io/topics/data-types
这篇关于Redis - 关键HASH和SET和ZSET如何与CrudRepository保存相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!