LRANGE command in redis

LRANGE key start end

Return the specified elements of the list stored at the specified key. Start and end are zero-based indexes. 0 is the first element of the list (the list head), 1 is the next element, and so on.

For example, LRANGE foobar 0 2 will return the first three elements of the list.

TIME COMPLEXITY: O(n) (with n being the length of the range)

Note 1: start and end can also be negative numbers indicating offsets from the end of the list. For example, -1 is the last element of the list, -2 is the penultimate element, and so on.

Note 2: Indexes out of range will not produce an error: if start is over the end of the list, or start > end, an empty list is returned. If the end is over at the end of the list Redis will threaten it just like the last element of the list.

RETURN VALUE: A multi bulk reply of a list of elements in the specified range.

Syntax:

LRANGE KEY_NAME START END

Example 1: When the range is valid.

redis:6379> LPUSH myRedisList 1000
(integer) 1
redis:6379> LPUSH myRedisList 2000
(integer) 2
redis:6379> LPUSH myRedisList 3405
(integer) 3
redis:6379> LPUSH myRedisList 1288
(integer) 4
redis:6379>

redis:6379> LRANGE myRedisList 0 2
1) "1288"
2) "3405"
3) "2000"

Output

1) "1288"

2) "3405"

3) "2000"


Example 2: When the range is invalid

redis:6379> LPUSH myRedisList 1000
(integer) 1
redis:6379> LPUSH myRedisList 2000
(integer) 2
redis:6379> LPUSH myRedisList 3405
(integer) 3
redis:6379> LPUSH myRedisList 1288
(integer) 4
redis:6379>

redis:6379> LRANGE myRedisList 2 1
(empty array)

Output

empty array

No comments:

Post a Comment