Definition of Redis LRANGE
Redis LRANGE is a command that returns elements or values stored at the key in the specified range of the lists. It allows you to specify a start and stop position for the elements to return. The start and stop offsets are zero-based indexes; hence the first and second elements of the list are 0 and 1, respectively, and so on. LRANGE also allows negative indexes which retrieve elements from the tail of the lists, where the last and second last elements are -1 and -2, respectively, and so on.
- LRANGE is a command used in Redis Lists.
- LRANGE displays a specific list of elements stored at the key based on the range specified.
- The offsets start and stop are zero-based indexes; therefore, the first element is always represented as 0.
- LRANGE allows negative indexing where -1 denotes the last element and -2 denotes the second last element of the list, and so on.
How to Use Redis LRANGE?
The below syntax supports Redis LRANGE.
- The KEY_NAME is the name of the specific key where the elements are stored.
- The START denotes the first element you want to return.
- The STOP denotes the last element you want to return.
Before we start moving to the core, please install Redis in your terminal for better understanding and practicing purposes.
Redis LRANGE Command Explained
We know that elements in the lists are stored in an ordered sequence.
LRANGE command helps to retrieve the elements of the list by specifying the start and stop range without affecting the sequence. The LRANGE command displays elements based on the specified range and also supports negative indexing of the elements in the lists.
a) Displaying a Specific Set of Elements
- First, we created the lists by adding various elements to the key eduCBA.
- The RPUSH command adds elements to the right of the list.
- The LRANGE command with the specified range of 0 to 2 returns the elements.
- Where redis is 0, the tutorial is 1, and the guide is 2.
b) Displaying all the Elements
- In this example, the command range of 0 to -1 denotes the first element (redis) and the last element (students), respectively.
- Hence all the elements from 0 to -1 are returned.
c) Using Negative Indexes
- Here the range 0 denotes the first element (redis), and -2 denotes the second last element (guide) of the list.
- Hence the command returns all the elements from 0 to -2 of the list.
d) Using out-of-range indexes
1. STOP > size of the list
In the below example, the stop index is 8, which is greater than the size of the list; hence it is considered the last element, and the command returns the elements of the list.
2. START > size of the list
In the below example, the start position is 5, which is greater than the size of the list; hence the command returns an empty list.
Since the Redis LRANGE command is used in List to retrieve data, It finds its application in retrieving desired data from a database consisting of ample data.
Q1. What is Redis LRANGE?
Answer: LRANGE is a Redis list command that retrieves the list of elements based on the start and stop offsets. Both the start and stop offsets are zero-based indexes where 0 represents the first element, and -1 represents the last element.
Q2. How LRANGE handles out-of-range offsets?
Answer: The out-of-range offsets are handled in two ways. If the start offset is greater than the size of the list, then the command returns an empty list. In contrast, If the stop offset is greater than the size of the list, then the last element is considered the stop offset, and the command returns the elements.
Q3. Does Redis LRANGE allow Negative indexing?
Answer: Yes, LRANGE supports negative indexing and displays data from the tail of the list. Where -1 represents the last element and -2 represents the second last element, and so on.
This article explains everything about Redis LRANGE. To know more about related topics, visit the following links: