redis list

1.list增操作

1.左侧添加lpush key value value …… 右侧添加rpush key value value…… lpushx/rpushx正常和前面的一样,但是没有对应的键会报错,批量也可以正常添加

127.0.0.1:6379> lpush person lixue zhangsan wangwu zhangliu
(integer) 4
127.0.0.1:6379> lpush person zhao
(integer) 5
127.0.0.1:6379> rpush person zheng
(integer) 6
127.0.0.1:6379> LRANGE person 0 10
1) "zhao"
2) "zhangliu"
3) "wangwu"
4) "zhangsan"
5) "lixue"
6) "zheng"
127.0.0.1:6379> lpushx person lo
(integer) 7
127.0.0.1:6379> lpushx lixue lixue
(error) WRONGTYPE Operation against a key holding the wrong kind of value

 2.在指定一个值前或后添加 linsert key before/after value value

127.0.0.1:6379> linsert person before zhangsan 1
(integer) 11
127.0.0.1:6379> LRANGE person 0 10
 1) "nihao"
 2) "nv"
 3) "li"
 4) "lo"
 5) "zhao"
 6) "zhangliu"
 7) "wangwu"
 8) "1"
 9) "zhangsan"
10) "lixue"
11) "zheng"
127.0.0.1:6379> linsert person after zhangsan 2
(integer) 12
127.0.0.1:6379> LRANGE person 0 10
 1) "nihao"
 2) "nv"
 3) "li"
 4) "lo"
 5) "zhao"
 6) "zhangliu"
 7) "wangwu"
 8) "1"
 9) "zhangsan"
10) "2"
11) "lixue"

 2.list删除操作

1.删除key中的某个值,从左/右侧一次删除,返回值为删除的元素lpop/rpop key

127.0.0.1:6379> lpop person
"lisi"
127.0.0.1:6379> LRANGE person 0 10
1) "zhangsan"
2) "lixue"
3) "nv"
4) "li"
127.0.0.1:6379> rpop person
"li"
127.0.0.1:6379> LRANGE person 0 10
1) "zhangsan"
2) "lixue"
3) "nv"

2.只保留剩下某个范围的 ltrim key start end

127.0.0.1:6379> lrange person 0 10
 1) "0"
 2) "8"
 3) "7"
 4) "6"
 5) "5"
 6) "4"
 7) "3"
 8) "2"
 9) "1"
10) "zhangsan"
11) "lixue"
127.0.0.1:6379> ltrim person 9 10
OK
127.0.0.1:6379> lrange person 0 10
1) "zhangsan"
2) "lixue"

3.删除规定值的一些元素lrem key count value,返回值为被删除数量,删除前几个值等于value的元素,如果没有对应的返回0

 

127.0.0.1:6379> lrange person 0 10
1) "zhangsan"
2) "lixue"
127.0.0.1:6379> lrem person 2 lixue
(integer) 1
127.0.0.1:6379> lrange person 0 10
1) "zhangsan"
127.0.0.1:6379> lpush person lixue lixue lixue
(integer) 4
127.0.0.1:6379> lrem person 2 lixue
(integer) 2
127.0.0.1:6379> lrange person 0 10
1) "lixue"
2) "zhangsan"

 3.查看操作

1.根据key值查看相应的list中的值的集合LRANGE key start end,注意这里面一定要有起止范围,否则会报错

127.0.0.1:6379> LRANGE person 0 10
1) "zhangsan"
2) "lixue"
3) "nv"
127.0.0.1:6379> LRANGE person
(error) ERR wrong number of arguments for 'lrange' command

 2.根据key查看list元素数量,llen key 返回正常数量值

127.0.0.1:6379> llen person
(integer) 12

 3返回对应索引的值,lindex key index,这里面注意,下标不是数字会报错,下标超出会但会nil

127.0.0.1:6379> lindex person 1
"8"
127.0.0.1:6379> lindex person 10
"lixue"
127.0.0.1:6379> lindex person p
(error) ERR value is not an integer or out of range
127.0.0.1:6379> lindex person 19
(nil)
127.0.0.1:6379>

 4.修改list

修改某个index的对应值 lset key index value

127.0.0.1:6379> lset person 0 li
OK
127.0.0.1:6379> lrange person 0 10
1) "li"
2) "zhangsan"
127.0.0.1:6379>

 

posted @ 2019-03-08 13:38  程序猿雪儿  阅读(156)  评论(0编辑  收藏  举报