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

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