Persisting References
给属性添加@Reference 注解后,会存储一个简单的key 引用而不是拷贝所有的值到hash 中。从Redis 中读取时,引用会被自动的处理,映射回到对象中。
Example 17. Sample Property Reference
_class = org.example.Person
id = e2c7dcee-b8cd-4424-883e-736ce564363e
firstname = rand
lastname = al’thor
mother = persons:a9d4b3a0-50d3-4538-a2fc-f7fc2581ee56
Reference 存储了被引用对象的整个key (keyspace:id) 。
被引用的对象不受限制于引用对象的持续变化。请确保对引用对象的持续变更是独立的,因为只有引用被存储了。在引用类型的属性上设置的索引将不会被处理。
Persisting Partial Updates
有时候,仅仅为了设置一个新的值,而加载或重写整个实体是没必要的。当你仅仅想修改一个属性时,为它设置一个最后活跃的会话时间戳是个不错的方案。在已经存在的对象上,当你关注于更新整个实体的潜在过期时间,以及索引结构时,PartialUpdate 允许你定义set 和delete 事件。
Example 18. Sample Partial Update
PartialUpdate<Person> update = new PartialUpdate<Person>("e2c7dcee", Person.class)
.set("firstname", "mat")
.set("address.city", "emond's field")
.del("age");
template.update(update);
update = new PartialUpdate<Person>("e2c7dcee", Person.class)
.set("address", new Address("caemlyn", "andor"))
.set("attributes", singletonMap("eye-color", "grey"));
template.update(update);
update = new PartialUpdate<Person>("e2c7dcee", Person.class)
.refreshTtl(true);
.set("expiration", 1000);
template.update(update);
Set the simple property firstname to mat.
Set the simple property address.city to emond’s field without having to pass in the entire object. This does not work when a custom conversion is registered.
Remove the property age.
Set complex property address.
Set a map/collection of values removes the previously existing map/collection and replaces the values with the given ones.
Automatically update the server expiration time when altering Time To Live.
更新复杂的对象或 map/collection 结构,需要和Redis 做进一步的交互来确认值是否存在,这意味着重写整个实体会更快一些。