Skip to content
Visit booth 3171 at Google Cloud Next to see how to unlock real-time decisions at scaleMore info

Asynchronous write

Use the Aerospike C# Client APIs to asynchronously write to the Aerospike database.

Write a Single Value

The AsyncClient method takes an extra WriteHandler listener parameter that notifies the caller on command success or failure:

// Write a single value.
Key key = new Key("test", "myset", "mykey");
Bin bin = new Bin("mybin", "myvalue");
client.Put(policy, new WriteHandler(this, key, bin), key, bin);

For the read to wait until the write completes, the RecordHandler read initiates within theWriteHandler listener onSuccess() method.

private class WriteHandler : WriteListener
{
private readonly AsyncTest parent;
private readonly Key key;
private readonly Bin bin;
public WriteHandler(AsyncTest parent, Key key, Bin bin)
{
this.parent = parent;
this.key = key;
this.bin = bin;
}
public void OnSuccess(Key key)
{
try
{
// Write succeeded. Now call read.
parent.client.Get(parent.policy, new RecordHandler(parent, key), key);
}
catch (Exception e)
{
Console.WriteLine(string.Format("Failed to get: namespace={0} set={1} key={2} exception={3}",
key.ns, key.setName, key.userKey, e.Message));
}
}
public void OnFailure(AerospikeException e)
{
Console.WriteLine("Failed to put: namespace={0} set={1} key={2} exception={3}",
key.ns, key.setName, key.userKey, e.Message);
parent.NotifyCompleted();
}
}
Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?