X

Redis for .NET Developers – Redis Sets Datatype

Redis

Redis Sets Datatype are similar in C# world as HashSet, they are an unordered collection used for storing strings. One of the great benefit of Redis Sets is that the operation of add, remove, and testing for existence of items is of constant time of O(1) regardless of the number of items in the Set. An important thing about Sets is when adding the same element multiple times, it will only result in a set having a single copy of the item. In a way, one does not have to check if an item exist or not before adding. The amount of members you can store in a Set is more that 4 billion, thus one can store quite a few things in them. Sets are ideal candidate for set theory related items like unique number of people visiting a site, tagging of items, etc.

Redis Sets Datatype – Operations

  • SADD: Adds one or more elements to the Set O(N).
  • SPOP: Removes and returns a random element from the set O(1).
  • SREM: Removes and returns the specified elements from the set O(N).
  • SCARD: Gets the number of elements in a Set O(1).
  • SDIFF: Gets the list of elements from the first set after subtracting its elements from the other mentioned sets O(N).
  • SDIFFSTORE: Create or override a set, getting the list of elements from the first set after subtracting its elements from the other mentioned sets O(N).
  • SINTER: Gets the common elements in all the sets mentioned O(N * M).
  • SINTERSTORE: Store the elements in a set after intersection of all specified sets similar to SINTER but results are stored in the mentioned set O(N * M).
  • SISMEMBER: Finds if the value is a member of set O(1).
  • SMOVE: Moves members from one set to another set O(1).
  • SRANDMEMBER: Gets one or multiple random members from the set O(N).
  • SUNION: Adds multiple sets O(N).
  • SUNIONSTORE: Adds multiple sets and stores the result in a set O(N).

C# code using Redis Set Datatype

      class Program
    {
        static void Main(string[] args)
        {
            var redis = RedisStore.RedisCache;

            RedisKey key = "setKey";
            RedisKey alphaKey = "alphaKey";
            RedisKey numKey = "numberKey";
            RedisKey destinationKey = "destKey";


            redis.KeyDelete(key, CommandFlags.FireAndForget);
            redis.KeyDelete(alphaKey, CommandFlags.FireAndForget);
            redis.KeyDelete(numKey, CommandFlags.FireAndForget);
            redis.KeyDelete(destinationKey, CommandFlags.FireAndForget);


            //add 10 items to the set
            for (int i = 1; i  (RedisValue) x.ToString()).ToArray());
            redis.SetAdd(numKey, "123".Select(x => (RedisValue)x.ToString()).ToArray());

            var values = redis.SetCombine(SetOperation.Union, numKey, alphaKey);

            Console.WriteLine(string.Join(",", values)); //unordered list of items (e.g output can be "1,3,2,a,c,b")
            
            values = redis.SetCombine(SetOperation.Difference, key, numKey);

            Console.WriteLine(string.Join(",", values)); //4, 6, 7, 8, 9, 10

            values = redis.SetCombine(SetOperation.Intersect, key, numKey);

            Console.WriteLine(string.Join(",", values)); //1, 2, 3
         
            //move a random from source numKey to aplhaKey 
            redis.SetMove(numKey, alphaKey, 2);

            members = redis.SetMembers(alphaKey);

            Console.WriteLine(string.Join(",", members)); //output can be (b, c, a, 2)

            //Add apple to 
            redis.SetAdd(alphaKey, "apple");

            //look for item that starts with he
            var patternMatchValues = redis.SetScan(alphaKey, "ap*"); //output apple

            Console.WriteLine(string.Join(",", patternMatchValues));

            patternMatchValues = redis.SetScan(alphaKey, "a*"); //output apple, a

            Console.WriteLine(string.Join(",", patternMatchValues));
            

            //store into destinantion key the union of numKey and alphaKey
            redis.SetCombineAndStore(SetOperation.Union, destinationKey, numKey, alphaKey); 

            Console.WriteLine(string.Join(",", redis.SetMembers(destinationKey)));

            //radmon value and removes it from the set
            var randomVal = redis.SetPop(numKey);

            Console.WriteLine(randomVal); //random member in the set

            Console.ReadKey();
        }
    }
  

So this covers the basic usage of Redish Set Datatype, in the next blog post I will cover using Sorted Sets Datatype.

For the code please visit
https://github.com/taswar/RedisForNetDevelopers/tree/master/6.RedisSet

For previous Redis topics

  1. Intro to Redis for .NET Developers
  2. Redis for .NET Developer – Connecting with C#
  3. Redis for .NET Developer – String Datatype
  4. Redis for .NET Developer – String Datatype part 2
  5. Redis for .NET Developer – Hash Datatype
  6. Redis for .NET Developer – List Datatype
Categories: Redis
Taswar Bhatti:
Related Post