使用 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();