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

将所有指定值(Value)插入到键 key 对应的列表(List)的头部(Head)

Insert all the specified values at the head of the list stored at key.

如果键 key 不存在,在进行当前操作前,将会被新键为一个空列表。

If key does not exist, it is created as empty list before performing the push operation. 

当键 key 对应的值不为列表时,将会返回一个错误。

When key holds a value that is not a list, an error is returned.

可以通过在命令末尾指定多个参数,以达到单次调用插入多个元素的目的。所有元素按从左至右的顺序挨个插入到列表的头部。

It is possible to push multiple elements using a single command call just specifying multiple arguments at the end of the command. Elements are inserted one after the other to the head of the list, from the leftmost element to the rightmost element. So for instance the command LPUSH mylist a b c will result into a list containing c as first element, b as second element and a as third element.
  • 返回在进行压入操作后,当前列表的长度。

    Integer reply: the length of the list after the push operation.
  • redis> LPUSH mylist "world"
    (integer) 1
    redis> LPUSH mylist "hello"
    (integer) 2
    redis> LRANGE mylist 0 -1
    1) "hello"
    2) "world"
  • 版本支持

    1.0.0+

    2.4+ 版本才开始支持多值参数。在小于 2.4 的 Redis 版本中,一次命令调用只允许压入一个值。

    >= 2.4: Accepts multiple value arguments. In Redis versions older than 2.4 it was possible to push a single value per command.

    时间复杂度(Time complexity)

    O(1)