List类型讲解

List类型讲解

压入数据和查看

注:所有list的操作命令都以l开头,redis命令不区分大小写

# lpush 从左边压入
# rpush 从右边压入
# lrange key start end 获取数组从s到e下标中的数据

127.0.0.1:6379[3]> lpush list one # 将one压进list 
(integer) 1
127.0.0.1:6379[3]> lpush list two 
(integer) 2
127.0.0.1:6379[3]> lpush list three
(integer) 3
127.0.0.1:6379[3]> get list # 获取list方式不对
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379[3]> lrange list 0 -1 # 获取list从0开始的所有数据
1) "three"
2) "two"
3) "one"
127.0.0.1:6379[3]> lrange list 0 2 # 获取list从0开始到2的数据
1) "three"
2) "two"
3) "one"
127.0.0.1:6379[3]> lrange list 0 1
1) "three"
2) "two"
127.0.0.1:6379[3]> lrange list 0 0
1) "three"
127.0.0.1:6379[3]> Rpush list right # 从右边插入数据
(integer) 4
127.0.0.1:6379[3]> lrange list 0 0
1) "three"
127.0.0.1:6379[3]> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379[3]> rpush list right1
(integer) 5
127.0.0.1:6379[3]> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
5) "right1"

移除和下标获取

# lpop 从左边移除第一个元素
# rpop 从右边移除第一个元素
# lindex key index 获取某个list的index下标的数据

127.0.0.1:6379[3]> lpop list
"three"
127.0.0.1:6379[3]> rpop list
"right1"
127.0.0.1:6379[3]> lrange list 0 -1
1) "two"
2) "one"
3) "right"
127.0.0.1:6379[3]> lindex list 1
"one"
127.0.0.1:6379[3]> lindex list 0
"two"

获取list长度和移除指定值

# llen 获取list长度
# lrem key count value 移除指定数量的值

127.0.0.1:6379[3]> llen list
(integer) 3

127.0.0.1:6379[3]> lpush list one
(integer) 4
127.0.0.1:6379[3]> lrange list 0 -1
1) "one"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379[3]> lrem list 1 one
(integer) 1
127.0.0.1:6379[3]> lrange list 0 -1
1) "two"
2) "one"
3) "right"

修剪

# ltrim key start end 截取某个list中从start到end下标之间的值
127.0.0.1:6379[3]> lpush mylist hello0
(integer) 1
127.0.0.1:6379[3]> lpush mylist hello1
(integer) 2
127.0.0.1:6379[3]> lpush mylist hello2
(integer) 3
127.0.0.1:6379[3]> lpush mylist hello3
(integer) 4
127.0.0.1:6379[3]> lpush mylist hello4
(integer) 5
127.0.0.1:6379[3]> lrange mylist 0 -1
1) "hello4"
2) "hello3"
3) "hello2"
4) "hello1"
5) "hello0"
127.0.0.1:6379[3]> ltrim mylist 1 2
OK
127.0.0.1:6379[3]> lrange mylist 0 -1
1) "hello3"
2) "hello2"

rpoplpush命令

# rpoplpush source destination 将源列表中的最右边的值弹出并赋值给新列表
127.0.0.1:6379[3]> lpush list hello0
(integer) 1
127.0.0.1:6379[3]> lpush list hello1
(integer) 2
127.0.0.1:6379[3]> lpush list hello2
(integer) 3
127.0.0.1:6379[3]> lrange list 0 1
1) "hello2"
2) "hello1"
127.0.0.1:6379[3]> lrange list 0 -1
1) "hello2"
2) "hello1"
3) "hello0"
127.0.0.1:6379[3]> rpoplpush list list1
"hello0"
127.0.0.1:6379[3]> lrange list 0 -1
1) "hello2"
2) "hello1"
127.0.0.1:6379[3]> lrange list1 0 -1
1) "hello0"
127.0.0.1:6379[3]>

lset设置list的值

# lset key index value 设置list中第index元素值为value
127.0.0.1:6379[3]> flushdb # 清空数据库
OK
127.0.0.1:6379[3]> keys *
(empty list or set)
127.0.0.1:6379[3]> lset list 0 hello # 设置list 
(error) ERR no such key
127.0.0.1:6379[3]> lpush list wakaka
(integer) 1
127.0.0.1:6379[3]> lrange list 0 -1
1) "wakaka"
127.0.0.1:6379[3]> lset list 0 hello
OK
127.0.0.1:6379[3]> lrange list 0 -1
1) "hello"

linsert

# linsert key after/before pivot value 在pivot前面或后面插入value
127.0.0.1:6379[3]> rpush list hello
(integer) 1
127.0.0.1:6379[3]> rpush list world
(integer) 2
127.0.0.1:6379[3]> lrange list 0 -1
1) "hello"
2) "world"
127.0.0.1:6379[3]> linsert list before hello ohmydream
(integer) 3
127.0.0.1:6379[3]> lrange list 0 -1
1) "ohmydream"
2) "hello"
3) "world"
127.0.0.1:6379[3]> linsert list after hello 123
(integer) 4
127.0.0.1:6379[3]> lrange list 0 -1
1) "ohmydream"
2) "hello"
3) "123"
4) "world"

小结

  • 实际上是一个链表,before Node after ,在左侧和右侧都可以插入之
  • 如果key不存在,创建新的链表
  • 如果key存在,新增内容
  • 如果移除了所有的值,空链表,也代表不存在
  • 在两边插入或者改动值,效率最高。操作中间数据,相率相对低
posted @ 2022-02-24 15:02  Oh,mydream!  阅读(161)  评论(0编辑  收藏  举报