Redis List Datatype are similar in C# world as LinkeList e.g LinkedList
Redis List Datatype – Operations
- LPUSH: Prepends the values to the left of the list O(1).
- RPUSH: Prepends the values to the right of the list O(1).
- LPUSHX: Prepends the values to the left of the list if key exist O(1).
- RPUSHX: Prepends the values to the right of the list if key exist O(1).
- LINSERT: Inserts a value in the list after position calculated from left O(N).
- LSET: Sets the value of an element in a list based on index O(N).
- LRANGE: Gets the sub list of elements based on start and end position O(S+E).
- LTRIM: Deletes the elements outside the range specified O(N), where N is the length.
- RPOP: Removes the last element O(1).
- LREM: Removes the element at the index point O(N) , where N is the length.
- LPOP: Removes the first element of the list O(1).
- LINDEX: Gets the element from the list based on the index O(N) where N is the length of traverse.
- LLEN: This command gets the length of the list O(1).
- RPOPLPUSH: Operates on two lists source and destination list, takes the last element on the source list and push it to the first element of the destination list O(1).
C# code using Redis List 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 |
class Program { static void Main(string[] args) { var redis = RedisStore.RedisCache; var listKey = "listKey"; redis.KeyDelete(listKey, CommandFlags.FireAndForget); redis.ListRightPush(listKey, "a"); var len = redis.ListLength(listKey); Console.WriteLine(len); //output is 1 redis.ListRightPush(listKey, "b"); Console.WriteLine(redis.ListLength(listKey)); //putput is 2 //lets clear it out redis.KeyDelete(listKey, CommandFlags.FireAndForget); redis.ListRightPush(listKey, "abcdefghijklmnopqrstuvwxyz".Select(x => (RedisValue) x.ToString()).ToArray()); Console.WriteLine(redis.ListLength(listKey)); //output is 26 Console.WriteLine(string.Concat(redis.ListRange(listKey))); //output is abcdefghijklmnopqrstuvwxyz var lastFive = redis.ListRange(listKey, -5); Console.WriteLine(string.Concat(lastFive)); //output vwxyz var firstFive = redis.ListRange(listKey, 0, 4); Console.WriteLine(string.Concat(firstFive)); //output abcde redis.ListTrim(listKey, 0, 1); Console.WriteLine(string.Concat(redis.ListRange(listKey))); //output ab //lets clear it out redis.KeyDelete(listKey, CommandFlags.FireAndForget); redis.ListRightPush(listKey, "abcdefghijklmnopqrstuvwxyz".Select(x => (RedisValue)x.ToString()).ToArray()); var firstElement = redis.ListLeftPop(listKey); Console.WriteLine(firstElement); //output a, list is now bcdefghijklmnopqrstuvwxyz var lastElement = redis.ListRightPop(listKey); Console.WriteLine(lastElement); //output z, list is now bcdefghijklmnopqrstuvwxy redis.ListRemove(listKey, "c"); Console.WriteLine(string.Concat(redis.ListRange(listKey))); //output is bdefghijklmnopqrstuvwxy redis.ListSetByIndex(listKey, 1, "c"); Console.WriteLine(string.Concat(redis.ListRange(listKey))); //output is bcefghijklmnopqrstuvwxy var thirdItem = redis.ListGetByIndex(listKey, 3); Console.WriteLine(thirdItem); //output f //lets clear it out var destinationKey = "destinationList"; redis.KeyDelete(listKey, CommandFlags.FireAndForget); redis.KeyDelete(destinationKey, CommandFlags.FireAndForget); redis.ListRightPush(listKey, "abcdefghijklmnopqrstuvwxyz".Select(x => (RedisValue)x.ToString()).ToArray()); var listLength = redis.ListLength(listKey); for (var i = 0; i < listLength ; i++) { var val = redis.ListRightPopLeftPush(listKey, destinationKey); Console.Write(val); //output zyxwvutsrqponmlkjihgfedcba } Console.ReadKey(); } } |
So this covers the basic usage of Redish List Datatype, in the next blog post I will cover using Sets Datatype.
For the code please visit
https://github.com/taswar/RedisForNetDevelopers/tree/master/5.RedisList/RedisList
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
Hi. this is good blog and ,this is good tutorial on Redis for .net Developers.
where can get a complete documentation ,beginner to advance about Redis for .net Developer
thanks
Thanks I intend to release a free ebook for all the redis items I have here probably you will be able to download from here in 2019. Will update you once I have it, also feel free to join my mailing list I will update everyone on my mailing list.