Redis LRANGE – How to get all elements of a list value in redis datastore
4 weeks ago Lalit Bhagtani 0
In this tutorial, we will learn about how to get all elements of a list value stored at a key. For this we will use a COMMAND – LRANGE in redis-cli.
This command, returns one or more elements of the list value stored at specified key, defined by the specified offsets. It takes start ( inclusive ) and end ( inclusive ) offsets, these offsets are zero based indexes where 0 means the first element, 1 means the second element and so on. Negative numbers can also be used to provide an offset starting from the end of the list value, here -1 means the last element, -2 means second last element and so on.
An out of range offsets are handle in the following ways :-
- start > end :- Result is an empty list.
- end > size of list :- Index of last element of the list value is set to end offset.
If key exist but value stored at the key is not of list datatype, error is returned. The syntax of redis LRANGE command is as follows :-
redis host:post> LRANGE <keyname> <start> <end>
- (integer) reply, representing list of elements in the specified offsets. - Error, if key exist and value stored at the key is not a list.
So to get all elements of the list value, we have to use 0 as start offset and -1 as end offset in the LRANGE command like :-
LRANGE <key name> 0 -1
That’s all for how to get all elements of a list value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.