用法
Spring Data Redis 允许您轻松实现域实体,如以下示例所示:
示例 1.示例人员实体
@RedisHash("people")
public class Person {
  @Id String id;
  String firstname;
  String lastname;
  Address address;
}我们这里有一个非常简单的域对象。
请注意,它有一个@RedisHash注释,并命名为属性id用org.springframework.data.annotation.Id.
这两个项负责创建用于持久化哈希的实际密钥。
| 用 @Id以及那些被命名的id被视为标识符属性。
那些有注释的人比其他人更受青睐。 | 
现在要真正拥有一个负责存储和检索的组件,我们需要定义一个存储库接口,如以下示例所示:
示例 2.用于持久化人员实体的基本存储库接口
public interface PersonRepository extends CrudRepository<Person, String> {
}随着我们的存储库的扩展CrudRepository,它提供基本的 CRUD 和查找器作。
我们需要将东西粘合在一起的是相应的 Spring 配置,如以下示例所示:
示例 3.用于 Redis 存储库的 JavaConfig
@Configuration
@EnableRedisRepositories
public class ApplicationConfig {
  @Bean
  public RedisConnectionFactory connectionFactory() {
    return new LettuceConnectionFactory();
  }
  @Bean
  public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>();
    template.setConnectionFactory(redisConnectionFactory);
    return template;
  }
}给定前面的设置,我们可以注入PersonRepository到我们的组件中,如以下示例所示:
示例 4.访问个人实体
@Autowired PersonRepository repo;
public void basicCrudOperations() {
  Person rand = new Person("rand", "al'thor");
  rand.setAddress(new Address("emond's field", "andor"));
  repo.save(rand);                                         (1)
  repo.findOne(rand.getId());                              (2)
  repo.count();                                            (3)
  repo.delete(rand);                                       (4)
}| 1 | 生成新的 id如果当前值为null或重用已设置的idvalue 并存储类型为Person在 Redis 哈希中,其键的模式为keyspace:id——在这种情况下,它可能是people:5d67b7e1-8640-2025-beeb-c666fab4c0e5. | 
| 2 | 使用提供的 id检索存储在keyspace:id. | 
| 3 | 计算键空间中可用的实体总数, people,由@RedisHash上Person. | 
| 4 | 从 Redis 中删除给定对象的键。 | 
持久化引用
标记属性@Reference允许存储一个简单的键引用,而不是将值复制到哈希本身。
从 Redis 加载时,引用会自动解析并映射回对象,如以下示例所示:
示例 5.示例属性参考
_class = org.example.Person
id = e2c7dcee-b8cd-4424-883e-736ce564363e
firstname = rand
lastname = al’thor
mother = people:a9d4b3a0-50d3-4538-a2fc-f7fc2581ee56      (1)| 1 | 引用存储整个键 ( keyspace:id) 引用对象。 | 
| 保存引用对象时,不会保留引用对象。 必须单独保留对引用对象的更改,因为仅存储引用。 不会解析在引用类型的属性上设置的索引。 | 
保留部分更新
在某些情况下,您无需加载和重写整个实体,只是为了在其中设置新值。
上次活动时间的会话时间戳可能是您想要更改一个属性的场景。PartialUpdate允许您定义set和delete对现有对象执行作,同时负责更新实体本身和索引结构的潜在过期时间。
以下示例显示了部分更新:
示例 6.部分更新示例
PartialUpdate<Person> update = new PartialUpdate<Person>("e2c7dcee", Person.class)
  .set("firstname", "mat")                                                           (1)
  .set("address.city", "emond's field")                                              (2)
  .del("age");                                                                       (3)
template.update(update);
update = new PartialUpdate<Person>("e2c7dcee", Person.class)
  .set("address", new Address("caemlyn", "andor"))                                   (4)
  .set("attributes", singletonMap("eye-color", "grey"));                             (5)
template.update(update);
update = new PartialUpdate<Person>("e2c7dcee", Person.class)
  .refreshTtl(true);                                                                 (6)
  .set("expiration", 1000);
template.update(update);| 1 | 设置简单的 firstname属性设置为mat. | 
| 2 | 将简单的 'address.city' 属性设置为 'emond's field',而无需传入整个对象。 注册自定义转换时,此功能不起作用。 | 
| 3 | 删除 age财产。 | 
| 4 | 设置复杂 address财产。 | 
| 5 | 设置值映射,这将删除先前存在的映射并将值替换为给定的映射。 | 
| 6 | 更改生存时间时自动更新服务器过期时间。 | 
| 更新复杂对象以及 map(或其他集合)结构需要与 Redis 进一步交互以确定现有值,这意味着重写整个实体可能会更快。 |