-
Notifications
You must be signed in to change notification settings - Fork 1
Getting results
When a command is executed, a IRedisResults is obtained. It is essentially a collection of IRedisResultInspector that are used to inspect the return of each single statement in the command.
Redis only returns errors, strings, integers and arrays of string and/or integers, so it is important to know your Redis commands to know which response type are you expecting.
- Each statement correlates to a position in the
IRedisResultsitems. First statement is item 0, and so on. - The
.RedisTypeproperty indicates the result type.
- If the result is an error, accessing the statement result will throw a
RedisClientCommandExceptionwith the details of the Redis error. It is possible to get the exception without throwing it by using.GetException().
var result = channel.Execute(@"incrby examplekey");
if(result[0].RedisType == RedisType.Error)
{
var error = result[0].GetException();
// do something with the error...
}- If you are expecting the command to return just
OK, you can just assert the result:
var result = channel.Execute(@"set examplekey 1");
result[0].AssertOK();- If you do not care about the individual results and you only want to ensure no errors were returned, you can use another helper method named
.ThrowErrorIfAny(). If errors are found, aRedisClientMultipleCommandExceptionwill be thrown:
var result = channel.Execute(@"set examplekey 1");
result.ThrowErrorIfAny();-
.GetXXXmethods will try to read the value asXXXtype, and will throw anRedisClientCastExceptionif the data is not in the expected type.
var result = channel.Execute(@"
set examplekey 0
incr examplekey");
var ivalue = result[1].GetInteger(); // gets 1
var svalue = result[1].GetString(); // throws RedisClientCastException, because INCR always returns a integer-
.GetXXXArray()methods can be used to read a result that is a list of values:
var result = channel.Execute("hmget myhash Id Name Surname");
var value = result[0].GetStringArray(); // gets a String[3]-
.AsXXXmethods will try to read the value asXXXtype, or parse it asXXX(there is no.GetDouble()because Redis only returns string, integer or error, but there is a.AsDouble().
var result = channel.Execute(@"
set examplekey 0
incrbyfloat examplekey 1.0");
var ivalue = result[1].AsInteger(); // gets 1
var svalue = result[1].AsString(); // gets "1.0"
var dvalue = result[1].AsDouble(); // gets 1.0D-
.AsResults()method: will expand a single result as another collection ofIRedisResultInspector. When using LUA scripts (see "procedures"), the result of a script execution may be multiple results coming from multiple commands that have been executed in the script.
// Execute procedure
var result = channel.Execute("ZPaginate @key @page @items",
new { key = "products:bydate", page=3, items=10 });
// Expand result of the first line as a collection of results
var hashes = result[0].AsResults();
// Bind each hash to an object
// Where <Product> is a class with properties that match the hash keys.
var products = hashes.Select(h => h.AsObjectCollation<Product>()).ToArray();-
.AsEnum()can parse a value and returns the enum value.
var result = channel.Execute(@"hget examplekey Status");
var val = result[0].AsEnum<Status>();.AsObjectCollation<T>() allows to bind the result to an object by parsing a sequence of key-value pairs, and bind it to the object properties. Only properties with integral types, String, DateTime, enums and their Nullable<> counterparts are supported.
For example member1 value1 member2 value2 will be bound as { member1 = "value1", member2 = "value2" }. By default, it will ignore missing properties and property type mismatches, but these are configurable through two optional parameters named ignoreMissingMembersand ignoreTypeMismatchMembers respectively.
var result = channel.Execute("hgetall @customer",
new { customer = "customer:" + customerId });
var obj = results[1].AsObjectCollation<Customer>();-
.AsDictionaryCollation<TKey, TValue>()allows to bind the result to an object by parsing a sequence of key-value pairs asKeyValuePair<>.
var result = channel.Execute("hgetall @customer",
new { customer = "customer:" + customerId });
var obj = results[1].AsDictionaryCollation<String, String>();