| 此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Data Redis 3.5.3! | 
Redis 存储库剖析
Redis 作为一个存储本身提供了一个非常狭窄的低级 API,将更高级别的功能(例如二级索引和查询作)留给用户。
本节提供了存储库抽象发出的命令的更详细视图,以便更好地了解潜在的性能影响。
将以下实体类视为所有作的起点:
示例 1.示例实体
@RedisHash("people")
public class Person {
  @Id String id;
  @Indexed String firstname;
  String lastname;
  Address hometown;
}
public class Address {
  @GeoIndexed Point location;
}插入新
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 | 将以 <1> 写入的哈希的键添加到同一键空间中实体的辅助索引中。 | 
| 3 | 将以 <2> 写入的哈希的键添加到具有属性值的 firstnames 的二级索引中。 | 
| 4 | 将 <3> 的索引添加到用于条目的帮助程序结构集中,以跟踪删除/更新时要清理的索引。 | 
替换现有
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 | 将扁平化条目保存为哈希。 | 
| 3 | 将以 <1> 写入的哈希的键添加到同一键空间中实体的辅助索引中。 | 
| 4 | 获取可能需要更新的现有索引结构。 | 
| 5 | 检查索引是否存在以及它的类型(文本、地理位置等)。 | 
| 6 | 从索引中删除可能存在的键。 | 
| 7 | 删除保存索引信息的帮助程序。 | 
| 8 | 将 <2> 中添加的哈希的键添加到具有属性值的 firstnames 的二级索引中。 | 
| 9 | 将 <6> 的索引添加到用于条目的帮助程序结构集中,以跟踪删除/更新时要清理的索引。 | 
保存地理数据
地理索引遵循与普通基于文本的索引相同的规则,但使用地理结构来存储值。 保存使用异地索引属性的实体会导致以下命令:
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 | 跟踪索引结构。 | 
使用简单索引查找
repository.findByFirstname("egwene");SINTER  "people:firstname:egwene"                     (1)
HGETALL "people:d70091b5-0b9a-4c0a-9551-519e61bc9ef3" (2)
HGETALL ...| 1 | 获取二级索引中包含的键。 | 
| 2 | 单独获取 <1> 返回的每个密钥。 | 
使用地理索引查找
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 | 单独获取 <1> 返回的每个密钥。 |