Redis provides us with a list data structure to store a collection of items. It is closely similar to a typical array in most programming languages. Once you add an element to a list, Redis will assign an index value starting at index 0.
Let us learn how we can use and work with lists in Redis.
Requirements:
This tutorial assumes you have the latest version of the Redis server installed and configured in your system.
We recommend using the Redis CLI to get maximum compatibility and similar output.
Redis Create List
We use the RPUSH, and LPUSH commands to create a list in Redis. Redis uses the concept of head and tail to add elements to the list.
RPUSH adds an element to the right of the list, while the LPUSH command adds an element to the left.
The syntax of the commands is as shown:
LPUSH key value1 value2 value3…valueN
For example, we create a list that holds databases as shown:
(integer) 3
The command above will create a new list called databases and add three elements. It returns an integer value denoting how many elements were added to the list.
The same case applies to the LPUSH command.
Redis Get List Items
To get elements in a Redis, use the LRANGE command. This command takes the name of the list and the index range of the element you wish to access.
The syntax is as shown below:
For example, to get the elements from index 0 to index 3, we can do:
1) "MySQL"
2) "PostgreSQL"
3) "Cassandra"
The command should return the values of the elements in the specified range.
NOTE: In Redis, indexes start at 0. Hence, the first element in the list is at index 0.
If you want to get all elements in a list, we can use negative indexing. For example, if you access elements from index 0 to -1, Redis will return all elements in the list.
An example is as shown:
1) "MySQL"
2) "PostgreSQL"
3) "Cassandra"
You can also access the item at a specific index using the LINDEX command. An example is as shown:
"MySQL"
The command above returns the item stored at index 0 in the specified list.
Redis Get Number of Elements in a List
To get the total number of elements in a list, we can use the LLEN command. The syntax is as shown below:
An example command usage is as shown below:
(integer) 3
The command should return an integer with the number of elements in the list.
Redis Delete List Element
We can use the LPOP, and RPOP commands to remove an item from the list. These commands remove the last element to the left and right of the list, respectively.
Example:
"MySQL"
In this case, the command removes and returns the last element to the left of the list. Simply put, it removes the first element in the list.
To remove the last element in the list, we can use the RPOP command.
"Cassandra"
You can also specify how many elements to the right or left of the list you wish to remove. For example, to remove three items from the left of the list, run the command.
1) "Cassandra"
2) "MySQL"
3) "PostgreSQL"
The command removes and returns the elements removed.
Redis Remove Entire List
If you want to clear an entire list, you can use the DEL command followed by the list name.
Example:
(integer) 1
If the specified list exists in the database, Redis will remove it and return integer 1.
Conclusion
In this article, we covered one of the famous and valuable data types in Redis. Using lists, you can create collection types without incurring heavy memory usage.
Thanks for reading!!