Redis Hash Datatype for .NET developers
Redis Hash Datatype are similar to Dictionary in C#. Redis Hash datatype is a mapping of key as string and value as a string. Redis hashes are memory optimized.
Thanks for dropping by !!! Feel free to comment to this post or you can also drop me an email at naik899@gmail.com.
The post Redis Hash Datatype for .NET developers appeared first on TechPatch.
var hashKey = “hashKey”;HashEntry[] redisMerchantDetails= {new HashEntry(“Name”, “Ravindra Naik”),new HashEntry(“Age”, 26),new HashEntry(“Profession”, “Software Engineer”)};redis.HashSet(hashKey, redisMerchantDetails);if (redis.HashExists(hashKey, “Age”)){var age= redis.HashGet(hashKey, “Age”); //Age is 26}var allHash = redis.HashGetAll(hashKey);//get all the itemsforeach (var item in allHash){//output//key: Name, value: Ravindra Naik//key: Age , value: 26//key: Profession, value: Software EngineerConsole.WriteLine(string.Format(“key : {0}, value : {1}”, item.Name, item.Value));}//get all the valuesvar values = redis.HashValues(hashKey);foreach (var val in values){Console.WriteLine(val); //result =Ravindra Naik, 26, Software Engineer}//get all the keysvar keys = redis.HashKeys(hashKey);foreach (var k in keys){Console.WriteLine(k); //result = Name, Age, Profession}var len = redis.HashLength(hashKey); //result of len is 3if (redis.HashExists(hashKey, “Age”)){var age= redis.HashIncrement(hashKey, “Age”, 1); //year now becomes 27var age2= redis.HashDecrement(hashKey, “Age”, 1.5); //year now becomes 25.5}
- Here is how you can save, retrieve, update, increment and decrement the value.
- HashExists takes O(1) for checking the existence of a field, HashGetAll takes O(n) to get all n values of hash key and O(1) if n is small.
Thanks for dropping by !!! Feel free to comment to this post or you can also drop me an email at naik899@gmail.com.
The post Redis Hash Datatype for .NET developers appeared first on TechPatch.
Comments
Post a Comment