Redis lists have 0-based indexing. The first element is designated as the 0th index; the second one is at the 1st index, and so on. Accessing tail elements with the indexes is possible with negative indices like the last element is indexed as -1, the penultimate element is -2, and so on.
The list elements can be set at any given index with the LSET command, which is described in the following section.
The LSET Command
The LSET command sets an element at the given index of a Redis list stored at the specified key. It has the following simple syntax:
list_key: The key of the Redis list.
index: The index where you need to set an element.
element: The string element to be set at the specified index.
This command returns a Simple string “OK” if the element has been set properly. Whenever it encounters an index that is out of the range, the LSET command returns an error.
One of the most common use cases of the Redis lists is to store social media posts and their updates for individual users. The LSET command plays an important role there, as discussed in the following section.
Use Case – Update Social Media Posts
Let’s assume an event where a company maintains its own social media website with thousands of users actively posting posts and statuses. They have identified that the Redis lists are the suitable candidate to hold these posts and statuses where frequent insertion happens, and the Redis lists perform insertion in constant time.
Let’s create a Redis list for a given user id 100 that will be used to store that particular user’s posts and status messages. We will use the LPUSH command to put some dummy posts and status messages for the user id 100.
LPUSH social-media:user:100 "2022-08-05:Started new job at linuxhint"
LPUSH social-media:user:100 "2022-08-06:Great party today"
LPUSH social-media:user:100 "2022-08-07:Bad day!!!"
Assume that the user with user id 100 will update one of his old posts. Suppose the third post that he has posted needs some correction. The LSET command can easily achieve this, as shown in the following.
Since the third element is at index 2 from the Head. We will be updating it as follows:
As expected, the command was executed successfully, and the element at the second index was updated. Let’s recheck the list as follows:
We can do the same by specifying the index from the tail as follows:
As expected, the third element has been set to the new string “Job started at LinuxHINT”.
If you specify an index that is out of range, it will raise an error, as shown in the following:
The LSET command sets a list element at a specified index to a given element of the Redis list stored at a given key. Since the Redis list indexes start from 0, it is the first element. Index 1 is the second element of the list, and so on. As discussed, the indexes of the elements starting from the tail can be specified using negative numbers like -1 is the last element, -2 is the element before the last, and so on. Whenever a non-existing index is passed, the LSET command will return an error. Furthermore, this command returns a simple string output over the successful execution.