默认从左边插入,打印从后面;类似于头插;
LPUSH KEY ELEMENT [ELEMENT.......]
RPUSH KEY ELEMENT [ELEMENT.......]
LRANGE KEY START END (0 -1 代表全部)
- 127.0.0.1:6379> keys *
- (empty array)
- 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 rightval # 将一个值或者多个值,插入到列表位的右边
- (integer) 4
- 127.0.0.1:6379> LRANGE list 0 -1
- 1) "three"
- 2) "two"
- 3) "one"
- 4) "rightval"
- 127.0.0.1:6379> LPUSH newlist val1 val2 # 从左边插入两个值
- (integer) 2
- 127.0.0.1:6379> LRANGE newlist 0 -1
- 1) "val2"
- 2) "val1"
- 127.0.0.1:6379> RPUSH newlist val3 val4 # 从右边插入两个值
- (integer) 4
- 127.0.0.1:6379> LRANGE newlist 0 -1
- 1) "val2"
- 2) "val1"
- 3) "val3"
- 4) "val4"
LPOP KEY [COUNT] //头删
RPOP KEY [COUNT] //尾删
- 127.0.0.1:6379> keys *
- 1) "list"
- 127.0.0.1:6379> LRANGE list 0 -1
- 1) "ten"
- 2) "nine"
- 3) "eight"
- 4) "seveen"
- 5) "six"
- 6) "five"
- 7) "flour"
- 8) "threee"
- 9) "two"
- 10) "one"
- 127.0.0.1:6379> LPOP list # 移除list的第一个元素
- "ten"
- 127.0.0.1:6379> LRANGE list 0 -1
- 1) "nine"
- 2) "eight"
- 3) "seveen"
- 4) "six"
- 5) "five"
- 6) "flour"
- 7) "threee"
- 8) "two"
- 9) "one"
- 127.0.0.1:6379> RPOP list # 移除list的最后一个元素
- "one"
- 127.0.0.1:6379> LRANGE list 0 -1
- 1) "nine"
- 2) "eight"
- 3) "seveen"
- 4) "six"
- 5) "five"
- 6) "flour"
- 7) "threee"
- 8) "two"
- 127.0.0.1:6379> LPOP list 2 # 移除list的开头的两个元素
- 1) "nine"
- 2) "eight"
- 127.0.0.1:6379> LRANGE list 0 -1
- 1) "seveen"
- 2) "six"
- 3) "five"
- 4) "flour"
- 5) "threee"
- 6) "two"
- 127.0.0.1:6379> RPOP list 3 # 移除list的末尾的3个元素
- 1) "two"
- 2) "threee"
- 3) "flour"
- 127.0.0.1:6379> LRANGE list 0 -1
- 1) "seveen"
- 2) "six"
- 3) "five"
LINDEX KEY INDEX
- 127.0.0.1:6379> LPUSH list val1 val2 val3 val4 val5
- (integer) 5
- 127.0.0.1:6379> LRANGE list 0 -1
- 1) "val5"
- 2) "val4"
- 3) "val3"
- 4) "val2"
- 5) "val1"
- 127.0.0.1:6379> LINDEX list 2 # 通过下标获得 list 中的某一个值!
- "val3"
- 127.0.0.1:6379> LINDEX list 1
- "val4"
- 127.0.0.1:6379> LINDEX list 0
- "val5"
- 127.0.0.1:6379> LINDEX list -1
- "val1"
LLEN KEY
- 127.0.0.1:6379> LPUSH list lval1 lval2 lval3
- (integer) 3
- 127.0.0.1:6379> LRANGE list 0 -1
- 1) "lval3"
- 2) "lval2"
- 3) "lval1"
- 127.0.0.1:6379> LLEN list # 返回列表的长度
- (integer) 3
- 127.0.0.1:6379> LPUSH list lval4 lval5
- (integer) 5
- 127.0.0.1:6379> LLEN list
- (integer) 5
LREM KEY COUNT ELEMENT
- 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"
LTRIM KEY START STOP
- 127.0.0.1:6379> LPUSH list one two three flour five
- (integer) 5
- 127.0.0.1:6379> LRANGE list 0 -1
- 1) "five"
- 2) "flour"
- 3) "three"
- 4) "two"
- 5) "one"
- 127.0.0.1:6379> LTRIM list 1 2 # 通过下标截取指定的长度,这个list已经被改变了,截断了,只剩下截取的元素!
- OK
- 127.0.0.1:6379> LRANGE list 0 -1
- 1) "flour"
- 2) "three"
RPOPLPUSH SOURCE DESTINATION
- 127.0.0.1:6379> keys *
- 1) "list"
- 127.0.0.1:6379> LRANGE list 0 -1
- 1) "one"
- 2) "two"
- 3) "three"
- 4) "flour"
- 5) "five"
- 127.0.0.1:6379> RPOPLPUSH list newList # 移除列表的最后一个元素,将他移动到新的列表中!
- "five"
- 127.0.0.1:6379> KEYS *
- 1) "newList"
- 2) "list"
- 127.0.0.1:6379> LRANGE list 0 -1 # 查看原来的列表
- 1) "one"
- 2) "two"
- 3) "three"
- 4) "flour"
- 127.0.0.1:6379> LRANGE newList 0 -1 # 查看目标列表中,确实存在该值!
- 1) "five"
LSET KEY INDEX ELEMENT
- # 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 KEY BEFORE/AFTER PIVOT ELEMENT
- # 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存在,新增内容
如果移除了所有值,空链表,也代表不存在!
在两边插入或者改动值,效率最高! 中间元素,相对来说效率会低一点~