基本的数据类型,列表,redis命令是不区分大小写的
在redis中,我们可以把list玩成,线,队列,阻塞队列!
所有的list命令都是用l开头的
[root@centos7964 bin]# redis-cli -p 6379
127.0.0.1:6379> LPUSH list one #将一个值或者多个值,插入到列表头部(左)
(integer) 1
127.0.0.1:6379> lpush list two
(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.
#今天新安装的镜像,新安装的redis,人都傻了
127.0.0.1:6379> LPUSH list two
(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.
(错误)MISCONF Redis被配置为保存RDB快照,但它目前Ly不能持久在磁盘上。可能修改数据集的命令被禁用,因为该实例被配置为在RDB快照失败时报告写入错误(stop- written -on - bgsave-error选项)。有关RDB错误的详细信息,请查看Redis日志。127.0.0.1:6379>推送列表2
#使用下面这个命令进行修改rdb
127.0.0.1:6379> config set stop-writes-on-bgsave-error no
OK
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
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"
lpush/rpush
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> lrange list 0 -1
1) "two"
2) "one"
3) "righr"
127.0.0.1:6379> RPOP list #移除list的最后一个元素
"righr"
127.0.0.1:6379> lrange list 0-1
(error) ERR wrong number of arguments for 'lrange' command
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> 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 0 -1
(integer) 0
127.0.0.1:6379> lrem list 1 one
(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 #移除list集合中指定个数的value,精确匹配
(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 theree
(integer) 0
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 *
1) "list"
127.0.0.1:6379> flushdb
OK
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 #通过下标截取指定的长度,这个lsit已经被改变了,截断了只剩下截取的元素
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "hello1"
2) "hello2"
127.0.0.1:6379> lrange mylist 0 -1
1) "hello1"
2) "hello2"
127.0.0.1:6379>
rpoplpush #移除列表的最后一个元素,将他一档到新的列表中!
127.0.0.1:6379> flushdb
OK
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> lrange mylist 0 -1'
Invalid argument(s)
127.0.0.1:6379> lrange mylist 0 -1
1) "hello"
2) "hello1"
3) "hello2"
127.0.0.1:6379> RPOPLPUSH mylist myotherlist #移除列表的最后一个元素怒=,将他移动到新的列表中!
"hello2"
127.0.0.1:6379> lrange myotherlist 0 -1
1) "hello2"
127.0.0.1:6379> lrange myslist 0 -1 #查看目标列表中,确实存在该值
(empty list or set)
127.0.0.1:6379> lrange mylist 0 -1 #查看原来的列表
1) "hello"
2) "hello1"
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 "wordl" "other"
(integer) -1
127.0.0.1:6379> linsert mylist before "world" "other"
(integer) 3
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"
小结:redis中lsit实际上是一个链表,before node after,left,right都可以插入值
如果key不存在,创建新链表
如果key存在,新增内容
如果移除了所有值,空链表,也代表不存在!
在两边插入或者改动值,效率最高!中间元素,相对来说效率会低一些
消息排队!消息队列(Lpush Rpop),栈(Lpush lpop)