Redis是一种key-value存储服务器,在其文档中有对LPUSH命令的说明如下:
LPUSH key value [value ...]
Insert all the specified values at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operations. 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.
Return value
Integer reply: the length of the list after the push operations.
假设,当前服务器中存储了如下的键与值:
k1 :字符串"Hello, world"
k2 :列表[1, 2, 3]
k3 :列表[1, 2, 3]
选项是下列命令执行后的对应健中存储的值,及命令的返回值,请选择错误的是?
(1). LPUSH k1 8 9
(2). LPUSH k2 8
(3). LPUSH k3 8 9
(4). LPUSH k4 8 9
另外,可以一次push多个元素,只需在push后面跟着多个参数。多个参数会按从做到右的顺序插入头部。举个栗子:lpush mylist a b c
那么执行完后,mylist中的值 为 : c b a
此命令的返回值为:Integer类型的,push之后的list的长度
这样应该可以很好理解了吧。下面开始做题了:
原始值
k1 :字符串"Hello, world"
k2 :列表[1, 2, 3]
k3 :列表[1, 2, 3]
(1)LPUSH k1 8 9 :k1的value不是list类型的,返回error,k1仍然是原来的值"Hello, world"(2)LPUSH k2 8:插入头部,返回4,k2变为:列表[8, 1, 2, 3]
(3)LPUSH k3 8 9:插入头部,返回5,k3变为:列表[9, 8, 1, 2, 3]
(4)LPUSH k4 8 9:k4不存在,先创建一个,然后插入值,k4变为:列表[9, 8]
所以ABCD都错了。