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 right # 将一个值或者多个值,插入到列表的尾部(右)
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
######################################################
LPOP
RPOP
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379> LPOP list # 移除list的第一个元素
"three"
127.0.0.1:6379> RPOP list # 移除list的最后一个元素
"right"
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
######################################################
Lindex
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
######################################################
移除指定的值
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> LREM list 2 three
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
######################################################
trim 修建:list 截断
127.0.0.1:6379> RPUSH my list "hello"
(integer) 2
127.0.0.1:6379> RPUSH my list "hello1"
(integer) 4
127.0.0.1:6379> RPUSH my list "hello2"
(integer) 6
127.0.0.1:6379> RPUSH my list "hello3"
(integer) 8
127.0.0.1:6379> LRANGE list 0 -1
(empty array)
127.0.0.1:6379> LRANGE my list 0 -1
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379> LRANGE my list 0 -1
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379> keys *
1) "my"
127.0.0.1:6379> LRANGE my 0 -1
1) "list"
2) "hello"
3) "list"
4) "hello1"
5) "list"
6) "hello2"
7) "list"
8) "hello3"
127.0.0.1:6379> LTRIM my 1 2 # 通过下标截取指定的长度,list已经被修改,截断了只剩下截取的元素
OK
127.0.0.1:6379> LRANGE my 0 -1
1) "hello"
2) "list"
######################################################
rpoplpush # 移除列表的最后一个元素,将他移动到新的列表中
127.0.0.1:6379> LPUSH mylist "leett"
(integer) 1
127.0.0.1:6379> LPUSH mylist "leett1"
(integer) 2
127.0.0.1:6379> LPUSH mylist "leett2"
(integer) 3
127.0.0.1:6379> RPOPLPUSH mylist myotherlist
"leett"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "leett2"
2) "leett1"
127.0.0.1:6379> LRANGE myotherlist 0 -1
1) "leett"
######################################################
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存在,新增内容
- 如果移除了所有值,空链表,也代表不存在!
- 在两边插入或者改动值,效率最高!中间元素,相对来说效率会低一点~