Search:

Search all manuals
Search this manual
Manual
Couchbase Client Library: .NET (C#) 1.2
Community Wiki and Resources
Wiki: .NET Client Library
Download Client Library
.NET Client Library
Couchbase Developer Guide 2.0
Couchbase Server Manual 2.0
SDK Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
7 Update Operations
Chapter Sections
Chapters

7.7. CAS Methods

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 behavior of Cas and ExecuteCas operations is defined by setting the first parameter to a value from the StoreMode enumeration.

API Callobject.Cas(storemode, key, value)
Asynchronousno
Description Compare and set a value providing the supplied CAS key matches
ReturnsCasResult<bool> ( Cas result of bool )
Arguments 
StoreMode storemode Storage mode for a given key/value pair
string key Document ID used to identify the value
object value Value to be stored

The Cas() method is used to persist new values by key. Any class decorated with the Serializable attribute may be stored. The CAS value is returned by way of a CasResult

var casResult = client.Cas(StoreMode.Add, "beer", new Beer() {
    Brewer = "Thomas Hooker Brewing Company",
    Name = "American Ale"
});

if (casResult.Result)
{
    Console.WriteLine("Cas: ", casResult.Cas);
}
API Callobject.Cas(storemode, key, value, validfor, casunique)
Asynchronousno
Description Compare and set a value providing the supplied CAS key matches
ReturnsCasResult<bool> ( Cas result of bool )
Arguments 
StoreMode storemode Storage mode for a given key/value pair
string key Document ID used to identify the value
object value Value to be stored
TimeSpan validfor Expiry time (in seconds) for key
ulong casunique Unique value used to verify a key/value combination
client.Cas(StoreMode.Set, "beer", new Beer() {
    Brewer = "Peak Organic Brewing Company",
    Name = "IPA"
}, TimeSpan.FromSeconds(60));
API Callobject.Cas(storemode, key, value, expiresat, casunique)
Asynchronousno
Description Compare and set a value providing the supplied CAS key matches
ReturnsCasResult<bool> ( Cas result of bool )
Arguments 
StoreMode storemode Storage mode for a given key/value pair
string key Document ID used to identify the value
object value Value to be stored
DateTime expiresat Explicit expiry time for key
ulong casunique Unique value used to verify a key/value combination
client.Cas(StoreMode.Replace, "beer", new Beer() {
    Brewer = "Six Point Craft Ales",
    Name = "Righteous Rye"
}, DateTime.Now.Addhours(1));
API Callobject.ExecuteCas(storemode, key, value)
Asynchronousno
Description Compare and set a value providing the supplied CAS key matches
ReturnsIStoreOperationResult ( Store operation result )
Arguments 
StoreMode storemode Storage mode for a given key/value pair
string key Document ID used to identify the value
object value Value to be stored

The ExecuteCas() methods are similar to the Cas methods, but return an instance of an IStoreOperationResult.

var result = client.ExecuteCas(StoreMode.Add, "beer", new Beer() {
    Brewer = "Thomas Hooker Brewing Company",
    Name = "American Ale"
});

if (! result.Success)
{
    Console.WriteLine("Store failed with message {0} and status code {1}", result.Message, result.StatusCode);
    
    if (result.Exception != null) 
    {
        throw result.Exception;
    }
}

client.ExecuteCas(StoreMode.Replace"beer", new Beer() {
    Brewer = "Thomas Hooker Brewing Co.",
    Name = "American Ale"
}, result.Cas);
API Callobject.ExecuteCas(storemode, key, value, validfor, casunique)
Asynchronousno
Description Compare and set a value providing the supplied CAS key matches
ReturnsIStoreOperationResult ( Store operation result )
Arguments 
StoreMode storemode Storage mode for a given key/value pair
string key Document ID used to identify the value
object value Value to be stored
TimeSpan validfor Expiry time (in seconds) for key
ulong casunique Unique value used to verify a key/value combination
client.ExecuteCas(StoreMode.Set, "beer", new Beer() {
    Brewer = "Peak Organic Brewing Company",
    Name = "IPA"
}, TimeSpan.FromSeconds(60));
API Callobject.ExecuteCas(storemode, key, value, expiresat, casunique)
Asynchronousno
Description Compare and set a value providing the supplied CAS key matches
ReturnsIStoreOperationResult ( Store operation result )
Arguments 
StoreMode storemode Storage mode for a given key/value pair
string key Document ID used to identify the value
object value Value to be stored
DateTime expiresat Explicit expiry time for key
ulong casunique Unique value used to verify a key/value combination
client.ExecuteCas(StoreMode.Replace, "beer", new Beer() {
    Brewer = "Six Point Craft Ales",
    Name = "Righteous Rye"
}, DateTime.Now.Addhours(1));