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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
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 <= 10; i++) redis.SetAdd(key, i); var members = redis.SetMembers(key); Console.WriteLine(string.Join(",", members)); //output 1,2,3,4,5,6,7,8,9,10 //remove 5th element redis.SetRemove(key, 5); Console.WriteLine(redis.SetContains(key, 5)); //False members = redis.SetMembers(key); Console.WriteLine(string.Join(",", members)); //output 1,2,3,4,6,7,8,9,10 Console.WriteLine(redis.SetContains(key, 10)); //True //number of elements Console.WriteLine(redis.SetLength(key)); //output 9 //add alphabets to set redis.SetAdd(alphaKey, "abc".Select(x => (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
- Intro to Redis for .NET Developers
- Redis for .NET Developer – Connecting with C#
- Redis for .NET Developer – String Datatype
- Redis for .NET Developer – String Datatype part 2
- Redis for .NET Developer – Hash Datatype
- Redis for .NET Developer – List Datatype
Leave A Comment