Couchbase Server provides a set of commands to store documents. The commands are very similar to each other and differ only in their meaning on the server-side. These are:
set: Stores a document in Couchbase
Server (identified by its unique key) and overrides the
previous document (if there was one).
add: Adds a document in Couchbase Server
(identified by its unique key) and fails if there is already
a document with the same key stored.
replace: Replaces a document in Couchbase
Server (identified by its unique key) and fails if there is
no document with the given key already in place.
The SDK provides overloaded methods for these operations, but to start out here are the simplest forms:
String key = "mykey"; int timeout = 0; String doc = "something"; OperationFuture<Boolean> setResult = client.set(key, timeout, doc); OperationFuture<Boolean> addResult = client.add(key, timeout, doc); OperationFuture<Boolean> replaceResult = client.replace(key, timeout, doc);
Couchbase Server takes performance very seriously, so all these
operations are non-blocking (asynchronous). They return
immediately and are handled in a different thread behind the
scenes. If you want to explicitly wait until the operation has
finished, you can use the
get() method on the
returned future. If you do so, your calling thread will wait
until the operation has been fulfilled by the server and is
done. If something goes wrong, the
return value of the future will be
you can use the
future.getStatus().getMessage() method to see
why it failed.
The second param on all these mutation operations is a
timeout. When set to
the document will be stored forever (or until deleted manually).
If set to (for example)
10, the document will
be deleted automatically after 10 seconds. If you want to use
Couchbase Server as a session store, this comes in very handy
(amongst lots of other use cases).