Usage
Spring Data Redis 让你可以轻松实现领域实体,如下面的示例所示: .Sample Person Entity
@RedisHash("people")
public class Person {
@Id String id;
String firstname;
String lastname;
Address address;
}
这里有一个非常简单的领域对象。请注意它在其类型上带有 @RedisHash
注释,并且有一个名为 id
的属性,该属性用 org.springframework.data.annotation.Id
进行了注释。这两个项目负责创建用于持久化哈希的实际键。
使用 |
为了现在实际上拥有一个负责存储和检索的组件,我们需要定义一个存储库接口,如下面的示例所示: .Basic Repository Interface To Persist Person Entities
public interface PersonRepository extends CrudRepository<Person, String> {
}
由于我们的存储库扩展了 CrudRepository
,因此它提供了基本的 CRUD 和查找器操作。我们需要在两者之间粘贴东西的东西是相应的 Spring 配置,如下面的示例所示:
.JavaConfig for Redis Repositories
@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
注入到我们的组件中,如下面的示例所示:
.Access to Person Entities
@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> 如果当前值为 `null`,则生成一个新的 `id`,或重复使用已设置的 `id`值,并在具有模式 `keyspace:id`的键中存储类型为 `Person`的属性——在这种情况下,可能是 `people:5d67b7e1-8640-2024-beeb-c666fab4c0e5`。 <1> 使用提供的 `id`检索存储在 `keyspace:id`的对象。 <1> 统计 keyspace 中可用的实体的总数,`people`,由 `Person`上的 `@RedisHash`定义。 <1> 从 Redis 中删除给定对象的键。
Persisting References
使用 @Reference
标记属性允许存储一个简单的键引用,而不是将值本身复制到哈希中。从 Redis 加载时,引用会自动解析并映射回对象,如下面的示例所示:
_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`)。
当引用对象被保存时,引用对象不会被持久化。您必须单独持久化对引用对象的更改,因为只存储了该引用。不会解析设置在引用类型属性上的索引。
Persisting Partial Updates
在某些情况下,你不必加载和重写整个实体,只需要在其中设置一个新值。最后一个活动时间的会话时间戳可能就是一个这样的场景,你希望更改一个属性。PartialUpdate
让你可以对现有对象定义 set
和 delete
操作,同时负责更新实体本身和索引结构的潜在过期时间。以下示例显示了部分更新:
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`。 <1> 将简单 'address.city' 属性设置为 'emond’s field',而无需传入整个对象。当注册了自定义转换时,此方法不起作用。 <1> Remove the `age` property. <1> Set complex `address` property. <1> 设置一个值映射,该映射删除先前存在的值映射,并用给定的值替换该值。 <1> 更改 xref:redis/redis-repositories/expirations.adoc[Time To Live]时自动更新服务器过期时间。
更新复杂的对象以及映射(或其他集合)结构需要进一步与 Redis 交互来确定现有值,这意味着可能更快速地重写整个实体。 |