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.
StoreMode.Add - Add a key to the
database, failing if the key exists
StoreMode.Replace - Replace a key in the
database, failing if the key does not exist
StoreMode.Set - Add a key to the
database, replacing the key if it already exists
| API Call | object.Cas(storemode, key, value) | ||
| Asynchronous | no | ||
| Description | Compare and set a value providing the supplied CAS key matches | ||
| Returns | CasResult<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 Call | object.Cas(storemode, key, value, validfor, casunique) | ||
| Asynchronous | no | ||
| Description | Compare and set a value providing the supplied CAS key matches | ||
| Returns | CasResult<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 Call | object.Cas(storemode, key, value, expiresat, casunique) | ||
| Asynchronous | no | ||
| Description | Compare and set a value providing the supplied CAS key matches | ||
| Returns | CasResult<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 Call | object.ExecuteCas(storemode, key, value) | ||
| Asynchronous | no | ||
| Description | Compare and set a value providing the supplied CAS key matches | ||
| Returns | IStoreOperationResult (
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 Call | object.ExecuteCas(storemode, key, value, validfor, casunique) | ||
| Asynchronous | no | ||
| Description | Compare and set a value providing the supplied CAS key matches | ||
| Returns | IStoreOperationResult (
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 Call | object.ExecuteCas(storemode, key, value, expiresat, casunique) | ||
| Asynchronous | no | ||
| Description | Compare and set a value providing the supplied CAS key matches | ||
| Returns | IStoreOperationResult (
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));