Spring Data Redis(Repositories-Persisting References)

本文探讨了在Redis中使用@Reference注解进行属性存储的方法,仅保存引用而非完整值,以及如何通过PartialUpdate实现对象的部分更新而不必加载或重写整个实体。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 做进一步的交互来确认值是否存在,这意味着重写整个实体会更快一些。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值