如何使用spring-data-couchbase为特定的Couchbase文档设置TTL? [英] How to set TTL for a specific Couchbase document using spring-data-couchbase?
问题描述
如何使用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)
它将为所有文件设置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 $ c中的转码步骤,插入(变异)并一次性设置TTL会非常复杂$ c>方法。
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 viagetCouchbaseClient()
(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 anOperationFuture
(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屋!