Compare Exchange in Cluster-Wide Session
-
Compare-Exchange items can be created and managed on the advanced session (
Session.Advanced
)
Other options are listed in this compare-exchange overview. -
When working with compare-exchange items from the session,
the session must be opened as a cluster-wide session. -
In this page:
Create compare-exchange
Example
- Sync
- Async
// The session must be first opened with cluster-wide mode
session.Advanced.ClusterTransaction.CreateCompareExchangeValue(
key: "Best NoSQL Transactional Database",
value: "RavenDB"
);
session.SaveChanges();
// The session must be first opened with cluster-wide mode
session.Advanced.ClusterTransaction.CreateCompareExchangeValue(
key: "Best NoSQL Transactional Database",
value: "RavenDB"
);
await session.SaveChangesAsync();
SaveChanges()
throws aConcurrencyException
if the key already exists.- An
InvalidOperationException
exception is thrown if the session was Not opened as cluster-wide.
Syntax
- Sync
- Async
session.Advanced.ClusterTransaction.CreateCompareExchangeValue<T>(key, value);
session.Advanced.ClusterTransaction.CreateCompareExchangeValue<T>(key, value);
Parameters | Type | Description |
---|---|---|
key | string | The compare-exchange item key. This string can be up to 512 bytes. |
value | T | The associated value to store for the key |
Return Value | Description |
---|---|
CompareExchangeValue<T> | The new compare-exchange item is returned |
The CompareExchangeValue
Parameters | Type | Description |
---|---|---|
key | string | The compare-exchange item key. This string can be up to 512 bytes. |
value | T | The value associated with the key |
index | long | Index for concurrency control |
Get compare-exchange
Get single value
- Sync
- Async
session.Advanced.ClusterTransaction.GetCompareExchangeValue<T>(key);
await session.Advanced.ClusterTransaction.GetCompareExchangeValueAsync<T>(key);
Parameters | Type | Description |
---|---|---|
key | string | The key to retrieve |
Return Value | Description |
---|---|
CompareExchangeValue<T> | If the key doesn't exist it will return null |
Get multiple values
- Sync
- Async
session.Advanced.ClusterTransaction.GetCompareExchangeValues<T>(keys);
await session.Advanced.ClusterTransaction.GetCompareExchangeValuesAsync<T>(keys);
Parameters | Type | Description |
---|---|---|
keys | string[] | Array of keys to retrieve |
Return Value | Description |
---|---|
Dictionary<string, CompareExchangeValue<T>> | If a key doesn't exists the associate value will be null |
Get compare-exchange lazily
- Sync
- Async
// Single value
session.Advanced.ClusterTransaction.Lazily.GetCompareExchangeValue<T>(key);
// Multiple values
session.Advanced.ClusterTransaction.Lazily.GetCompareExchangeValues<T>(keys);
// Single value
session.Advanced.ClusterTransaction.Lazily.GetCompareExchangeValueAsync<T>(key);
// Multiple values
session.Advanced.ClusterTransaction.Lazily.GetCompareExchangeValuesAsync<T>(keys);
Parameters | Type | Description |
---|---|---|
key | string | The key to retrieve |
keys | string[] | Array of keys to retrieve |
Return Value | Description |
---|---|
Lazy<CompareExchangeValue<T>> | If the key doesn't exist it will return null |
Lazy<Dictionary<string, CompareExchangeValue<T>>> | If a key doesn't exists the associate value will be null |
Delete compare-exchange
- Sync
- Async
// Delete by key & index
session.Advanced.ClusterTransaction.DeleteCompareExchangeValue(key, index);
// Delete by compare-exchange item
session.Advanced.ClusterTransaction.DeleteCompareExchangeValue<T>(item);
// Delete by key & index
session.Advanced.ClusterTransaction.DeleteCompareExchangeValue(key, index);
// Delete by compare-exchange item
session.Advanced.ClusterTransaction.DeleteCompareExchangeValue<T>(item);
Parameters | Type | Description |
---|---|---|
key | string | The key of the compare-exchange item to delete |
index | long | The index of this compare-exchange item |
item | CompareExchangeValue<T> | The compare-exchange item to delete |