Redis two basic data structure of the list (list)

There are five basic Redis data structures, respectively: string (string), List (lists), SET (set), the hash (hash) and zset (ordered set).
Today it is about the list (list) this data structure, here is a list of a linked list structure (two-way list), fetching the elements or remove elements of time complexity is O (1).
Also here, by way of demonstration operations command:
Left to right, the value inserted into the list: lpush books java python golang 
value from right to left inserted into the list: rpush books java python golang 
takes a value of several positions (starting from 0): lindex books 0 (Note: when using this command, the time complexity is O (n), because the list is essentially a list, when the number of so many elements when using this command efficiency slow) 
Note: if the next number is negative when expressed as the first of several corresponding reciprocal, that is, -1 indicates a penultimate 
left to right remove the first element in the list: lpop books 
from right to left removes the first element in the list: rpop books

Here draw a diagram to illustrate the process of insertion elements

Note: When the elements in the list using lpop or rpop delete all, this list will automatically be deleted, the memory will be recycled.

Similarly, using the procedure rpush insertion elements to the list is a view, opposite

 

 Note: there is a command with the keys * This command is to look at all the key, but generally do not use this command, because when a very large number of key time, will affect the performance of redis.

Throughout the specified range of data acquired in list: lrange 0 -1 
Note: The first number represents the starting position, the second number represents the end position (when the number is negative, it means that the penultimate corresponding to several)

如果使用lpop或rpop删除列表中的全部元素会比较麻烦,尤其当列表中的元素非常多的时候;这个时候可以时候用ltrim删除列表中的全部元素,
命令为:ltrim books 1 0 (其中这个命令是保留的意思,第一个数字代表起始位置,第二个数字代表结束位置,即表示要保留的列表的区间,当第一个数字为1,第二个数字为零时,这个区间为负,即为全部删除列表中的元素;
也并不是只有1,0能全部删除列表中的元素,只要区间为负,都能删除 ,不过为了规范,还是用1和0)
#右边进左边出(队列)
rpush numbers 1 2 3 4 5 6
lpop numbers

#右边进右边出(栈)
rpush numbers 1 2 3 4 5 6
rpop numbers

获取列表中元素的个数:llen numbers

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/li666/p/12122024.html