LTRIM command in redis

LTRIM key start end

Trim an existing list so that it will contain only the specified range of elements specified. 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.

TIME COMPLEXITY: O(n) (with n being len of the list - len of 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.

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 left as a value. If ends over the end of the list Redis will threaten it just like the last element of the list.

RETURN VALUE: Status code reply

Syntax:

LTRIM KEY_NAME START END

Example 1: When the key is present.

redis:6379> LPUSH myList 100
(integer) 1
redis:6379> LPSUH myList 200
(error) unknown command 'LPSUH'
redis:6379> LPUSH myList 300
(integer) 2
redis:6379> LPUSH myList 200
(integer) 3
redis:6379> LPUSH myList 400
(integer) 4
redis:6379> LTRIM myList 0 2
"OK"

Output

OK


Example 2: When the key is not present.

redis:6379> LTRIM rediKey 0 2
"OK"

Output

OK


No comments:

Post a Comment