redis命令(三)-双端队列类型
队列类型内部是使用双向链表(double linked list)实现的,所以向列表两端添加元素的时
间复杂度为0(1),获取越接近两端的元素速度就越快。
以下命令中 key 代表列表
1.向队列左侧加入元素,返回增加后队列的长度
lpush key value1 value2 value...
localhost:6379> lpush students a b c (integer) 3 添加后队列中的元素顺序为 【c,b,a】
2.向队列右侧加入元素,返回增加后队列的长度
rpush key value1 value2 value....
localhost:6379> rpush students o p q (integer) 6 添加后的元素顺序为【c,b,a,o,p,q】
3.从队列左侧弹出元素(相当于移出元素)
lpop key
localhost:6379> lpop students "c" localhost:6379> lpop students "b"
4.从队列右侧弹出元素(相当于移出元素)
rpop key
localhost:6379> rpop students "q" localhost:6379> rpop students "p"
5.获取队列中元素的个数
llen key
localhost:6379> llen students (integer) 4
6.获取按索引范围获取队列中的元素(但不从队列中移出)
lrange key startIndex endIndex
startIndex :开始元素序号
endIndex :截止元素序号(获取的元素中含有该元素)
元素序号值:队列最左侧元素序号为0,之后元素序号依次+1,最右侧元素为序号最大的元素
另一种元素序号值:最右侧元率序号为-1,之前的元素序号依次-1,最左侧 元素为序号最小的元素
localhost:6379> del students
localhost:6379> lpush students a b c (integer) 3 localhost:6379> rpush students o p q (integer) 6 localhost:6379> lrange students 1 4 1) "b" 2) "a" 3) "o" 4) "p" localhost:6379> lrange students 1 -2 1) "b" 2) "a" 3) "o" 4) "p"
7.删除队列中指定的值(返回实际删除元素的个数)
lrem key count value
从队列中删除count个值等于value的元素。
count 为正数时:则从左向右 开始删除,直至达到count个元素或队列中没有元素值==value的元素为止
count 为负时:则从右向左开始删除,直至达到count个元素或队列中没有元素值==value的元素为止
count ==0 时:表示删除队列中所有元素值==value的元素
localhost:6379> del students
localhost:6379> lpush students c b a c b a c b a (integer) 9 localhost:6379> lrange students 0 -1 1) "a" 2) "b" 3) "c" 4) "a" 5) "b" 6) "c" 7) "a" 8) "b" 9) "c" localhost:6379> lrem students -2 c//从右侧删除2个元素值==c的元素 (integer) 2 localhost:6379> lrange students 0 -1 1) "a" 2) "b" 3) "c" 4) "a" 5) "b" 6) "a" 7) "b" localhost:6379> lrem students 2 a//从左侧删除2个元素值==a的元素 (integer) 2 localhost:6379> lrange students 0 -1 1) "b" 2) "c" 3) "b" 4) "a" 5) "b"
8.按索引序号获取队列中的元素
lindex key index
元素序号值:队列最左侧元素序号为0,之后元素序号依次+1,最右侧元素为序号最大的元素
另一种元素序号值:最右侧元率序号为-1,之前的元素序号依次-1,最左侧 元素为序号最小的元素
localhost:6379> del students
localhost:6379> lpush students a b c d e f (integer) 6 localhost:6379> lindex students 0 "f" localhost:6379> lindex students -1 "a" localhost:6379> lindex students 2 "d"
9,修改队列中的元素值
lset key index value
指定索引位置的元素值被value覆盖
localhost:6379> del students (integer) 1 localhost:6379> lpush students a b c d e f g (integer) 7 localhost:6379> lset students -2 x OK localhost:6379> lrange students 0 -1 1) "g" 2) "f" 3) "e" 4) "d" 5) "c" 6) "x" 7) "a" localhost:6379> lset students 0 z OK localhost:6379> lrange students 0 -1 1) "z" 2) "f" 3) "e" 4) "d" 5) "c" 6) "x" 7) "a"
10.删除指定索引范围之外的所有元素
ltrim key startIndex endIndex
删除索引值<startIndex 且>endIndex的所有元素
localhost:6379> del students (integer) 1 localhost:6379> rpush students a b c d e f (integer) 6 localhost:6379> lrange students 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 6) "f" localhost:6379> ltrim students 1 -2 OK localhost:6379> lrange students 0 -1 1) "b" 2) "c" 3) "d" 4) "e"
11.向队列中插入新元素,并返回插入之后的队列长度
linsert key before|after oldValue newValue
在列表中查找oldValue,找到后在它的之前(before) 或之后 (after)位置插入新的元素 newValue
localhost:6379> del students (integer) 1 localhost:6379> rpush students a b c d e f (integer) 6 localhost:6379> lrange students 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 6) "f" localhost:6379> linsert students before b x (integer) 7 localhost:6379> lrange students 0 -1 1) "a" 2) "x" 3) "b" 4) "c" 5) "d" 6) "e" 7) "f" localhost:6379> linsert students after e z (integer) 8 localhost:6379> lrange students 0 -1 1) "a" 2) "x" 3) "b" 4) "c" 5) "d" 6) "e" 7) "z" 8) "f"
12.把A队列最右侧元素移除并添加到B队列左侧 ,并返回被移动的元素值
rpoplpush sourceKey destKey
localhost:6379> rpush A 1 2 3 (integer) 3 localhost:6379> rpush B 7 8 9 (integer) 3 localhost:6379> rpoplpush A B "3" localhost:6379> lrange A 0 -1 1) "1" 2) "2" localhost:6379> lrange B 0 -1 1) "3" 2) "7" 3) "8" 4) "9"