List列表类型

HeJin大约 3 分钟数据库技术Redis

基本的数据类型,列表。在Redis里面,我们可以把List玩成栈、队列、阻塞队列。

所有的List命令都是以l开头的。Redis命令不区分大小写。

###################################################################
# 1、lpush 和 rpush
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 rigth	# 将一个值或者多个值插入列表尾部(右)
(integer) 4
127.0.0.1:6379> LRANGE list 0 1
1) "three"
2) "two"
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "rigth"
###################################################################
# 2、lpop 和 rpop
127.0.0.1:6379> lrange list 0 -1	
1) "three"
2) "two"
3) "one"
4) "rigth"
127.0.0.1:6379> lpop list			# 移除列表的第一个元素
"three"
127.0.0.1:6379> rpop list			# 移除列表的最后一个元素
"rigth"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
###################################################################
# 3、lindex
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"
127.0.0.1:6379> lindex list 2
(nil)
127.0.0.1:6379>
###################################################################
# 4、llen
127.0.0.1:6379> LPUSH list one two three
(integer) 3
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> LLEN list			# 返回list的长度
(integer) 3
###################################################################
# 5、移除指定的值
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> LRANGE list 0 -1
1) "three"
2) "three"
3) "two"
127.0.0.1:6379> lrem list 2 three
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
###################################################################
# 6、ltrim
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> RPUSH mylist hello hello1 hello2 hello3
(integer) 4
127.0.0.1:6379> lrange list 0 -1
(empty array)
127.0.0.1:6379> lrange mylist 0 -1
1) "hello"
2) "hello1"
3) "hello2"
4) "hello3"
127.0.0.1:6379> ltrim mylist 1 2	# 通过下标截取指定的长度。这个list已经被改变,只剩下截取的元素。
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "hello1"
2) "hello2"
###################################################################
# 7、rpoplpush 移除列表最后一个元素,并移动到新的列表中
127.0.0.1:6379> RPUSH mylist hello hello1 hello2
(integer) 3
127.0.0.1:6379> lrange mylist 0 -1
1) "hello"
2) "hello1"
3) "hello2"
127.0.0.1:6379> rpoplpush mylist otherlist	# 移除列表最后一个元素,并移动到新的列表中
"hello2"
127.0.0.1:6379> lrange mylist 0 -1			# 查看原来的列表
1) "hello"
2) "hello1"
127.0.0.1:6379> lrange otherlist 0 -1		# 查看目标列表中,确实存在该值
1) "hello2"
###################################################################
# 8、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 item1			# 不存在列表,更新就会报错
(error) ERR index out of range
###################################################################
# 9、linsert 将某个具体的value插入到列表中某个元素的前面或者后面
127.0.0.1:6379> rpush mylist hello world
(integer) 2
127.0.0.1:6379> lrange mylist 0 -1
1) "hello"
2) "world"
127.0.0.1:6379> linsert mylist before "world" "other"	# 将某个具体的value插入到列表中某个元素的前面
(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"		# 将某个具体的value插入到列表中某个元素的后面
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1) "hello"
2) "other"
3) "world"
4) "new"

小结

  • 实际上是一个链表
  • 如果key不存在,创建新的链表
  • 如果key存在,新增内容
  • 如果移除了所有值,空链表,也代表不存在
  • 在两边插入或者改动值,效率最高。中间元素,相对来说效率会低一点