redis-list常用命令
作者:互联网
list是双向链表
- list既可以被用作队列,先进先出FIFO,first-in-first-out;
- 也可以被用作栈,先进后出 FILO ,first-in-last-out
读写list
# 将name1 push到list1中
127.0.0.1:6379> lpush list1 name1
(integer) 1
#查找 从下标0 到 -1处的列表,相当于查询列表的全量值
127.0.0.1:6379> lrange list1 0 -1
1) "name1"
# push多个元素到list1中
127.0.0.1:6379> lpush list1 name2 name3 name4
(integer) 4
127.0.0.1:6379> lrange list1 0 -1
1) "name4"
2) "name3"
3) "name2"
4) "name1"
# 查询下标[0,1]处的元素,即为第1、2个元素,闭区间
127.0.0.1:6379> lrange list1 0 1
1) "name4"
2) "name3"
# 根据下标范围查询元素,如果查询下标超过实际下标,则只返回list有的部分
127.0.0.1:6379> lrange list1 0 10
1) "name4"
2) "name3"
3) "name2"
4) "name1"
#从右侧push元素到list1中
127.0.0.1:6379> rpush list1 age 1
(integer) 6
127.0.0.1:6379> LRANGE list1 0 -1
1) "name4"
2) "name3"
3) "name2"
4) "name1"
5) "age"
6) "1"
# list1中从左侧弹出一个元素
127.0.0.1:6379> lpop list1
"name4"
127.0.0.1:6379> lrange list1 0 -1
1) "name3"
2) "name2"
3) "name1"
4) "age"
5) "1"
# list1中从右侧弹出1个元素
127.0.0.1:6379> rpop list1
"1"
127.0.0.1:6379> lrange list1 0 -1
1) "name3"
2) "name2"
3) "name1"
4) "age"
127.0.0.1:6379>
# 获取list中元素的个数
127.0.0.1:6379> LLEN list1
(integer) 4
#如果list存在,从左侧向list中push元素,否则不尽兴任何操作
127.0.0.1:6379> LPUSHX list1 name
(integer) 5
127.0.0.1:6379> llen list1
(integer) 5
127.0.0.1:6379> lrange list1 0 -1
1) "name"
2) "name3"
3) "name2"
4) "name1"
5) "age"
127.0.0.1:6379> keys *
1) "list1"
127.0.0.1:6379> lpushx list2 name1
(integer) 0
127.0.0.1:6379> keys *
1) "list1"
127.0.0.1:6379> lpushx list1 name2
(integer) 6
# 如果list存在,从右侧向list中放入元素
127.0.0.1:6379> rpushx list4 name4
(integer) 0
127.0.0.1:6379>
# list中的元素可以重复
127.0.0.1:6379> lrange list1 0 -1
1) "name2"
2) "name"
3) "name3"
4) "name2"
5) "name1"
6) "age"
127.0.0.1:6379>
# 根据元素下标 查询元素
127.0.0.1:6379> LINDEX list1 1
"name"
127.0.0.1:6379> LINDEX list1 10
(nil)
127.0.0.1:6379>
# 根据元素名称 和个数 删除元素
# 从由左到由从list1中删除2个name2,如果list1中有3个name2,那么最右侧的会被保留
# 和pop的区别,都是删除,但是pop只能依次删除,lrem可以根据元素的value进行删除
127.0.0.1:6379> lrange list1 0 -1
1) "name2"
2) "name"
3) "name3"
4) "name2"
5) "name1"
6) "age"
127.0.0.1:6379> lrem list1 2 name2
(integer) 2
127.0.0.1:6379> lrange list1 0 -1
1) "name"
2) "name3"
3) "name1"
4) "age"
127.0.0.1:6379> lpop list1 age
(error) ERR wrong number of arguments for 'lpop' command
127.0.0.1:6379> lpop list1 age
(error) ERR wrong number of arguments for 'lpop' command
127.0.0.1:6379> lpop list1 1 age
(error) ERR wrong number of arguments for 'lpop' command
127.0.0.1:6379> lrem list1 1 age
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379> lpush list1 age1 age2 age3 age4
(integer) 4
127.0.0.1:6379> lpush list2 namea1 name2 name3 name4
(integer) 4
127.0.0.1:6379> lrange list1 0 -1
1) "age4"
2) "age3"
3) "age2"
4) "age1"
127.0.0.1:6379> lrange list2 0 -2
1) "name4"
2) "name3"
3) "name2"
127.0.0.1:6379> lrange list2 0 -1
1) "name4"
2) "name3"
3) "name2"
4) "namea1"
# 从右侧弹出1个元素,然后从左侧将该元素push到另一个list中
127.0.0.1:6379> RPOPLPUSH list1 otherlist
"age1"
127.0.0.1:6379> lrange otherlist 0 -1
1) "age1"
# 阻塞知道原有一个可用的list可以元素能用于pop操作
127.0.0.1:6379> BRPOPLPUSH list1 newlist 10
"age2"
127.0.0.1:6379> lrange newlist 0 -1
1) "age2"
127.0.0.1:6379> keys *
1) "otherlist"
2) "list2"
3) "newlist"
4) "list1"
# del 删除key
127.0.0.1:6379> del otherlist
(integer) 1
127.0.0.1:6379> keys *
1) "list2"
2) "newlist"
3) "list1"
# 从左侧弹出一个元素,或者阻塞等待有元素可用视,进行pop操作,超时时间设置为0
127.0.0.1:6379> BLPOP list 0
127.0.0.1:6379> BRPOP list 0
127.0.0.1:6379> lpush list1 name1 name2 name3 name4
(integer) 4
127.0.0.1:6379> lrange list1 0 -1
1) "name4"
2) "name3"
3) "name2"
4) "name1"
# 设置指定下标处,元素的值
127.0.0.1:6379> lset list1 2 evelyn
OK
127.0.0.1:6379> lrange list1 0 -1
1) "name4"
2) "name3"
3) "evelyn"
4) "name1"
127.0.0.1:6379> lrange list1 0 -1
1) "name4"
2) "name3"
3) "evelyn"
4) "name1"
# 将list由左到由进行截取为新的list
127.0.0.1:6379> lrange list1 0 -1
1) "name3"
2) "evelyn"
127.0.0.1:6379>
ltrim、rpoplpush、brpoplpush仅有左操作,没有由操作
应用场景
阻塞队列、消息队列
标签:127.0,list1,0.1,list,redis,6379,name2,name3,常用命令 来源: https://blog.csdn.net/github_39936816/article/details/117485517