设为首页 收藏本站
查看: 1322|回复: 0

[经验分享] redis命令(3)--双端队列类型

[复制链接]

尚未签到

发表于 2016-12-20 11:45:11 | 显示全部楼层 |阅读模式
  队列类型内部是使用双向链表(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"

 
 13.BRPOP 
  从队列右侧弹出元素,如果没有元素则阻塞。 
  BRPOP  key [key …]timeout
  参数:key [key] 为一个或多个队列的KEY,redis从左向右检测是否队列中有元素,如果有则直接取出返回,即N个队列都有元素的情况下,左侧队列元素优先取出,都没有元素时,阻塞直至某个队列最先有元素。
  timeout:超时时间,单位是秒。当超过了此时间仍然没有获得新元素的话就会返回nil。如果超时时间==0,表示不限制等待的时间,即如果没有新元素加入列表就会永远阻塞下去。

localhost:6379> del commandList
(integer) 0
localhost:6379> lpush commandList mkdir cd touch
(integer) 3
localhost:6379> brpop commandList 0
1) "commandList"
2) "mkdir"
localhost:6379> brpop commandList 0
1) "commandList"
2) "cd"
localhost:6379> brpop commandList 0
1) "commandList"
2) "touch"
localhost:6379> brpop commandList 0//被阻塞直到有元素为止

 
 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-316977-1-1.html 上篇帖子: Redis中的双向链表的实现 下篇帖子: redis3.0集群搭建所需软件redis-3.2.1.gem rubygems-2.4.8.tgz
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表