「这是我参与2022首次更文挑战的第5天,活动详情查看:2022首次更文挑战」
Redis的List操作
List(列表)
基本的数据类型,列表!
所有的list命令都是L开头的,Redis命令不区分大小写!
基本命令如下:(以下对列表的操作类似于栈操作)
1、从左向右添加数据:LPUSH ListName value
2、从右向左添加数据:RPUSH list value
3、获取list中的值:LRANGE ListName 0 -1
- 说明:取列表中的 0 到 -1 范围的值,即全部值。
4、从左向右弹出第一个数据并移除:LPOP ListName
5、从右向左弹出第一个数据并移除:RPOP ListName
6、获取list中指定索引的值:LINDEX ListName 索引值
- 说明:索引从0开始
7、获取指定list列表的长度:LLEN ListName
8、移除list列表中指定个数的指定值:LREM ListName 数量 值
- 说明:列表中可能有多个相同的值,则移除指定数量的值。
9、截取列表的指定位置范围的数据:LTRIM ListName start end
- 说明:从索引为start开始截取至end位置结束保存。(开区间)
10、将一个list列表中的最后一个数据移动到另一个列表中:RPOPLPUSH ListName1 ListName2
11、修改list列表中指定索引的值:LSET ListName index value
- 说明:若元素不存在则报错。
12、将指定值插入到列表中的某个元素的前面或者后面: LINSERT ListName before/after "被插入值" "插入的值"
小结
- 它实际上是一个链表,before Node after,left,right都可以插入值
- 如果key不存在,创建新的链表
- 如果key存在,新增内容
- 如果移除了所有的值,链表为空了,也代表不存在!
命令操作示例:
127.0.0.1:6379> LPUSH list one #从左向右向list中添加one
(integer) 1
127.0.0.1:6379> LPUSH list two
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> LRANGE list 0 1 #倒序取出(相当于操作栈)
1) "three"
2) "two"
127.0.0.1:6379> RPUSH list right #从右向左向list中添加right
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
3) "right"
127.0.0.1:6379> LPOP list
"two"
127.0.0.1:6379> RPOP list #从右向左弹出数据并移除
"right"
127.0.0.1:6379> LPUSH list abc #获取指定索引的值
(integer) 2
127.0.0.1:6379> LINDEX list 1
"one"
127.0.0.1:6379> LINDEX list 0
"abc"
127.0.0.1:6379> LRANGE list 0 -1
1) "abc"
2) "one"
127.0.0.1:6379> LLEN list #获取list长度
(integer) 2
127.0.0.1:6379> FLUSHDB #清空该数据库
OK
127.0.0.1:6379> LLEN list
(integer) 0
#lrem 移除指定的值!
127.0.0.1:6379> LRANGE list 0 -1
3) "two"
4) "one"
127.0.0.1:6379> LREM list 1 three #移除list列表中的1个three(从上到下)
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> LPUSH list three
(integer) 4
127.0.0.1:6379> LREM list 2 three #移除list列表中的2个three(从上到下)
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> RPUSH mylist "hello"
(integer) 1
127.0.0.1:6379> RPUSH mylist "hello1"
(integer) 2
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "hello1"
127.0.0.1:6379> LTRIM mylist 1 2 #截取mylist中的1到2的数据
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello1"
2) "hello2"
127.0.0.1:6379> RPOPLPUSH mylist myotherlist #将mylist中的最后一个数据移动到myotherlist中
"hello2"
127.0.0.1:6379> LRANGE mylist 0 -1 #查看原来列表
1) "hello1"
127.0.0.1:6379> LRANGE myotherlist 0 -1 #查看目标列表
1) "hello2"
127.0.0.1:6379> EXISTS list #判断list列表是否存在
(integer) 0
127.0.0.1:6379> LSET list 0 item #修改list中的第0个元素的值为item,如果不存在就报错
(error) ERR no such key
127.0.0.1:6379> LPUSH list value1 #从左向右向list中添加value1
(integer) 1
127.0.0.1:6379> LRANGE list 0 0 #获取list的第0个到第0个元素
1) "value1"
127.0.0.1:6379> LSET list 0 item #修改list中的第0个元素的值为item
OK
127.0.0.1:6379> LRANGE list 0 0
1) "item"
127.0.0.1:6379> LSET list 1 other #修改list中的第1个元素的值为other,若第第1个元素不存在则报错
(error) ERR index out of range
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" #向mylist列表中的world的前面插入other
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "other"
3) "world"
明天继续加油!