Redis Repositories Anatomy
Redis 自身作为存储提供了一个非常有限的低级 API,将二级索引和查询操作等高级功能留给用户自己处理。
Redis as a store itself offers a very narrow low-level API leaving higher level functions, such as secondary indexes and query operations, up to the user.
本部分提供了存储库抽象发出的命令的更详细视图,以便更好地理解潜在的性能影响。
This section provides a more detailed view of commands issued by the repository abstraction for a better understanding of potential performance implications.
考虑将以下实体类作为所有操作的起点:
Consider the following entity class as the starting point for all operations: .Example entity
@RedisHash("people")
public class Person {
@Id String id;
@Indexed String firstname;
String lastname;
Address hometown;
}
public class Address {
@GeoIndexed Point location;
}
Insert new
repository.save(new Person("rand", "al'thor"));
HMSET "people:19315449-cda2-4f5c-b696-9cb8018fa1f9" "_class" "Person" "id" "19315449-cda2-4f5c-b696-9cb8018fa1f9" "firstname" "rand" "lastname" "al'thor" 1
SADD "people" "19315449-cda2-4f5c-b696-9cb8018fa1f9" 2
SADD "people:firstname:rand" "19315449-cda2-4f5c-b696-9cb8018fa1f9" 3
SADD "people:19315449-cda2-4f5c-b696-9cb8018fa1f9:idx" "people:firstname:rand" 4
1 | 将扁平化条目以哈希形式保存。 |
2 | Save the flattened entry as hash. |
3 | 将 <1> 中编写的哈希键添加到相同键空间中实体的帮助程序索引中。 |
4 | Add the key of the hash written in <1> to the helper index of entities in the same keyspace. |
5 | 将 <2> 中编写的哈希键添加到名索引的辅助索引中,其中包含属性值。 |
6 | Add the key of the hash written in <2> to the secondary index of firstnames with the properties value. |
7 | 将 <3> 的索引添加到条目的帮助程序结构集中,以跟踪在删除/更新时需要清理的索引。 |
8 | Add the index of <3> to the set of helper structures for entry to keep track of indexes to clean on delete/update. |
Replace existing
repository.save(new Person("e82908cf-e7d3-47c2-9eec-b4e0967ad0c9", "Dragon Reborn", "al'thor"));
DEL "people:e82908cf-e7d3-47c2-9eec-b4e0967ad0c9" 1
HMSET "people:e82908cf-e7d3-47c2-9eec-b4e0967ad0c9" "_class" "Person" "id" "e82908cf-e7d3-47c2-9eec-b4e0967ad0c9" "firstname" "Dragon Reborn" "lastname" "al'thor" 2
SADD "people" "e82908cf-e7d3-47c2-9eec-b4e0967ad0c9" 3
SMEMBERS "people:e82908cf-e7d3-47c2-9eec-b4e0967ad0c9:idx" 4
TYPE "people:firstname:rand" 5
SREM "people:firstname:rand" "e82908cf-e7d3-47c2-9eec-b4e0967ad0c9" 6
DEL "people:e82908cf-e7d3-47c2-9eec-b4e0967ad0c9:idx" 7
SADD "people:firstname:Dragon Reborn" "e82908cf-e7d3-47c2-9eec-b4e0967ad0c9" 8
SADD "people:e82908cf-e7d3-47c2-9eec-b4e0967ad0c9:idx" "people:firstname:Dragon Reborn" 9
1 | 删除现有哈希以避免哈希键的剩余部分可能不再存在。 |
2 | Remove the existing hash to avoid leftovers of hash keys potentially no longer present. |
3 | 将扁平化条目以哈希形式保存。 |
4 | Save the flattened entry as hash. |
5 | 将 <1> 中编写的哈希键添加到相同键空间中实体的帮助程序索引中。 |
6 | Add the key of the hash written in <1> to the helper index of entities in the same keyspace. |
7 | 获取可能需要更新的现有索引结构。 |
8 | Get existing index structures that might need to be updated. |
9 | 检查索引是否存在以及它是哪种类型(文本、地理、…)。 |
10 | Check if the index exists and what type it is (text, geo, …). |
11 | 从索引中删除可能存在的键。 |
12 | Remove a potentially existing key from the index. |
13 | 删除保存索引信息的帮助程序。 |
14 | Remove the helper holding index information. |
15 | 将 <2> 中添加的哈希键添加到名索引的辅助索引中,其中包含属性值。 |
16 | Add the key of the hash added in <2> to the secondary index of firstnames with the properties value. |
17 | 将 <6> 的索引添加到条目的帮助程序结构集中,以跟踪在删除/更新时需要清理的索引。 |
18 | Add the index of <6> to the set of helper structures for entry to keep track of indexes to clean on delete/update. |
Save Geo Data
地理索引遵循与普通基于文本的索引相同规则,但使用地理结构来存储值。保存使用地理索引属性的实体会导致发出以下命令:
Geo indexes follow the same rules as normal text based ones but use geo structure to store values. Saving an entity that uses a Geo-indexed property results in the following commands:
GEOADD "people:hometown:location" "13.361389" "38.115556" "76900e94-b057-44bc-abcf-8126d51a621b" 1
SADD "people:76900e94-b057-44bc-abcf-8126d51a621b:idx" "people:hometown:location" 2
1 | 将已保存条目的键添加到地理索引中。 |
2 | Add the key of the saved entry to the the geo index. |
3 | 跟踪索引结构。 |
4 | Keep track of the index structure. |
Find using simple index
repository.findByFirstname("egwene");
SINTER "people:firstname:egwene" 1
HGETALL "people:d70091b5-0b9a-4c0a-9551-519e61bc9ef3" 2
HGETALL ...
1 | 获取辅助索引中包含的键。 |
2 | Fetch keys contained in the secondary index. |
3 | 分别获取每个由 <1> 返回的键。 |
4 | Fetch each key returned by <1> individually. |
Find using Geo Index
repository.findByHometownLocationNear(new Point(15, 37), new Distance(200, KILOMETERS));
GEORADIUS "people:hometown:location" "15.0" "37.0" "200.0" "km" 1
HGETALL "people:76900e94-b057-44bc-abcf-8126d51a621b" 2
HGETALL ...
1 | 获取辅助索引中包含的键。 |
2 | Fetch keys contained in the secondary index. |
3 | 分别获取每个由 <1> 返回的键。 |
4 | Fetch each key returned by <1> individually. |