Redis

Using Redis List

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:

RPUSH key value1 value2 value3…valueN
LPUSH key value1 value2 value3…valueN

For example, we create a list that holds databases as shown:

127.0.0.1:6379> RPUSH databases MySQL PostgreSQL Cassandra
(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:

LRANGE key start_offset stop_offset

For example, to get the elements from index 0 to index 3, we can do:

127.0.0.1:6379> LRANGE databases 0 3
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:

127.0.0.1:6379> LRANGE databases 0 -1
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:

127.0.0.1:6379> LINDEX databases 0
"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:

LLEN key

An example command usage is as shown below:

127.0.0.1:6379> LLEN databases
(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:

127.0.0.1:6379> LPOP databases
"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.

127.0.0.1:6379> RPOP databases
"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.

127.0.0.1:6379> LPOP databases 3
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:

127.0.0.1:6379> DEL databases
(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!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list