• redis学习4-list


    基本的数据类型,列表,redis命令是不区分大小写的

    在redis中,我们可以把list玩成,线,队列,阻塞队列!

    所有的list命令都是用l开头的

    1. [root@centos7964 bin]# redis-cli -p 6379
    2. 127.0.0.1:6379> LPUSH list one #将一个值或者多个值,插入到列表头部(左)
    3. (integer) 1
    4. 127.0.0.1:6379> lpush list two
    5. (error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
    6. #今天新安装的镜像,新安装的redis,人都傻了
    7. 127.0.0.1:6379> LPUSH list two
    8. (error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
    9. (错误)MISCONF Redis被配置为保存RDB快照,但它目前Ly不能持久在磁盘上。可能修改数据集的命令被禁用,因为该实例被配置为在RDB快照失败时报告写入错误(stop- written -on - bgsave-error选项)。有关RDB错误的详细信息,请查看Redis日志。127.0.0.1:6379>推送列表2
    10. #使用下面这个命令进行修改rdb
    11. 127.0.0.1:6379> config set stop-writes-on-bgsave-error no
    12. OK
    13. 127.0.0.1:6379> LPUSH list two
    14. (integer) 2
    15. 127.0.0.1:6379> Lpush list three
    16. (integer) 3
    17. 127.0.0.1:6379> lrange list 0 -1
    18. 1) "three"
    19. 2) "two"
    20. 3) "one"
    21. 127.0.0.1:6379> lrange list 0 1 #插入时从左边插,取出时从左边取,左边插,左边去,所以先插入的在后续插入时需要向右移
    22. 1) "three"
    23. 2) "two"
    24. 127.0.0.1:6379> rpush list righr #将一个值或者多个值,插入到列表位部(右)
    25. (integer) 4
    26. 127.0.0.1:6379> lrange list 0 -1
    27. 1) "three"
    28. 2) "two"
    29. 3) "one"
    30. 4) "righr"

     lpush/rpush

    1. 127.0.0.1:6379> lrange list 0 -1
    2. 1) "three"
    3. 2) "two"
    4. 3) "one"
    5. 4) "righr"
    6. 127.0.0.1:6379> lpop list #移除list的第一个元素
    7. "three"
    8. 127.0.0.1:6379> lrange list 0 -1
    9. 1) "two"
    10. 2) "one"
    11. 3) "righr"
    12. 127.0.0.1:6379> RPOP list #移除list的最后一个元素
    13. "righr"
    14. 127.0.0.1:6379> lrange list 0-1
    15. (error) ERR wrong number of arguments for 'lrange' command
    16. 127.0.0.1:6379> lrange list 0 -1
    17. 1) "two"
    18. 2) "one"

    lindex

    1. 127.0.0.1:6379> lrange list 0 -1
    2. 1) "two"
    3. 2) "one"
    4. 127.0.0.1:6379> lrange list 0 -1
    5. 1) "two"
    6. 2) "one"
    7. 127.0.0.1:6379> lindex list 1 #通过下标获取list中对应的值
    8. "one"
    9. 127.0.0.1:6379> lindex list 0
    10. "two"

    llen

    1. 127.0.0.1:6379> lpush list one
    2. (integer) 1
    3. 127.0.0.1:6379> lpush list two
    4. (integer) 2
    5. 127.0.0.1:6379> lpush list three
    6. (integer) 3
    7. 127.0.0.1:6379> llen list #返回列表的长度
    8. (integer) 3

    移除指定的值;取关 uid

    lrem

    1. 127.0.0.1:6379> lrange list 0 -1
    2. 1) "three"
    3. 2) "three"
    4. 3) "two"
    5. 4) "one"
    6. 127.0.0.1:6379> lrem list 0 -1
    7. (integer) 0
    8. 127.0.0.1:6379> lrem list 1 one
    9. (integer) 1
    10. 127.0.0.1:6379> lrange list 0 -1
    11. 1) "three"
    12. 2) "three"
    13. 3) "two"
    14. 127.0.0.1:6379> lrem list 1 three #移除list集合中指定个数的value,精确匹配
    15. (integer) 1
    16. 127.0.0.1:6379> lrange list 0 -1
    17. 1) "three"
    18. 2) "two"
    19. 127.0.0.1:6379> lpush list three
    20. (integer) 3
    21. 127.0.0.1:6379> lrem list 2 theree
    22. (integer) 0
    23. 127.0.0.1:6379> lrem list 2 three
    24. (integer) 2
    25. 127.0.0.1:6379> lrange list 0 -1
    26. 1) "two"

    trim修剪;list截断

    1. 127.0.0.1:6379> keys *
    2. 1) "list"
    3. 127.0.0.1:6379> flushdb
    4. OK
    5. 127.0.0.1:6379> keys *
    6. (empty list or set)
    7. 127.0.0.1:6379> rpush mylist "hello"
    8. (integer) 1
    9. 127.0.0.1:6379> rpush mylist "hello1"
    10. (integer) 2
    11. 127.0.0.1:6379> rpush mylist "hello2"
    12. (integer) 3
    13. 127.0.0.1:6379> rpush mylist "hello3"
    14. (integer) 4
    15. 127.0.0.1:6379> ltrim mylist 1 2 #通过下标截取指定的长度,这个lsit已经被改变了,截断了只剩下截取的元素
    16. OK
    17. 127.0.0.1:6379> lrange mylist 0 -1
    18. 1) "hello1"
    19. 2) "hello2"
    20. 127.0.0.1:6379> lrange mylist 0 -1
    21. 1) "hello1"
    22. 2) "hello2"
    23. 127.0.0.1:6379>

    rpoplpush #移除列表的最后一个元素,将他一档到新的列表中!

    1. 127.0.0.1:6379> flushdb
    2. OK
    3. 127.0.0.1:6379> rpush mylist "hello"
    4. (integer) 1
    5. 127.0.0.1:6379> rpush mylist "hello1"
    6. (integer) 2
    7. 127.0.0.1:6379> rpush mylist "hello2"
    8. (integer) 3
    9. 127.0.0.1:6379> lrange mylist 0 -1'
    10. Invalid argument(s)
    11. 127.0.0.1:6379> lrange mylist 0 -1
    12. 1) "hello"
    13. 2) "hello1"
    14. 3) "hello2"
    15. 127.0.0.1:6379> RPOPLPUSH mylist myotherlist #移除列表的最后一个元素怒=,将他移动到新的列表中!
    16. "hello2"
    17. 127.0.0.1:6379> lrange myotherlist 0 -1
    18. 1) "hello2"
    19. 127.0.0.1:6379> lrange myslist 0 -1 #查看目标列表中,确实存在该值
    20. (empty list or set)
    21. 127.0.0.1:6379> lrange mylist 0 -1 #查看原来的列表
    22. 1) "hello"
    23. 2) "hello1"

    lset 将列表中指定下标额值替换为另外一个值,更新操作

    1. 127.0.0.1:6379> exists list
    2. (integer) 0
    3. 127.0.0.1:6379> lset list 0 item #判断这个列表是否存在
    4. (error) ERR no such key
    5. 127.0.0.1:6379> lpush list value1 #如果不存在列表我们去更新就会报错
    6. (integer) 1
    7. 127.0.0.1:6379> lrange list 0 0
    8. 1) "value1"
    9. 127.0.0.1:6379> lset list 0 item #如果存在,更新当前下标的值
    10. OK
    11. 127.0.0.1:6379> lrange list 0 0
    12. 1) "item"
    13. 127.0.0.1:6379> lset list 1 other #如果不存在,则会报错
    14. (error) ERR index out of range

    linsert 将某个具体的value插入到列中某个元素的前面或者后面!

    1. 127.0.0.1:6379> rpush mylist "hello"
    2. (integer) 1
    3. 127.0.0.1:6379> rpush mylist "world"
    4. (integer) 2
    5. 127.0.0.1:6379> linsert mylist before "wordl" "other"
    6. (integer) -1
    7. 127.0.0.1:6379> linsert mylist before "world" "other"
    8. (integer) 3
    9. 127.0.0.1:6379> linsert mylist after world new
    10. (integer) 4
    11. 127.0.0.1:6379> lrange mylist 0 -1
    12. 1) "hello"
    13. 2) "other"
    14. 3) "world"
    15. 4) "new"

    小结:redis中lsit实际上是一个链表,before node after,left,right都可以插入值

    如果key不存在,创建新链表

    如果key存在,新增内容

    如果移除了所有值,空链表,也代表不存在!

    在两边插入或者改动值,效率最高!中间元素,相对来说效率会低一些

    消息排队!消息队列(Lpush Rpop),栈(Lpush lpop)

  • 相关阅读:
    2-6:Http请求头知识点详解
    SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
    STL-常用遍历算法
    机器学习3-岭回归,Lasso,变量选择技术
    博弈补充练习
    【DL】使用pytorch的线性回归简洁实现
    解决ios向mac复制文字不成功的一种方法
    前端设计模式——备忘录模式
    从政府工作报告探云计算、大数据与人工智能的应用与推动作用
    Oracle SQL执行计划操作(11)——层级相关操作
  • 原文地址:https://blog.csdn.net/weixin_61407147/article/details/127894672