ITPub博客

首页 > 数据库 > NoSQL > redis list链表结构及命令浅谈【四】

redis list链表结构及命令浅谈【四】

原创 NoSQL 作者:哎呀我的天呐 时间:2015-08-19 11:23:07 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出去












来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29990276/viewspace-1776119/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
从事Oracle/MySQL工作多年,11g OCM,擅长Oracle/MySQL SQL Tuning & DB Performance,高可用方案架构

注册时间:2014-10-30

  • 博文量
    252
  • 访问量
    1825735