对于最新的稳定版本,请使用 Spring Data Redis 4.0.4spring-doc.cadn.net.cn

Redis 存储库结构

Redis 本身作为一种存储,仅提供非常有限的底层 API,而将更高级的功能(例如二级索引和查询操作)留给用户自行实现。spring-doc.cadn.net.cn

本节提供了对仓库抽象层所发出命令的更详细视图,以帮助更好地理解其潜在的性能影响。spring-doc.cadn.net.cn

将以下实体类作为所有操作的起点:spring-doc.cadn.net.cn

示例 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的二级索引中,该二级索引具有value属性。
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>添加到用于记录删除/更新时需要清理的索引集的帮助结构集中。

保存地理数据

地理索引遵循与普通基于文本的索引相同的规则,但使用地理结构来存储值。 保存使用地理索引属性的实体将产生以下命令:spring-doc.cadn.net.cn

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>返回的每个键分别获取。