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
.
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
@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());
}
}
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.
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 supports different data structures such as:
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 ObjectHASH holding property values for id "c5cfd49d-6688-4b83-a9b7-be55dd1c36ad" in keyspace "persons"
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 offirstName
andlastName
in your data setSET holding all ids known in the keyspace "persons"
ZSet This is Redis operation for
Sorted Sets
data structure. Which is an ordered collections of strings. From Redis DocumentationsIn 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.
Seems like Spring Data automatically inserts the location data as a sorted set to optimize CRUD operations.
You can read more here:
https://github.com/spring-projects/spring-data-examples/blob/master/redis/repositories/README.md
来源:https://stackoverflow.com/questions/53194434/redis-how-the-key-hash-and-set-and-zset-are-related-on-the-crudrepository-save