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

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

  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