1、通过客户端连接redis

[root@localhost ~]# /usr/local/redis/bin/redis-cli -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

2、切换到第二个数据库 db1

127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> 

在这里插入图片描述

3、查看当前库所有key

127.0.0.1:6379[1]> keys *
(empty array)
127.0.0.1:6379[1]> 

4、从左边插入一个或多个值

127.0.0.1:6379[1]> lpush k1 a b c d
(integer) 4
127.0.0.1:6379[1]> keys *
1) "k1"
127.0.0.1:6379[1]> 

5、按照索引下标获得元素(从左到右)

127.0.0.1:6379[1]> lrange k1 0 -1
1) "d"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379[1]> 

在这里插入图片描述

6、针对key指定的list,从右边放入元素

127.0.0.1:6379[1]> rpush k2 a b c d
(integer) 4
127.0.0.1:6379[1]> lrange k2 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
127.0.0.1:6379[1]> 

在这里插入图片描述

7、返回list集合的长度

127.0.0.1:6379[1]> lrange k1 0 -1
1) "d"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379[1]> llen k1
(integer) 4
127.0.0.1:6379[1]> lrange k2 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
127.0.0.1:6379[1]> llen k2
(integer) 4
127.0.0.1:6379[1]> 

8、从左边弹出一个元素。弹出=返回+删除

127.0.0.1:6379[1]> lrange k2 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
127.0.0.1:6379[1]> lpop k2 1
1) "a"
127.0.0.1:6379[1]> lrange k2 0 -1
1) "b"
2) "c"
3) "d"
127.0.0.1:6379[1]> 

在这里插入图片描述

9、从右边弹出一个元素

127.0.0.1:6379[1]> lrange k2 0 -1
1) "b"
2) "c"
3) "d"
127.0.0.1:6379[1]> rpop k2 1
1) "d"
127.0.0.1:6379[1]> lrange k2 0 -1
1) "b"
2) "c"
127.0.0.1:6379[1]> 

在这里插入图片描述

10、从source中rpop一个元素,lpush到destination

127.0.0.1:6379[1]> lrange k1 0 -1
1) "d"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379[1]> rpoplpush k1 k3
"a"
127.0.0.1:6379[1]> lrange k1 0 -1
1) "d"
2) "c"
3) "b"
127.0.0.1:6379[1]> lrange k3 0 -1
1) "a"
127.0.0.1:6379[1]> 

在这里插入图片描述

11、根据索引从集合中取值,只是查看值,并没有弹出值

127.0.0.1:6379[1]> lrange k1 0 -1
1) "d"
2) "c"
3) "b"
127.0.0.1:6379[1]> lindex k1 1
"c"
127.0.0.1:6379[1]> lrange k1 0 -1
1) "d"
2) "c"
3) "b"
127.0.0.1:6379[1]> 

12、在pivot指定的值前面或后面插入value

127.0.0.1:6379[1]> lpush k4 n m l k j i h
(integer) 7
127.0.0.1:6379[1]> lrange k4 0 -1
1) "h"
2) "i"
3) "j"
4) "k"
5) "l"
6) "m"
7) "n"
127.0.0.1:6379[1]> linsert k4 after k x
(integer) 8
127.0.0.1:6379[1]> lrange k4 0 -1
1) "h"
2) "i"
3) "j"
4) "k"
5) "x"
6) "l"
7) "m"
8) "n"
127.0.0.1:6379[1]>

在这里插入图片描述

13、只能针对存在的list执行lpushx

127.0.0.1:6379[1]> keys *
1) "k1"
2) "k3"
3) "k2"
4) "k4"
127.0.0.1:6379[1]> lpushx k5 a b c d
(integer) 0
127.0.0.1:6379[1]> keys *
1) "k1"
2) "k3"
3) "k2"
4) "k4"
127.0.0.1:6379[1]> 

14、根据count指定的数量从key对应的list中删除value具体执行时从左往右删除,遇到一个删一个,删完为止

127.0.0.1:6379[1]> rpush k5 a b c a b c d a b c c a
(integer) 12
127.0.0.1:6379[1]> lrange k5 0 -1
 1) "a"
 2) "b"
 3) "c"
 4) "a"
 5) "b"
 6) "c"
 7) "d"
 8) "a"
 9) "b"
10) "c"
11) "c"
12) "a"
127.0.0.1:6379[1]> lrem k5 5 c
(integer) 4
127.0.0.1:6379[1]> lrange k5 0 -1
1) "a"
2) "b"
3) "a"
4) "b"
5) "d"
6) "a"
7) "b"
8) "a"
127.0.0.1:6379[1]> 

15、把指定索引位置的元素替换为另一个值

127.0.0.1:6379[1]> lrange k5 0 -1
1) "a"
2) "b"
3) "a"
4) "b"
5) "d"
6) "a"
7) "b"
8) "a"
127.0.0.1:6379[1]> lset k5 2 c
OK
127.0.0.1:6379[1]> lrange k5 0 -1
1) "a"
2) "b"
3) "c"
4) "b"
5) "d"
6) "a"
7) "b"
8) "a"
127.0.0.1:6379[1]> 

16、仅保留指定区间的数据,两边的数据被删除

127.0.0.1:6379[1]> lrange k5 0 -1
1) "a"
2) "b"
3) "c"
4) "b"
5) "d"
6) "a"
7) "b"
8) "a"
127.0.0.1:6379[1]> ltrim k5 1 3
OK
127.0.0.1:6379[1]> lrange k5 0 -1
1) "b"
2) "c"
3) "b"
127.0.0.1:6379[1]> 

在这里插入图片描述

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部