Introduction
LTRIM helps keep a Redis list at a fixed size by removing items outside a set range. This stops the list from growing too big and using too much memory.
LTRIM key start stop
LTRIM mylist 0 99
LTRIM mylist 0 -1
LTRIM mylist 10 19
LTRIM mylist 0 0
127.0.0.1:6379> RPUSH mylist a b c d e f g h i j (integer) 10 127.0.0.1:6379> LRANGE mylist 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 6) "f" 7) "g" 8) "h" 9) "i" 10) "j" 127.0.0.1:6379> LTRIM mylist 0 4 OK 127.0.0.1:6379> LRANGE mylist 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e"
LTRIM mylist 0 9 do to the list mylist?LTRIM mylist 0 9 keeps elements from index 0 to 9 inclusive.tasks using LTRIM?LTRIM tasks -5 -1 keeps elements from fifth last to last, i.e., last 5 elements.colors with elements ["red", "green", "blue", "yellow", "purple"], what will be the list content after executing LTRIM colors 1 3?LTRIM mylist 5 2 on a Redis list. What will happen?LTRIM mylist 5 2 empties the list.chatlog. Which sequence of commands correctly caps the list size after adding a new message "Hello"?LPUSH adds new messages to the start (index 0), so recent messages are at the front.LTRIM chatlog 0 19. Negative indexes also work but must match direction.LPUSH adds to front, LTRIM chatlog 0 19 keeps first 20 recent messages. LPUSH chatlog "Hello"; LTRIM chatlog -20 -1 uses LPUSH and LTRIM chatlog -20 -1, which keeps last 20 elements, but since new messages are at front, last 20 are oldest. So LPUSH chatlog "Hello"; LTRIM chatlog -20 -1 is incorrect.LPUSH and LTRIM chatlog 0 19, which correctly keeps the 20 most recent messages at front.Pass the test and this pattern moves unknown