目录
一、List类型使用
1.1、Lpush/rpush/Lrange
127.0.0.1:6379> lpush list1 1 2 3 4 5 6
(integer) 6
127.0.0.1:6379> rpush list2 11 22 33 44 55 66
(integer) 6
127.0.0.1:6379> type list1
list
127.0.0.1:6379> lrange list1 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
6) "1"
1.2、lpop/rpop
127.0.0.1:6379> lrange list1 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
6) "1"
127.0.0.1:6379> lpop list1
"6"
127.0.0.1:6379> lrange list1 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> rpop list1
"1"
127.0.0.1:6379> lrange list1 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
1.3、lindex
按照索引下标获取元素(从上到下)
127.0.0.1:6379> lrange list1 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
127.0.0.1:6379>
127.0.0.1:6379> lindex list1 0
"5"
127.0.0.1:6379> lindex list1 3
"2"
1.4、llen
获取列表中元素的个数
127.0.0.1:6379> llen list1
(integer) 4
127.0.0.1:6379> llen list2
(integer) 6
1.5、lrem key
数字N给定值2,相当于删除N个值等于2的元素。
127.0.0.1:6379> lpush list1 1 2 2 2 3 4
(integer) 6
127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "3"
3) "2"
4) "2"
5) "2"
6) "1"
127.0.0.1:6379> lrem list1 2 2
(integer) 2
127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
1.6、ltrim key
开始index结束index,截取指定范围的值后再赋值给key
127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> ltrim list1 0 2
OK
127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "3"
3) "2"
1.7、rpoplpush
源列表 目的列表
127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "3"
3) "2"
127.0.0.1:6379> lrange list2 0 -1
1) "11"
2) "22"
3) "33"
4) "44"
5) "55"
6) "66"
127.0.0.1:6379> rpoplpush list1 list2
"2"
127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "3"
127.0.0.1:6379> lrange list2 0 -1
1) "2"
2) "11"
3) "22"
4) "33"
5) "44"
6) "55"
7) "66"
1.8、lset key index value
127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "3"
127.0.0.1:6379> lset list1 1 java
OK
127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "java"
1.9、linsert key before/after
已有值,插入新值
127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "java"
127.0.0.1:6379> linsert list1 before java redis
(integer) 3
127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "redis"
3) "java"