码迷,mamicode.com
首页 > 其他好文 > 详细

redis队列链表使用操作

时间:2016-07-21 17:52:20      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

lpush/rpush:lpush从链表左边插入,rpush从链表右边插入


  1. 127.0.0.1:6379> lpush character a
  2. (integer) 1
  3. 127.0.0.1:6379> rpush character b
  4. (integer) 2
  5. 127.0.0.1:6379> rpush character c
  6. (integer) 3

那么这个链表是a-->b-->c
取出链表中元素


  1. 127.0.0.1:6379> lrange character 1 2
  2. 1) "b"
  3. 2) "c"
  4. 127.0.0.1:6379> lrange character 1 3
  5. 1) "b"
  6. 2) "c"
  7. 127.0.0.1:6379> lrange character 0 2
  8. 1) "a"
  9. 2) "b"
  10. 3) "c"

看其中所有的元素:


  1. 127.0.0.1:6379> lrange character 0 -1
  2. 1) "a"
  3. 2) "b"
  4. 3) "c"

弹出元素lpop/rpop


  1. 127.0.0.1:6379> lpop character
  2. "a"
  3. 127.0.0.1:6379> lrange character 0 -1
  4. 1) "b"
  5. 2) "c"

删元素:lrem[link_name] count value ,count是正数的话,从左边删除,是负数的话从链表尾端删除,count是指定删除的个数。


  1. 127.0.0.1:6379> lpush answer a b c a b d a
  2. (integer) 7
  3. 127.0.0.1:6379> lrange answer 0 -1
  4. 1) "a"
  5. 2) "d"
  6. 3) "b"
  7. 4) "a"
  8. 5) "c"
  9. 6) "b"
  10. 7) "a"
  11. 127.0.0.1:6379> lrem answer 1 b
  12. (integer) 1
  13. 127.0.0.1:6379> lrange answer 0 -1
  14. 1) "a"
  15. 2) "d"
  16. 3) "a"
  17. 4) "c"
  18. 5) "b"
  19. 6) "a"

截取:ltrim


  1. 127.0.0.1:6379> rpush test a b c d e f
  2. (integer) 6
  3. 127.0.0.1:6379> lrange test 0 -1
  4. 1) "a"
  5. 2) "b"
  6. 3) "c"
  7. 4) "d"
  8. 5) "e"
  9. 6) "f"
  10. 127.0.0.1:6379> ltrim test 2 5
  11. OK
  12. 127.0.0.1:6379> lrange test 0 -1
  13. 1) "c"
  14. 2) "d"
  15. 3) "e"
  16. 4) "f"

lindex:


  1. 127.0.0.1:6379> lrange test 0 -1
  2. 1) "c"
  3. 2) "d"
  4. 3) "e"
  5. 4) "f"
  6. 127.0.0.1:6379> lindex test 0
  7. "c"
  8. 127.0.0.1:6379> lindex test 1
  9. "d"
  10. 127.0.0.1:6379> lindex test 2
  11. "e"

看链表长度:


  1. 127.0.0.1:6379> llen test
  2. (integer) 4
  3. 127.0.0.1:6379> rpush test a v r s g w ere ge ge d ss ss
  4. (integer) 16
  5. 127.0.0.1:6379> llen test
  6. (integer) 16

linsert:


  1. 127.0.0.1:6379> rpush number 1 4 6 8 9
  2. (integer) 5
  3. 127.0.0.1:6379> lrange number 0 -1
  4. 1) "1"
  5. 2) "4"
  6. 3) "6"
  7. 4) "8"
  8. 5) "9"
  9. 127.0.0.1:6379> linsert number before 4 2
  10. (integer) 6
  11. 127.0.0.1:6379> lrange number 0 -1
  12. 1) "1"
  13. 2) "2"
  14. 3) "4"
  15. 4) "6"
  16. 5) "8"
  17. 6) "9"
  18. 127.0.0.1:6379> linsert number before 3 1
  19. (integer) -1
  20. 127.0.0.1:6379> lrange number 0 -1
  21. 1) "1"
  22. 2) "2"
  23. 3) "4"
  24. 4) "6"
  25. 5) "8"
  26. 6) "9"
  27. 127.0.0.1:6379> linsert number after 9 10
  28. (integer) 7
  29. 127.0.0.1:6379> lrange number 0 -1
  30. 1) "1"
  31. 2) "2"
  32. 3) "4"
  33. 4) "6"
  34. 5) "8"
  35. 6) "9"
  36. 7) "10"

右边弹出左边塞入:两个链表,第一个链表的尾部元素弹出到第二个链表的头部,原子性操作,构建安全队列,抢票可以这么搞。


  1. 127.0.0.1:6379> rpush task a b c d
  2. (integer) 4
  3. 127.0.0.1:6379> lrange task 0 -1
  4. 1) "a"
  5. 2) "b"
  6. 3) "c"
  7. 4) "d"
  8. 127.0.0.1:6379> rpoplpush task job
  9. "d"
  10. 127.0.0.1:6379> rpoplpush task job
  11. "c"
  12. 127.0.0.1:6379> lrange job 0 -1
  13. 1) "c"
  14. 2) "d"
  15. 127.0.0.1:6379> lrange task 0 -1
  16. 1) "a"
  17. 2) "b"

brpop:


  1. 127.0.0.1:6379> brpop job 20
  2. 1) "job"
  3. 2) "e"
  4. (13.64s)

当前的job链表是空的,因为没有元素可以被pop,等20s,另起会话,直到另外会话添加元素的话,就会pop出去

redis队列链表使用操作

标签:

原文地址:http://www.cnblogs.com/yjqx/p/5692352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!