How to set TTL for a specific Couchbase document using spring-data-couchbase?

荒凉一梦 提交于 2019-11-30 18:24:58

问题


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

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

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

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

Any help.....


回答1:


Using Spring data couchbase, this is a simple way you can configure ttl per document.

public class CouchbaseConfig extends 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());
            }
        }
    }


}



回答2:


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.

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:

  • 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.


来源:https://stackoverflow.com/questions/31528665/how-to-set-ttl-for-a-specific-couchbase-document-using-spring-data-couchbase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!