Using Radis Hashes .NET

Redis Hashes are essentially maps between string fields and string values. Here I have published on how to get started using Redis with .NET. Once you have installed StackExchange.Redis and have setup console script as per above link, here are the things to do for using Redis Hashes with .NET.
  • Create a new visual studio console project. Then right click select on Manage Nu-Get packages and select online option and search for “StackExchange” in the search field and install StackExchange.Redis.
  • Now to get connected to Redis and its database here is a snippet.   // This should be stored and reused
    var redis = ConnectionMultiplexer.Connect(“localhost”); // IDatabase is simple object which is cheap to build
    IDatabase db = redis.GetDatabase();
  • Lets create a data object.
          var readObject = new ReadObject();
readObject._id = “3”; readObject.Name =”Ravi”;
readObject.Age = “26”; readObject.Address = “Bangalore, India”;
  • Now lets convert this object into Hash entry list.
var propertyList = ConvertToHashEntryList(readObject);
  • Now save the object into redis hash
db.HashSet(“object-“+ readObject._id, propertyList.toArray());
  • Now if you want to fetch all keys stored against a particular hash, here is what you should do
var hashEntries = db.HashGetAll(“object-“+ readObject._id);
  • Now if you want to update any particular key of the hash field:
db.HashSet(“object-3”, new []{new HashEntry(“Age”, “25”) });

This is how you can save, retrieve and update the data in Hashes of Redis using .NET. Using hashes with StackExchange.Redis is little pain but if you want to build flexible framework around it then hashes are worth the pain.

Thanks for dropping by!! Feel free to comment to this post or you can alternatively drop me an email at naik899@gmail.com.



The post Using Radis Hashes .NET appeared first on TechPatch.

Comments

Popular posts from this blog

Serve GZip CSS and JS from AWS S3

Create a Central Logging System

Create a calendar invite (ics file) in ASP NET