Redis笔记(List)
Redis中list类型的Redis key,在key中会保存value链表的head,tail地址,同时对于存储的链表有双向的索引。
可以通过help @list看到Redis的list相关命令
127.0.0.1:6379> help @list
BLPOP key [key ...] timeout
summary: Remove and get the first element in a list, or block until one is available
since: 2.0.0
BRPOP key [key ...] timeout
summary: Remove and get the last element in a list, or block until one is available
since: 2.0.0
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
LINDEX key index
summary: Get an element from a list by its index
since: 1.0.0
LINSERT key BEFORE|AFTER pivot value
summary: Insert an element before or after another element in a list
since: 2.2.0
LLEN key
summary: Get the length of a list
since: 1.0.0
LPOP key
summary: Remove and get the first element in a list
since: 1.0.0
LPUSH key value [value ...]
summary: Prepend one or multiple values to a list
since: 1.0.0
LPUSHX key value
summary: Prepend a value to a list, only if the list exists
since: 2.2.0
LRANGE key start stop
summary: Get a range of elements from a list
since: 1.0.0
LREM key count value
summary: Remove elements from a list
since: 1.0.0
LSET key index value
summary: Set the value of an element in a list by its index
since: 1.0.0
LTRIM key start stop
summary: Trim a list to the specified range
since: 1.0.0
RPOP key
summary: Remove and get the last element in a list
since: 1.0.0
RPOPLPUSH source destination
summary: Remove the last element in a list, prepend it to another list and return it
since: 1.2.0
RPUSH key value [value ...]
summary: Append one or multiple values to a list
since: 1.0.0
RPUSHX key value
summary: Append a value to a list, only if the list exists
since: 2.2.0
首先是最基本的命令,push和pop
LPUSH用于将值放入链表的最左端,RPUSH用于将值放入链表的最右端
相同的LPOP将最左端元素弹出,RPOP将最右的元素弹出
同向的命令组合使用,类似于栈的数据结构,先进后出,反向的命令组合使用则为链表。
如下图,LPUSH a b c d e f后,使用LPOP会依次弹出f e ….

使用LRANGE命令可以取出给定范围内的list值

LINDEX命令可以取出一个索引值位置的元素,比如

LSET命令可以设置一个索引值位置的元素

LREM可以移除元素,第一个参数是要移除的元素个数,如果是正数从左向右数个数,如果是负数,从右向左数删除,如果为0全部删除。第二个参数是要移除的元素的值。

这时利用索引操作,类似于一个数组的数据结构
Redis还可以实现阻塞的单播队列,利用BLPOP和BRPO命令

LTRIM命令可以删除索引位置start前和stop后的元素
比如ltrim list 1 -2会删除头尾各一个元素