2

如何使用spring-data-couchbase爲特定的couchbase文檔設置TTL(生存時間)? 我知道有辦法使用文檔註釋如下 @Document設置過期時間(過期= 10)如何使用spring-data-couchbase爲特定的Couchbase文檔設置TTL?

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

將TTL設置爲所有的文檔保存通過實體類。

但是,似乎有辦法爲特定文檔設置到期(TTL)時間 「獲取並觸摸:獲取指定的文檔並更新文檔到期。」 在 http://docs.couchbase.com/developer/dev-guide-3.0/read-write.html

提到我如何能實現上述功能,通過彈簧數據couchbase 即使我能實現使用Java SDK的功能,就可以了。

任何幫助.....

回答

2

使用Spring-數據Couchbase,你不能在一個特定的實例中設置一個TTL。考慮到轉碼步驟隱藏在方法中,因此插入(變異)和一次性設置TTL將會非常複雜。

但是,如果你想要做的就是更新的已經堅持文檔(這是什麼getAndTouch做)的TTL,有不涉及任何轉碼等方式可以方便地應用於:

  • CouchbaseTemplate,通過getCouchbaseClient()獲得訪問底層SDK的客戶現在SDC是建立在上一代的SDK,1.4.x的頂部,但會SDC-2.0的預覽很快;))
  • 我們對該文檔的ID執行touch操作,給它一個新的TTL
  • touch()方法返回一個OperationFuture(它是異步的),因此請確保阻止它或僅在通知時才考慮觸摸完成在回調中。
0

使用Spring數據庫,這是一個簡單的方法,您可以爲每個文檔配置ttl。

公共類CouchbaseConfig延伸AbstractCouchbaseConfiguration {

@Override 
protected List<String> bootstrapHosts() { 
    return Arrays.asList("localhost"); 
} 

@Override 
protected String getBucketName() { 
    return "default"; 
} 

@Override 
protected String getBucketPassword() { 
    return "password1"; 
} 

@Bean 
public MappingCouchbaseConverter mappingCouchbaseConverter() throws Exception { 
    MappingCouchbaseConverter converter = new ExpiringDocumentCouchbaseConverter(couchbaseMappingContext()); 
    converter.setCustomConversions(customConversions()); 
    return converter; 
} 


class ExpiringDocumentCouchbaseConverter extends MappingCouchbaseConverter { 

    /** 
    * Create a new {@link MappingCouchbaseConverter}. 
    * 
    * @param mappingContext the mapping context to use. 
    */ 
    public ExpiringDocumentCouchbaseConverter(MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext) { 
     super(mappingContext); 
    } 

    // Setting custom TTL on documents. 
    @Override 
    public void write(final Object source, final CouchbaseDocument target) { 
     super.write(source, target); 
     if (source instanceof ClassContainingTTL) { 
      target.setExpiration(((ClassContainingTTL) source).getTimeToLive()); 
     } 
    } 
} 

}

相關問題