• Redis从0到精通--List


    List(列表)

    基本的数据类型,列表

    在redis里面,我们可以把list玩成 ,栈、队列、阻塞队列! 所有的list命令都是用l开头的,Redis不区分大小命令

    ##########################################################################
    127.0.0.1:6379> LPUSH list one # 将一个值或者多个值,插入到列表头部 (左)
    (integer) 1
    127.0.0.1:6379> LPUSH list two
    (integer) 2
    127.0.0.1:6379> LPUSH list three
    (integer) 3
    127.0.0.1:6379> LRANGE list 0 -1 # 获取list中值!
    1) "three"
    2) "two"
    3) "one"
    127.0.0.1:6379> LRANGE list 0 1 # 通过区间获取具体的值!
    1) "three"
    2) "two"
    127.0.0.1:6379> Rpush list righr # 将一个值或者多个值,插入到列表位部 (右)
    (integer) 4
    127.0.0.1:6379> LRANGE list 0 -1
    1) "three"
    2) "two"
    3) "one"
    4) "righr"
    ##########################################################################
    LPOP
    RPOP
    127.0.0.1:6379> LRANGE list 0 -1
    1) "three"
    2) "two"
    3) "one"
    4) "righr"
    127.0.0.1:6379> Lpop list # 移除list的第一个元素
    "three"
    127.0.0.1:6379> Rpop list # 移除list的最后一个元素
    "righr"
    127.0.0.1:6379> LRANGE list 0 -1
    1) "two"
    2) "one"
    ##########################################################################
    Lindex
    127.0.0.1:6379> LRANGE list 0 -1
    1) "two"
    2) "one"
    127.0.0.1:6379> lindex list 1 # 通过下标获得 list 中的某一个值!
    "one"
    127.0.0.1:6379> lindex list 0
    "two"
    ##########################################################################
    Llen
    127.0.0.1:6379> Lpush list one
    (integer) 1
    127.0.0.1:6379> Lpush list two
    (integer) 2
    127.0.0.1:6379> Lpush list three
    (integer) 3
    127.0.0.1:6379> Llen list # 返回列表的长度
    (integer) 3
    ##########################################################################
    移除指定的值!
    取关 uid
    Lrem
    127.0.0.1:6379> LRANGE list 0 -1
    1) "three"
    2) "three"
    3) "two"
    4) "one"
    127.0.0.1:6379> lrem list 1 one # 移除list集合中指定个数的value,精确匹配
    (integer) 1
    127.0.0.1:6379> LRANGE list 0 -1
    1) "three"
    2) "three"
    3) "two"
    127.0.0.1:6379> lrem list 1 three
    (integer) 1
    127.0.0.1:6379> LRANGE list 0 -1
    1) "three"
    2) "two"
    127.0.0.1:6379> Lpush list three
    (integer) 3
    127.0.0.1:6379> lrem list 2 three
    (integer) 2
    127.0.0.1:6379> LRANGE list 0 -1
    1) "two"
    ##########################################################################
    trim 修剪。; list 截断!
    127.0.0.1:6379> keys *
    (empty list or set)
    127.0.0.1:6379> Rpush mylist "hello"
    (integer) 1
    127.0.0.1:6379> Rpush mylist "hello1"
    (integer) 2
    127.0.0.1:6379> Rpush mylist "hello2"
    (integer) 3
    127.0.0.1:6379> Rpush mylist "hello3"
    (integer) 4
    127.0.0.1:6379> ltrim mylist 1 2 # 通过下标截取指定的长度,这个list已经被改变了,截断了
    只剩下截取的元素!
    OK
    127.0.0.1:6379> LRANGE mylist 0 -1
    1) "hello1"
    2) "hello2"
    ##########################################################################
    rpoplpush # 移除列表的最后一个元素,将他移动到新的列表中!
    127.0.0.1:6379> rpush mylist "hello"
    (integer) 1
    127.0.0.1:6379> rpush mylist "hello1"
    (integer) 2
    127.0.0.1:6379> rpush mylist "hello2"
    (integer) 3
    127.0.0.1:6379> rpoplpush mylist myotherlist # 移除列表的最后一个元素,将他移动到新的
    列表中!
    "hello2"
    127.0.0.1:6379> lrange mylist 0 -1 # 查看原来的列表
    1) "hello"
    2) "hello1"
    127.0.0.1:6379> lrange myotherlist 0 -1 # 查看目标列表中,确实存在改值!
    1) "hello2"
    ##########################################################################
    lset 将列表中指定下标的值替换为另外一个值,更新操作
    127.0.0.1:6379> EXISTS list # 判断这个列表是否存在
    (integer) 0
    127.0.0.1:6379> lset list 0 item # 如果不存在列表我们去更新就会报错
    (error) ERR no such key
    127.0.0.1:6379> lpush list value1
    (integer) 1
    127.0.0.1:6379> LRANGE list 0 0
    1) "value1"
    127.0.0.1:6379> lset list 0 item # 如果存在,更新当前下标的值
    OK
    127.0.0.1:6379> LRANGE list 0 0
    1) "item"
    127.0.0.1:6379> lset list 1 other # 如果不存在,则会报错!
    (error) ERR index out of range
    ##########################################################################
    linsert # 将某个具体的value插入到列把你中某个元素的前面或者后面!
    127.0.0.1:6379> Rpush mylist "hello"
    (integer) 1
    127.0.0.1:6379> Rpush mylist "world"
    (integer) 2
    127.0.0.1:6379> LINSERT mylist before "world" "other"
    (integer) 3
    127.0.0.1:6379> LRANGE mylist 0 -1
    1) "hello"
    2) "other"
    3) "world"
    127.0.0.1:6379> LINSERT mylist after world new
    (integer) 4
    127.0.0.1:6379> LRANGE mylist 0 -1
    1) "hello"
    2) "other"
    3) "world"
    4) "new"
    
    

    小结

    • 他实际上是一个链表,before Node after , left,right 都可以插入值
    • 如果key 不存在,创建新的链表
    • 如果key存在,新增内容
    • 如果移除了所有值,空链表,也代表不存在!
    • 在两边插入或者改动值,效率最高! 中间元素,相对来说效率会低一点~

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

  • 相关阅读:
    imagemagick-图片
    selenium-嘿
    centos命令行连接无线网络
    centos7安装桌面合盖不休眠
    mysql执行命令:ERROR 1820 (HY000): You must reset your password
    编码规范 C++
    Docker使用总结
    JAVA使用总结
    VS IDE 相关
    编程网站总结
  • 原文地址:https://www.cnblogs.com/qq3245792286/p/15405606.html
Copyright © 2020-2023  润新知