LRANGE

Syntax
LRANGE key start stop
Available since:
1.0.0
Time complexity:
O(S+N) where S is the distance of start offset from HEAD for small lists, from nearest end (HEAD or TAIL) for large lists; and N is the number of elements in the specified range.
ACL categories:
@read, @list, @slow,

返回存储在key中的列表的指定元素。 偏移量startstop是基于零的索引,0表示列表的第一个元素(列表的头部),1表示下一个元素,依此类推。

这些偏移量也可以是负数,表示从列表末尾开始的偏移量。 例如,-1 是列表的最后一个元素,-2 是倒数第二个,依此类推。

与各种编程语言中的范围函数的一致性

请注意,如果你有一个从0到100的数字列表,LRANGE list 0 10将返回11个元素,也就是说,最右边的项目是包含在内的。这可能或可能不与你选择的编程语言中与范围相关的函数的行为一致(想想Ruby的Range.newArray#slice或Python的range()函数)。

超出范围的索引

超出范围的索引不会产生错误。 如果start大于列表的末尾,将返回一个空列表。 如果stop大于列表的实际末尾,Redis会将其视为列表的最后一个元素。

示例

RPUSH mylist "one" RPUSH mylist "two" RPUSH mylist "three" LRANGE mylist 0 0 LRANGE mylist -3 2 LRANGE mylist -100 100 LRANGE mylist 5 10

RESP2/RESP3 回复

Array reply: a list of elements in the specified range, or an empty array if the key doesn't exist.
RATE THIS PAGE
Back to top ↑