首页 > 试题广场 >

Redis命令返回值

[不定项选择题]

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


  • k1:“Hello,world 8 9”  k2:[1,2,3,8]   K3:[1,2,3,8,9]   K4:[8,9]
  • k1:“Hello,world”  k2:[1,2,3,8]   K3:[1,2,3,8,9]   K4:[8,9]
  • k1:“Hello,world 8 9”  k2:[1,2,3]   K3:[1,2,3,8,9]   K4:[8,9]
  • k1:“Hello,world”  k2:[1,2]   K3:[1,2,3,8,9]   K4:[8,9]
推荐
首先翻译一下:
    在key对应的list的头部插入指定的value。如果键不存在,在进行push之前会创建一个空的list;如果键存在,但对应的value不是list类型的值,会返回错误。
    另外,可以一次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都错了。

编辑于 2015-02-02 11:46:49 回复(0)
ABCD
LPUSH:将所有指定的值插入到存于 key 的列表的头部。如果 key 不存在,那么在进行 push 操作前会创建一个空列表。 如果 key 对应的值不是一个 list 的话,那么会返回一个错误。
(1)k1为String类型,lpush命令支持list类型,LPUSH k1 8 9 报错:(error) ERR Operation against a key holding the wrong kind of value
(2)LPUSH命令加入列表头部,LPUSH k2 8: k2:[8,1,2,3]
(3)LPUSH命令加入列表头部,LPUSH k3 8 9: k3:[9,8,1,2,3 ]
(4)key不存在时,进行 push 操作前会创建一个空列表,LPUSH k4 8 9: k4:[9,8] 
发表于 2015-01-08 09:33:01 回复(0)