跳到主要内容
版本:4.1.1

用法

Doubao 中英对照 Usage

Spring Data Redis 让你可以轻松实现领域实体,如下例所示:

示例 1. 示例 Person 实体

@RedisHash("people")
public class Person {

@Id String id;
String firstname;
String lastname;
Address address;
}

这里我们有一个非常简单的领域对象。请注意,它的类型上有 @RedisHash 注解,还有一个名为 id 的属性,用 org.springframework.data.annotation.Id 注解标记。这两者负责创建用于持久化 hash 的实际键。

备注

用 @Id 注解的属性以及名为 id 的属性都被视为标识符属性。带注解的属性优先于其他属性。

为了真正拥有一个负责存储和检索的组件,我们需要定义一个仓库接口,如下例所示:

示例 2. 持久化 Person 实体的基本仓库接口

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. 访问 Person 实体

@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
}
  • 如果当前值为 null,则生成新的 id;否则复用已设置的 id 值,并将 Person 类型的属性存储在 Redis Hash 中,键的模式为 keyspace:id —— 本例中可能是 people:5d67b7e1-8640-present-beeb-c666fab4c0e5。

  • 使用提供的 id 检索存储在 keyspace:id 的对象。

  • 统计 Person 上的 @RedisHash 所定义的 keyspace(people)内可用的实体总数。

  • 从 Redis 中移除给定对象的键。

持久化引用​

用 @Reference 标记属性可以存储简单的键引用,而不是将值复制到 hash 本身中。从 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>
  • 引用存储被引用对象的完整键(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);
  • 将简单属性 firstname 设置为 mat。

  • 无需传入整个对象,即可将简单属性 'address.city' 设置为 'emond's field'。当注册了自定义转换时,此方式不适用。

  • 移除 age 属性。

  • 设置复杂属性 address。

  • 设置一个值 map,这会移除先前已存在的 map,并用给定的值替换。

  • 更改存活时间时自动更新服务端过期时间。

备注

更新复杂对象以及 map(或其他集合)结构需要与 Redis 进行更多交互来确定已有值,这意味着重写整个实体可能更快。