本文内容为 http://www.redis.io/commands/lrange 的翻译、注解、例子扩充及其它修改。感谢 Redis 作者 Antirez 为开源社区作出的贡献,本文保证最新、最准、最全以表示对其的敬意。欢迎留言纠错、提示更新或支持。
LRANGE key start stop

返回存储在键 key 中的列表(List)中的指定元素。

Returns the specified elements of the list stored at key.

偏移值 startstop 都是基于 0(Zero-based),即 0 为列表中的第一个元素,1 为第二个元素,以此类推。

The offsets start and stop are zero-based indexes, with 0 being the first element of the list (the head of the list), 1 being the next element and so on.

它们也可以为负值,表明偏移从列表末尾开始计算,-1 为最后一个元素,-2 为倒数第二个元素,以此类推。

These offsets can also be negative numbers indicating offsets starting at the end of the list. For example, -1 is the last element of the list, -2 the penultimate, and so on.
  • 指定范围中的元素列表。

    Array reply: list of elements in the specified range.
  • redis> RPUSH mylist "one"
    (integer) 1
    redis> RPUSH mylist "two"
    (integer) 2
    redis> RPUSH mylist "three"
    (integer) 3
    redis> LRANGE mylist 0 0
    1) "one"
    redis> LRANGE mylist -3 2
    1) "one"
    2) "two"
    3) "three"
    redis> LRANGE mylist -100 100
    1) "one"
    2) "two"
    3) "three"
    redis> LRANGE mylist 5 10
    (empty list or set)
  • Consistency with range functions in various programming languages

    Note that if you have a list of numbers from 0 to 100, LRANGE list 0 10 will return 11 elements, that is, the rightmost item is included. This may or may not be consistent with behavior of range-related functions in your programming language of choice (think Ruby's Range.new, Array#slice or Python's range() function).
  • 超出范围的索引将不会产生错误。

    如果选项 start 比列表中最大索引还要大,或 start > stop,将会返回空列表。

    如果选项 stop 比列表中最大索引还要大,Redis 将会视其为列表中最后一个元素对待。

    Out-of-range indexes

    Out of range indexes will not produce an error. If start is larger than the end of the list, an empty list is returned. If stop is larger than the actual end of the list, Redis will treat it like the last element of the list.
  • 版本支持

    1.0.0+

    时间复杂度(Time complexity)

    O(S+N)

    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.