Updated February 17, 2023
Introduction to Redis LPUSH
Redis LPUSH is used to insert the value specified in the head list and saved it in the key. If the key does not exist, it will create an empty list before performing the LPUSH operations. When a key contains a value that is not in the list, an error is returned. When we use LPUSH, we use the lrange method.
Key Takeaways
- While using redis LPUSH command, it is possible to insert single as well as multiple values in a single command.
- The list is used by Redis LPUSH to store the value. When inserting data into the redis key, we only used the key type as a list.
What is Redis LPUSH?
The redis LPUSH command inserts the specified values that were heading in the value from the specified key. Using redis LPUSH, we can push multiple elements with a single command; we simply need to specify the arguments at the end of the command. Using redis LPUSH, elements will be inserted one by one, with the last element inserted at the top of the list, from leftmost to rightmost. If we insert x, y, and z elements, the output will have z in the first position, y in the second position, and x in the last position.
Syntax:
LPUSH name_of_key val1 …. ValN
We can insert multiple elements of values into a single command by using LPUSH. LPUSH is the command used to insert the values in the syntax above. When using the LPUSH command, we must specify the key that will be used to store the element. When we use the LPUSH command, we only use one key. Value is the parameter that was used to define the value when using the redis LPUSH command.
How to Use Redis LPUSH?
To use it we are using the ubuntu system for installing the redis server. While using the LPUSH we need to install redis server in our system.
1. First, we are installing the redis server in our system. In the below example, we are installing the redis server using the command name as apt as follows.
Code:
# apt install redis-server
# redis-server --version
Output:
2. While using redis LPUSH now in this step we are login into the redis database by using the below command as follows. While executing the LPUSH command we need to login into redis cli by using the redis-cli command as follows.
Code:
# redis-cli
Output:
3. Now in this step we are using the redis LPUSH method to insert the element. In the below example, we are using single key and single value to use the redis LPUSH command.
Code:
LPUSH key_lpush "lpush_val1"
Output:
4. As we can see in the above example, we have used the key name as “key_lpush” and defined value as “lpush_val1”. Now in this step, we are adding a new value to the key name as key_lpush as follows.
Code:
LPUSH key_ lpush " lpush_val2"
Output:
5. While adding the value into the key, now in this step we are using the LRANGE command to retrieve the element which we have defined. The below example shows how we can retrieve the value by using lrange which was defined in LPUSH as follows.
Code:
LRANGE key_lpush 0 -1
Output:
Redis LPUSH Command
The Redis LPUSH command is useful for inserting values because it allows us to insert single and multiple values in the same command. The command has been available since version 1.0.0. After the push operations are completed, the command will return the integer that was responding to the list length. The LPUSH command’s return value type is an integer. Basically, the LPUSH command adds a new element to the list’s left side.
The following example shows how we can use the LPUSH command to insert a single element into the key. In the example below, we can see that we first inserted the val1 element and then the val2 element, but when we retrieve the value from the key using the lrange command, it shows the val2 element first, then the val1 element, because the last inserted element comes first when using the LPUSH command.
Code:
LPUSH redis_lpush "val1"
LPUSH redis_lpush "val2"
LRANGE redis_lpush 0 1
Output:
If we have already created a key with the type string, we cannot add the element by using LPUSH into the specified key. It will return the error that the key has the incorrect value. In the following example, the key contains a string value, and we add an integer value, resulting in an error.
Code:
LPUSH color 1
TYPE color
Output:
We can insert the data into the key when it holds the type as list. In the below example, the key name redis_LPUSH1 is holding the type as list, so we can be able to insert the element into the specified key.
Code:
Type redis_lpush1
LPUSH redis_lpush1 "val"
LRANGE redis_lpush1 0 1
Output:
The below example shows how we can insert multiple elements by using the LPUSH command as follows. In the example below, we can see that it works the same as when inserting a single element. In the below example, we are inserting val1, val2, and val3 elements respectively, but we can see that it will retrieve the element as val3, val2, and val1 respectively.
Code:
LPUSH redis_lpush2 val1 val2 val3
LRANGE redis_lpush2 0 2
Output:
Examples of Redis LPUSH
Given below are the examples mentioned:
Example #1
In the example below, we are inserting a single element. As shown below, we define the key name as color1 and the value as white. We are also retrieving the value using the lrange method.
Code:
LPUSH color1 "white"
LRANGE color1 0 1
Output:
Example #2
In the example below, we are inserting a single element. As shown below, we define the key name as color1 and the value as blue, red, and yellow. We are also retrieving the value using the lrange method.
Code:
LPUSH color1 "blue" "red" "yellow"
LRANGE color1 0 2
LRANGE color1 0 3
Output:
Example #3
In the below example, we are adding two integer elements by using LPUSH.
Code:
LPUSH integer_key 11 13
LRANGE integer_key 0 2
Output:
Conclusion
The redis LPUSH command inserts the specified values that were heading into the value from the specified key. It is possible to push multiple elements with a single command by using it. It is used to insert the value specified in the head list and saved it in the key.
Recommended Articles
This is a guide to Redis LPUSH. Here we discuss the introduction, to how to use redis LPUSH? and examples respectively. You may also have a look at the following articles to learn more –