redis LTRIM command
Translation results:
trim
英[trɪm] 美[trɪm]
vt. Decorate; trim; organize
adj. Neat, tidy ; slender; slender
n. trimmed; neat; healthy state; attire
vi.cut
Third person singular: trims Present participle: trimming Past tense: trimmed Past participle: trimmed Comparative: trimmer Superlative: trimmest
redis LTRIM commandsyntax
Function:Trim a list, that is, let the list only retain elements within the specified range, and elements that are not within the specified range will be deleted.
Syntax: LTRIM key start stop
Available versions:>= 1.0.0
Time complexity Degree: O(N), N is the number of elements removed.
Return: When the command is executed successfully, ok is returned.
redis LTRIM commandexample
# 情况 1: 常见情况, start 和 stop 都在列表的索引范围之内 redis> LRANGE alpha 0 -1 # alpha 是一个包含 5 个字符串的列表 1) "h" 2) "e" 3) "l" 4) "l" 5) "o" redis> LTRIM alpha 1 -1 # 删除 alpha 列表索引为 0 的元素 OK redis> LRANGE alpha 0 -1 # "h" 被删除了 1) "e" 2) "l" 3) "l" 4) "o" # 情况 2: stop 比列表的最大下标还要大 redis> LTRIM alpha 1 10086 # 保留 alpha 列表索引 1 至索引 10086 上的元素 OK redis> LRANGE alpha 0 -1 # 只有索引 0 上的元素 "e" 被删除了,其他元素还在 1) "l" 2) "l" 3) "o" # 情况 3: start 和 stop 都比列表的最大下标要大,并且 start < stop redis> LTRIM alpha 10086 123321 OK redis> LRANGE alpha 0 -1 # 列表被清空 (empty list or set) # 情况 4: start 和 stop 都比列表的最大下标要大,并且 start > stop redis> RPUSH new-alpha "h" "e" "l" "l" "o" # 重新建立一个新列表 (integer) 5 redis> LRANGE new-alpha 0 -1 1) "h" 2) "e" 3) "l" 4) "l" 5) "o" redis> LTRIM new-alpha 123321 10086 # 执行 LTRIM OK redis> LRANGE new-alpha 0 -1 # 同样被清空 (empty list or set)