
For example, if an I/O exception occurred during the following call, then the value of getResult would be null.
{code:java}
var getResult = client.Get("SomeKey");
{code}
Similarly, if the key already existed when the following snippet executes, the value of storeResult would simply be false.
{code:java}
var storeResult = client.Store(StoreMode.Add, "ExistingKey", "SomeValue");
{code}
In summary, for the standard API, retrieve operations will return null for cache misses, I/O exceptions and all other error conditions. Store operations will return false for key errors and I/O exceptions. No exceptions should bubble up to the caller when using the standard API.
In an effort to expose more about an operation's success or failure, additions to the API were introduced starting in client version 1.1. These new methods mirror the existing (while maintaining backwards compatibility), but are prefixed with "Execute." The return value of each of these methods is an instance of an IOperationResult implementation.
With the new API, a caller may use ExecuteGet to learn more about the state of an operation.
{code:java}
var getResult = client.ExecuteGet("foo");
//some examples of how properties are set on the results follow
//sample is not meant to imply flow control for your application
//check the success of the operation
if (getResult.Success) {
Console.WriteLine("Get result successful and the value was {0}", getResult.Value);
}
//exceptions will typically be I/O related and are not always present for a failure
if (getResult.Exception != null) {
Console.WriteLine("Exception occurred: {0}", getResult.Exception.Message);
}
//Check if the nullable status code has a value,
//on a cache miss, for example, this value will be 1
if (getResult.StatusCode != null && getResult.HasValue) {
Console.WriteLine("Server sent StatusCode {0}", getResult.StatusCode.Value);
}
//Check the message (note - the constants will be released in v1.1.7)
//success would also be false, but no exception
//StatusCode would also be equal to (int)StatusCodeEnums.NotFound
if (getResult.Message == StatusCodeMessages.NOT_FOUND) {
Console.WriteLine("Key not found");
}
{code}
In summary:
||Property||Description||
| Success | false when key is not found or exception is thrown |
| Exception | not null when a handled I/O exception is swallowed, null on operation failures |
| StatusCode | not null when server returns valid status code, null on I/O exceptions |
| Message | null when server returns valid results, not null on I/O exceptions or operation failures |
Similarly, Store operations return StoreOperationResult instances.
{code:java}
var storeResult = client.ExecuteStore(StoreMode.Add, "foo", "bar");
//some examples of how properties are set on the results follow
//again, sample is not meant to imply flow control for your application
//check the success of the operation
if (storeResult.Success) {
Console.WriteLine("Store result successful and the cas value was {0}", storeResult.Cas);
}
//exceptions will typically be I/O related and are not always present for a failure
if (storeResult.Exception != null) {
Console.WriteLine("Exception occurred: {0}", storeResult.Exception.Message);
}
//Check if the nullable status code has a value,
//when trying to replace an non-existing key, for example, this value will be 1
if (storeResult.StatusCode != null && getResult.HasValue) {
Console.WriteLine("Server sent StatusCode {0}", getResult.StatusCode.Value);
}
//Check the message (note - the constants will be released in v1.1.7)
//success would also be false, but no exception
//StatusCode would also be equal to (int)StatusCodeEnums.NotFound
if (storeResult.Message == StatusCodeMessages.NOT_FOUND) {
Console.WriteLine("Key not found");
}
{code}
Generally speaking, testing the Success property is the starting point for checking for any failures. Success should never be false if an operation succeeded on the server (status code is 0). The various properties described above could then be used to test for swallowed exceptions or failed operations that didn't raise an exception.
Also, please note that there were missing code paths in the current version of the Enyim.Caching client that was released with the .NET 1.1 library. Specifically, operation failures that should report a StatusCode were falling through to a "Node not found condition." An upcoming 1.1.7 release will patch Enyim and resolve this issue - also reducing (or eliminating) the need for the InnerResult structure that currently is in place.