In this recent blog article, we learned about Redis' six data types. Redis Lists contain a collection of strings, sorted by the same order in which they were added. This article will expand on the List data type and highlight some of the main commands for managing them.
List Performance
In Redis, it's important to note that Lists are implemented as linked lists. A linked list is one whose nodes contain a data field as well as a "next" reference (link) to the next node in the list:
This has some important implications regarding performance. It is fast to add elements to the head and tail of the List but it's slower to search for elements within the List as we do not have indexed access to the elements (like we do in an array).
Creating a List
A List is created by using a Redis command that pushes data followed by a key name. There are two commands that we can use: RPUSH and LPUSH. If the key doesn't exist, these commands will return a new List with the passed arguments as elements. If the key already exists or it is not a List, an error is returned.
RPUSH
RPUSH inserts a new element at the end of the List (at the tail):
RPUSH key value [value ...]
Let's create a "guitars" key that represents a List of guitar brands:
RPUSH guitars "Jackson" // 1 RPUSH guitars "Fender" // 2 RPUSH guitars "Gibson" // 3
Each time we insert an element, Redis replies with the length of the List after that insertion. After the above three statements, the guitars should contain the following three elements:
Jackson Fender Gibson
LPUSH
LPUSH behaves the same as RPUSH except that it inserts the element at the front of the List (at the header):
LPUSH key value [value ...]
We can use LPUSH to insert a new value at the front of the guitars list as follows:
LPUSH guitars "Ibanez" //4
We now have four guitars, starting with "Ibanez":
Ibanez Jackson Fender Gibson
Creating a List in Navicat
In the Navicat for Redis Editor, list values are represented as Elements. Clicking on the ellipsis [...] button on the right of the Element opens a special Editor where you can enter individual list elements:
Clicking the Apply button adds the new list or element.
Once added, an element's position in the list may be changed using the up and down arrow buttons.
Fetching List Items using LRANGE
LRANGE returns a subset of the List based on a specified start and stop index:
LRANGE key start stop
We can see the full List by supplying 0 and -1 for the start and stop indexes:
LRANGE guitars 0 -1 //returns Ibanez Jackson Fender Gibson
Meanwhile, the following command retrieves the first two guitars:
LRANGE guitars 0 1 //returns Ibanez Jackson
Removing Elements from a List
LPOP removes and returns the first element of the List while RPOP removes and returns the last element of the List. Here are some examples:
LPOP guitars //returns Ibanez RPOP guitars //returns Gibson
In the Navicat Editor, we can remove any List element by selecting it and clicking the Delete [-] button located under the Element values:
Conclusion
This blog article highlighted some of the main commands for managing Lists in Redis, both via the redis-cli and using Navicat for Redis.
Interested in giving Navicat for Redis a try. Download it here. The trial version is fully functional for 14 days.