Redis List Datatype are similar in C# world as LinkeList e.g LinkedList. Just like in LinkedList in C#, Redis can store items at Head or Tail of a List. The nice thing about them is that the insertion speed is just O(1), but do note that if you have a large list, the reading speed goes down since it sequentially traverses through the items (i.e walking down the chain). One could also say its an advantage since Redis was made to write faster than read, and LinkedList are great data type for such things. List are ideal candidate for logs, since the write speed is more important than read when dealing with logs.
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
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
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
View Comments (2)
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.