天天看点

redis list命令操作

1.将值追加到列表

rpush key value [value ...]

summary: append one or multiple values to a list

since: 1.0.0

127.0.0.1:6379> rpush mylist value1 value2 value3

(integer) 3

2.获取列表的长度

llen key

summary: get the length of a list

127.0.0.1:6379> llen mylist

3.获取并移除列表中第一个元素

blpop key [key ...] timeout

summary: remove and get the first element in a list, or block until one is available

since: 2.0.0

127.0.0.1:6379> blpop mylist 3

1) "mylist" ##列表key

2) "value1" #列表当前第一个值

1) "mylist"

2) "value2"

2) "value3"

127.0.0.1:6379> blpop mylist 3 列表已经不存在value

(nil)

(3.78s)

4.获取并移除列表中的最后一个元素

brpop key [key ...] timeout

summary: remove and get the last element in a list, or block until one is available

127.0.0.1:6379> brpop list1 3

1) "list1" #列表键名

2) "value3" #列表最后一个值

5.出栈list中的一个value,并放入另一个list中,并返回该值

brpoplpush source destination timeout

summary: pop a value from a list, push it to another list and return it; or block until one is available

since: 2.2.0

127.0.0.1:6379> brpoplpush list1 list2 3

"value2"

6.获取指定位置的value值,返回的是该位置的值,无值或超出边界返回nil

lindex key index

summary: get an element from a list by its index

7.在列表一个元素的之前或之后插入一个元素,返回当前列表的长度

linsert key before|after pivot value

summary: insert an element before or after another element in a list

127.0.0.1:6379> linsert ml before v2 value2

(integer) 5  在v2之前插入值value2

8.栈顶元素出栈

lpop key

summary: remove and get the first element in a list

127.0.0.1:6379> lpop ml

"v1"

9.向list中添加一个或多个value,后加入的值,index在前(将元素压入栈顶)

lpush key value [value ...]

summary: prepend one or multiple values to a list

127.0.0.1:6379> lpush list2 val1 val2 val3 val4 val5

(integer) 6

127.0.0.1:6379> lindex list2 0

"val5"

10.只有当列表存在时,才从栈顶压入元素

lpushx key value

summary: prepend a value to a list, only if the list exists

11.获取指定范围的list的value值

lrange key start stop

summary: get a range of elements from a list

12.从列表中移除元素(当list中存在多个重复的值时,count确定要移除几个value)

lrem key count value

summary: remove elements from a list

13.通过元素的索引index设置value

lset key index value

summary: set the value of an element in a list by its index

127.0.0.1:6379> lset list2 3 namew #修改第三个位置的值

ok

14. 

ltrim key start stop

summary: trim a list to the specified range

15.移除并获取列表中的最后一个元素

rpop key

summary: remove and get the last element in a list

16.移除列表中的最后一个元素,追加到另一个列表中,并返回该值

rpoplpush source destination

summary: remove the last element in a list, append it to another list and return it

since: 1.2.0

17.将值追加到列表中,只有当这个列表已经存在

rpushx key value

summary: append a value to a list, only if the list exists

继续阅读