Redis Sets and .NET

3.27 , 3 Comments

What are Redis sets? There is a good definition written in redis.io:
Redis sets are an unordered collection of Strings. It is possible to add, remove, and test for existence of members in sets. 
So set is a collection of strings which are in "random" order. If we map sets into .NET datatype, I would say that its close to List<Tuple<string, string>>. Unlike List, Redis sets doesn't allow repetition of data in a key.

Sets are create way to store related objects and handle collection of data.

Here is list of some set commands converted into .NET equivalent:

Setup code
var setCollection = new List<Tuple<string, string>>();
var redis = ConnectionMultiplexer.Connect("localhost");
var db = redis.GetDatabase(2);

Add (add items into collection vs. database)

setCollection.Add(new Tuple<string, string>("exampleKey", "exampleValue1"));
setCollection.Add(new Tuple<string, string>("exampleKey", "exampleValue2"));
db.SetAdd("exampleKey", "exampleValue1");
db.SetAdd("exampleKey", "exampleValue2"); 
If exampleKey, exampleValue1 were added again, it would still occur only one time in database.

Remove (remove items from collection vs. database)
 setCollection.Remove(new Tuple<string, string>("exampleKey", "exampleValue1"));
 db.SetRemove("exampleKey", "exampleValue1");
Exists (contains value)
var collectionContainsItem = setCollection.Exists(tuple => tuple.Item1 == "exampleKey" && tuple.Item2 == "exampleValue2");
var dbContainsItem = db.SetContains("exampleKey", "exampleValue2");
Union (nice command to fetch combination of two different set with unique values)
// Create new collection which we will union
var setCollection2 = new List<Tuple<string, string>>();
setCollection2.Add(new Tuple<string, string>("exampleKey2", "exampleValue2"));
setCollection2.Add(new Tuple<string, string>("exampleKey2", "exampleValue3")); 
// Add same values into database
db.SetAdd("exampleKey2", "exampleValue2");
db.SetAdd("exampleKey2", "exampleValue3");
           
var unionResult = setCollection.Union(setCollection2, new RedisValueComparer());
// unionResult contains { "exampleKey", "exampleValue2" }, { "exampleKey2", "exampleValue3" }
         
var dbresult = db.SetCombine(SetOperation.Union, "exampleKey", "exampleKey2");
 // dbResult contains { "exampleValue2" }, { "exampleValue3" }
This is used comparer class
public class RedisValueComparer : IEqualityComparer<Tuple<string, string>>
{
    public bool Equals(Tuple<string, string> x, Tuple<string, string> y)
    {
         return x.Item2 == y.Item2;
    }
    public int GetHashCode(Tuple<string, string> obj)
    {
        return obj.Item2.GetHashCode();
    }
}
Full command list is available at Redis command reference page.

The best way to predict the future is to implement it.

3 kommenttia:

  1. Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often
    Web development Company in India Company | Web development Company Bangalore
    Web Development Company in Bangalore | Responsive Web development

    VastaaPoista

  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Online Training from India. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    JavaScript Online Training from India

    VastaaPoista