The check-and-set methods provide a mechanism for updating information only if the client knows the check (CAS) value. This can be used to prevent clients from updating values in the database that may have changed since the client obtained the value. Methods for storing and updating information support a CAS method that allows you to ensure that the client is updating the version of the data that the client retrieved.
The check value is in the form of a 64-bit integer which is updated every time the value is modified, even if the update of the value does not modify the binary data. Attempting to set or update a key/value pair where the CAS value does not match the value stored on the server will fail.
The cas() methods are used to explicitly
set the value only if the CAS supplied by the client matches the
CAS on the server, analogous to the
Section 6.2, “Set Operations” method.
With all CAS operations, the CASResponse value
returned indicates whether the operation succeeded or not, and if
not why. The CASResponse is an
Enum with three possible values:
EXISTS
The item exists, but the CAS value on the database does not match the value supplied to the CAS operation.
NOT_FOUND
The specified key does not exist in the database. An
add() method should be used to add the key
to the database.
OK
The CAS operation was successful and the updated value is stored in Couchbase
| API Call | client.cas(key, casunique, value) | ||
| Asynchronous | no | ||
| Description | Compare and set a value providing the supplied CAS key matches | ||
| Returns | CASResponse (
Check and set object
) | ||
| Arguments | |||
String key | Document ID used to identify the value | ||
long casunique | Unique value used to verify a key/value combination | ||
Object value | Value to be stored | ||
The first form of the cas() method allows
for an item to be set in the database only if the CAS value
supplied matches that stored on the server.
For example:
CASResponse casr = client.cas("caskey", casvalue, "new string value"); if (casr.equals(CASResponse.OK)) { System.out.println("Value was updated"); } else if (casr.equals(CASResponse.NOT_FOUND)) { System.out.println("Value is not found"); } else if (casr.equals(CASResponse.EXISTS)) { System.out.println("Value exists, but CAS didn't match"); }
| API Call | client.cas(key, casunique, value, transcoder) | ||
| Asynchronous | no | ||
| Description | Compare and set a value providing the supplied CAS key matches | ||
| Returns | CASResponse (
Check and set object
) | ||
| Arguments | |||
String key | Document ID used to identify the value | ||
long casunique | Unique value used to verify a key/value combination | ||
Object value | Value to be stored | ||
Transcoder<T> transcoder | Transcoder class to be used to serialize value | ||
The second form of the method supports using a custom transcoder for storing a value.
| API Call | client.cas(key, casunique, expiry, value, transcoder) | ||
| Asynchronous | no | ||
| Description | Compare and set a value providing the supplied CAS key matches | ||
| Returns | CASResponse (
Check and set object
) | ||
| Arguments | |||
String key | Document ID used to identify the value | ||
long casunique | Unique value used to verify a key/value combination | ||
int expiry | Expiry time for key. Values larger than 30*24*60*60 seconds (30 days) are interpreted as absolute times (from the epoch). | ||
Object value | Value to be stored | ||
Transcoder<T> transcoder | Transcoder class to be used to serialize value | ||
This form of the cas() method updates
both the key value and the expiry time for the value. For
information on expiry values, see
Section 4.4, “Expiry Values”.
For example the following attempts to set the key
caskey with an updated value, setting the
expiry times to 3600 seconds (one hour).
Transcoder<Integer> tc = new IntegerTranscoder(); CASResponse casr = client.cas("caskey", casvalue, 3600, 1200, tc);
| API Call | client.asyncCAS(key, casunique, value) | ||
| Asynchronous | yes | ||
| Description | Compare and set a value providing the supplied CAS key matches | ||
| Returns | Future<CASResponse> (
Asynchronous request value, as CASResponse
) | ||
| Arguments | |||
String key | Document ID used to identify the value | ||
long casunique | Unique value used to verify a key/value combination | ||
Object value | Value to be stored | ||
Performs an asynchronous CAS operation on the given key/value. You can use this method to set a value using CAS without waiting for the response. The following example requests an update of a key, timing out after 5 seconds if the operation was not successful.
Future<CASResponse> future = client.asyncCAS("someKey", casvalue, "updatedvalue"); CASResponse casr; try { casr = future.get(5, TimeUnit.SECONDS); } catch(TimeoutException e) { future.cancel(false); }
| API Call | client.asyncCAS(key, casunique, value, transcoder) | ||
| Asynchronous | yes | ||
| Description | Compare and set a value providing the supplied CAS key matches | ||
| Returns | Future<CASResponse> (
Asynchronous request value, as CASResponse
) | ||
| Arguments | |||
String key | Document ID used to identify the value | ||
long casunique | Unique value used to verify a key/value combination | ||
Object value | Value to be stored | ||
Transcoder<T> transcoder | Transcoder class to be used to serialize value | ||
Performs an asynchronous CAS operation on the given key/value using a custom transcoder. The example below shows the update of an existing value using a custom Integer transcoder.
Transcoder<Integer> tc = new IntegerTranscoder(); Future<CASResponse> future = client.asyncCAS("someKey", casvalue, 1200, tc); CASResponse casr; try { casr = future.get(5, TimeUnit.SECONDS); } catch(TimeoutException e) { future.cancel(false); }
| API Call | client.asyncCAS(key, casunique, expiry, value, transcoder) | ||
| Asynchronous | yes | ||
| Description | Compare and set a value providing the supplied CAS key matches | ||
| Returns | Future<CASResponse> (
Asynchronous request value, as CASResponse
) | ||
| Arguments | |||
String key | Document ID used to identify the value | ||
long casunique | Unique value used to verify a key/value combination | ||
int expiry | Expiry time for key. Values larger than 30*24*60*60 seconds (30 days) are interpreted as absolute times (from the epoch). | ||
Object value | Value to be stored | ||
Transcoder<T> transcoder | Transcoder class to be used to serialize value | ||
The final form of the asyncCAS() method
supports a custom transcoder and setting the associated expiry
value. For example, to update a value and set the expiry to 60
seconds:
Transcoder<Integer> tc = new IntegerTranscoder(); Future<CASResponse> future = client.asyncCAS("someKey", casvalue, 60, 1200, tc); CASResponse casr; try { casr = future.get(5, TimeUnit.SECONDS); } catch(TimeoutException e) { future.cancel(false); }