Redis Lists: LTRIM
Redis LTRIM Command
Redis LTRIM command is used to trim an existing list so that it will contain only the specified range of elements specified. Both start and stop are zero-based indexes, where 0 is the first element of the list (the head), 1 the next element and so on.
Here are some common uses and scenarios for the "LTRIM" command:
- List Size Management: Limit the size of a list by keeping only a specified range of elements.
- Data Partitioning: Maintain efficient data partitioning by trimming unnecessary elements from lists.
- Task Queue Optimization: Optimize task queues by retaining only the most relevant tasks within a specified range.
- Event Handling: Manage event lists by trimming outdated or unnecessary events based on their position.
- Memory Efficiency: Improve memory usage by reducing the size of lists to essential elements.
Syntax:
redis 127.0.0.1:6379> LTRIM KEY_NAME START STOP
Available since
1.0.0.
Return Value
String reply, OK
Return Value Type
String
Example: Redis LTRIM: Delete your data outside the range specified by index
127.0.0.1:6379> LPUSH mycolor1 white black red blue (integer) 4 127.0.0.1:6379> LRANGE mycolor1 0 -1 1) "blue" 2) "red" 3) "black" 4) "white" 127.0.0.1:6379> LTRIM mycolor1 1 2 OK 127.0.0.1:6379> LRANGE mycolor1 0 -1 1) "red" 2) "black"
Example: Redis LTRIM: Use negative
127.0.0.1:6379> LPUSH mycolor1 white black red blue (integer) 4 127.0.0.1:6379> LTRIM mycolor1 -2 -1 OK 127.0.0.1:6379> LRANGE mycolor1 0 -1 1) "black" 2) "white"
Example: Redis LTRIM: Range Selection Caution
127.0.0.1:6379> LPUSH mycolor1 white black red blue (integer) 4 127.0.0.1:6379> LTRIM mycolor1 10 10 OK 127.0.0.1:6379> LRANGE mycolor1 0 -1 (empty list or set)
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/redis/redis-ltrim-key-start-stop.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics