Keep in mind that this tutorial is not an introduction to Redis data types. Check our tutorial on the topic to learn more.
We have tested the commands and examples on the latest version of Redis. We recommend replicating a similar environment to get maximum compatibility. For example, commands such as GETRANGE might not work in Redis version 2.0 and lower.
The first range command we will look at is the GETRANGE command. The command takes a key with a string value and returns a substring of the value as determined by start and end indexes.
The syntax of the command is as shown:
Start by creating a simple key and value in a Redis database as shown in the command below:
Next, to get a substring starting from index 3 to index 10, we can do:
Note that the index of the string at index 0 forward. Whitespace characters are also included as valid characters.
You can also negative range to get the last elements in the string. For example, to get the last three indexes, we can do.
The above syntax gets the last three characters in the string value.
You will often come across this command when working with lists in Redis. The LRANGE command returns a specified number of elements in a Redis list.
It takes the key of the list and a start and stop value. Similarly, the indexing of elements in a Redis list starts at index 0.
Start by creating a simple list as shown in the example command below:
The command above creates a Redis list called databases and adds the values specified to it.
You can use the LRANGE command to get elements within a specific range. For example, to get elements from index 0 to 5, we can do.
You will notice that the elements start with the last inserted element to the first. The list is simply a collection of items sorted by the insertion order.
You can also use a negative index to get the last element from a list.
The command returns the elements from the tail of the list.
If you fetch a range where there are no elements available, the command will return an empty list as:
(empty list or set)
The next range command you need to be familiar with is the ZRANGE command. Using the ZRANGE command, you can fetch items within a specific range from a sorted set.
The command takes the sorted set key, a start and stop index value. It returns all the elements within the specified range sorted by the score from the lowest to the highest.
The syntax of the command can be expressed as:
To see how the ZRANGE command works, start by creating a sorted list using commands as shown:
The above command adds the values and their corresponding scores to the sorted set “languages”.
To fetch items in the set within a specific score range, use the ZRANGE command as shown:
The command returns all elements within that range sorted by score value.
To show the elements and their corresponding score value, use the WITHSCORES option as:
The command should return the element and its score.
The ZREVRANGE command works similarly to the ZRANGE command. However, it returns the values in reverse order, i.e., the score range starts from the highest to the lowest.
In this example, the results start from the highest score to the lowest value. You can enable scores using the WITHSCORES option.
Notice how the scores are in reverse order compared to the ZRANGE command output.
This guide gives you a walkthrough of some of the fundamental range commands in Redis. Keep in mind that there are other range commands such as LTRIM, XRANGE, and more. Consider the documentation for commands not discussed in this guide.