如何使用spring-data-couchbase为特定的Couchbase文档设置TTL? [英] How to set TTL for a specific Couchbase document using spring-data-couchbase?

查看:289
本文介绍了如何使用spring-data-couchbase为特定的Couchbase文档设置TTL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用spring-data-couchbase为特定的couchbase文档设置TTL(生存时间)?
我知道有办法使用Document notation设置到期时间如下
@Document(expiry = 10)

How to set TTL (Time to Live) for a specific couchbase document using spring-data-couchbase? I know there is way to set expiry time using Document notation as follows @Document(expiry = 10)

http://docs.spring.io/spring -data / couchbase / docs / 1.1.1.RELEASE / reference / html / couchbase.entity.html

它将为所有文件设置TTL保存通过实体类。

It will set TTL for all the documents save through the Entity class.

但似乎有办法设置特定文档的到期时间(TTL)
获取并触摸:获取指定的文档并更新文件到期。
中提到
http:// docs .couchbase.com / developer / dev-guide-3.0 / read-write.html

But it seems there is way to set expiration(TTL) time for a specific document "Get and touch: Fetch a specified document and update the document expiration." mentioned in http://docs.couchbase.com/developer/dev-guide-3.0/read-write.html

如何通过spring-data-couchbase $实现上述功能b $ b即使我可以使用Java SDK实现功能,也没关系。

How can I achieve above functionality through spring-data-couchbase Even If I can achieve the functionality using Java SDK, would be fine.

任何帮助.....

推荐答案

使用Spring-Data-Couchbase,您无法在特定实例上设置TTL。考虑到隐藏在 CouchbaseTemplate save 方法。

Using Spring-Data-Couchbase, you cannot set a TTL on a particular instance. Inserting (mutating) and setting the TTL in one go would be quite complicated given the transcoding steps that are hidden away in the CouchbaseTemplate save method.

但是,如果您想要做的只是更新已保存文档的TTL(这是 getAndTouch 确实如此,有一种方法不涉及任何转码,因此可以轻松应用:

However, if what you want to do is just update the TTL of an already persisted document (which is what getAndTouch does), there is a way that doesn't involve any transcoding and so can be applied easily:


  • 来自 CouchbaseTemplate ,通过 getCouchbaseClient() note 目前sdc是基于上一代SDK构建的, 1.4.x ,但很快就会有sdc-2.0的预览;))

  • 使用SDK,对文档的ID执行 touch 操作,为其提供新的TTL

  • touch()方法返回 OperationFuture (它是异步),所以请确保封锁或考虑只有在回调中通知才会完成触摸。

  • From the CouchbaseTemplate, get access to the underlying SDK client via getCouchbaseClient() (note for now sdc is built on top of the previous generation of SDK, 1.4.x, but there'll be a preview of sdc-2.0 soon ;) )
  • Using the SDK, perform a touch operation on the document's ID, give it the new TTL
  • The touch() method returns an OperationFuture (it is async), so make sure to either block on it or consider the touch done only if notified so in the callback.

这篇关于如何使用spring-data-couchbase为特定的Couchbase文档设置TTL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆