使用 TTL 创建文档(生存时间)

TTL 值可用于确定文档在存储桶中需要多长时间。默认情况下,TTL 值为 0,这意味着它将在无限期的时间内存在。

String bucketName = "bucket";
List<String> nodes = Arrays.asList("node1","node2"); // IP or hostname of one or more nodes in the cluster

Cluster cluster = CouchbaseCluster.create(nodes);
Bucket bucket = cluster.openBucket(bucketName);
//create the document with id 123 and TTL 1seconds
bucket.insert(JsonDocument.create("123",JsonObject.empty(), 1)); //if TTL is 0 document will be there in the DB for indefinite time
//do other stuffs
//to update the TTL 
bucket.upsert(JsonDocument.create("123",JsonObject.empty())); //no TTL value is provided 
bucket.close();
cluster.disconnect();