List types in Redis and common commands

Source: Internet
Author: User
Tags redis

The list type in Redis is somewhat similar to an array in a programming language, and the list type, as shown in the following illustration, is actually a two-way linked list structure that controls the data in the list by Lpush, Lpop, Rpush, rpop commands, so the list type can be used as a stack, can also be used as a queue.

Redis Chain List List type

The efficiency of inserting or deleting elements from both ends of a list can be very efficient in terms of element insertion and deletion, even if the millions record is already stored in the list, or in a very short time. It is, however, important to note that if the element insertion or deletion is acting on the middle of the list, it will be very inefficient. It's not hard to understand that for developers with good data structure fundamentals.

Therefore, Redis linked lists are often used for Message Queuing services to complete message exchange between multiple programs.

The following are some common commands for list types (lists):

1. "Lpush Key value" Inserts an element into the queue header
2. "Rpush key value" Inserts an element from the tail
3. "Lpop key" deletes an element from the head of the queue
4. "Rpop key" deletes an element from the tail of the queue and returns the value of the deleted element
5. "Llen" returns the length of the queue, that is, how many elements are inside. No key returns 0, and a key that is not a queue type returns an error.
6. "Lrange Key Start" Returns the element information between the queue from start to end.
7. "LTrim Key start end" intercepts a queue, retaining only the elements within the specified interval.

Common Actions for list:

(1) Lpush and Rpush

Lpush represents adding a string element to the head of the list corresponding to the key. For example: Lpush list Hello

Rpush represents the addition of a string element at the end of the list corresponding to key. For example: Rpush list World

(2) Lpop and Rpop

The lpop represents the deletion of the element from the head of the list and returns the element.

The rpop represents the deletion of the element from the head of the list and returns the element.

(3) Lrange

Extracts the elements within the specified range. For example: Lrange list 0-1,0 is the subscript for the first element,-1 is the subscript of the last element.

(4) Linsert

Adds a string before or after a specific location in the key's corresponding list, for example:

Linsert list before Hello Redis indicates that an element is inserted before hello in the list table Redis

(5) LSet

Sets the element value of the specified subscript in the list, for example: LSet list 1 Database, which means to replace the element labeled 1 in the list with


(6) Lrem

Remove n and value identical elements from the list of key, if N<0 is deleted from the tail, n=0 means delete all.

For example: Lrem list 1 Hello

(7) LTrim

Preserves data within the specified key range. For example: LTrim list 1-1, which retains only the value of the following table from 1 to the last element.

(8) Rpoplpush

Removes the element from the tail of the first list and adds it to the head of the second list. For example:

(9) Lindex

Returns an element of the index position in the list named key. For example: Lindex list 1 Returns the element labeled 1 in the list.

(a) Llen

Returns the length of the key corresponding to the list.

command example:

Redis about List type operations

1> Stack Features: Advanced out, LIFO
2> queue characteristics: Advanced first out, backward out of the
list is a linked list structure, can be done as a stack or as a queue processing data.
Lpush mylist "World"/ Place the world
Lpush mylist "Hello"//to the tail of the mylist into the tail of the mylist Hello
lrange 0-1//Get all the elements in the MyList
Rpush mylist2 "Hello" Store Hello
Rpush mylist2 "World" into the head of MyList//to MyList head into World
Lrange 0-1
Linsert mylist2 the World "before" Insert "Xhy"
LSet mylist2 0 "Example"//Set the value of the No. 0 element in the mylist to "example"
Lpush mylist_rem "One"
in front of the world element in MyList Lpush Mylist_rem "One"
Lpush mylist_rem ' One "
Lrem mylist_rem 1" A//delete 1 of the elements
Lpush Mylist_ltrim
Lpush Mylist_ltrim "Two"
Lpush Mylist_ltrim "three"
Lpush Mylist_ltrim "Four"
Lpush Mylist_ltrim "Five" LTrim Mylist_ltrim 2 3//except for the 2nd and 3rd elements, delete the other elements

Lpush List_pop 1
Lpush List_pop 1
Lpush List_pop 1
Lrange List_pop 0-1
Lpop List_pop//delete the first element of the List_pop from the head
Rpop List_pop//delete the first element of List_pop from the tail
Lpush list_1 0
Lpush list_1 1
Lpush list_1 2
Lpush list_1 3
Lrange list_1 0-1
Lpush list_2 4
Lpush list_2 5
Lpush List_2 6
Lpush list_2 7
Lrange list_2 0-1
Rpoplpush list_1 list_2//from the Tail of list_1 pops up the 1th element and adds it to List_2 's head.
Lindex list_2 0//Remove list_2 No. 0 Element
Lindex list_2 1
Llen//Returns the length of the list

command example:

1. Lpush/lpushx/lrange:
/> redis-cli# starts Redis client tools at the shell prompt.
Redis> del mykey
(integer) 1
#mykey键并不存在, which creates the key and the list associated with it, and then inserts values from left to right in the parameter.
Redis> Lpush MyKey a b c d
(integer) 4
Redis> lrange mykey 0 2
1) "D"
2) "C"
3) "B"
#取链表中的全部元素, where 0 represents the first element,-1 represents the last element.
Redis> lrange mykey 0-1
1) "D"
2) "C"
3) "B"
4) "A"
#mykey2键此时并不存在, so the command will not be What to do, and the return value is 0.
Redis> lpushx mykey2 e
(integer) 0
#可以看到mykey2没有关联任何List Value.
Redis> lrange mykey2 0-1
(empty list or set)
#mykey键此时已经存在, so the command is inserted successfully and returns the number of the current element in the list. The
Redis> lpushx mykey e
(integer) 5
#获取该键的List The header element of value.
Redis> lrange mykey 0 0
1) "E"

2. Lpop/llen:
Redis> Lpush MyKey a b c d
(integer) 4
Redis> lpop mykey< br> "D"
Redis> lpop mykey
#在执行lpop命令两次后, two elements of the header of the list have been ejected, and the number of elements in the list is 2
Redis> llen mykey
(integer) 2

   3. Lrem/lset/lindex/ltrim:
Redis> Lpush MyKey a b c d a c
(integer) 6
#从头部 (left) to the tail (right) variable list, delete 2 values equal to a, and return the actual number of deleted values 。
Redis> lrem MyKey 2 a
(integer) 2
Redis> lrange mykey 0-1
1) "C"
2) "D"
3) "C"
4) "B"
#获取索引值为1 the element value of the second element of the head. The element value of the
Redis> lindex mykey 1
#将索引值为1 (the second element of the head) is set to the new value E.
Redis> LSet MyKey 1 e
Redis> lindex mykey 1
#索引值6超过了链表中元素的数量, this command returns nil.
Redis> lindex MyKey 6
#设置的索引值6超过了链表中元素的数量, the setting fails, and the command returns an error message.
Redis> lset MyKey 6 hh
(Error) ERR index out of range
#仅保留索引值0到2之间的3个元素, note that both the No. 0 and 2nd elements are preserved.
Redis> ltrim mykey 0 2
Redis> lrange mykey 0-1
1) "C"
2) "E"
3 "C"

4. Linsert:
Redis> del MyKey
(integer) 1
Redis> Lpush MyKey a b c d E
(integer) 5
Redis> Linsert MyKey before a A1
(integer) 6
#查看是否插入成功, from the result look has been inserted. Note that the index value of Lindex is 0-based.
Redis> lindex MyKey 0
#在e的后面插入新元素e2, from the return result look has been inserted successfully.
Redis> Linsert MyKey after e E2
(integer) 7
Redis> lindex MyKey 1
#在不存在的元素之前或之后插入新元素, the command operation failed and returned-1.
Redis> Linsert MyKey after k a
#为不存在的Key插入新元素, the command operation failed, returning 0.
Redis> Linsert Mykey1 after a A2
(integer) 0

5. Rpush/rpushx/rpop/rpoplpush:
#删除该键 to facilitate the subsequent test.
Redis> del MyKey
(integer) 1
#从链表的尾部插入参数中给出的values, the insertion order is inserted from left to right.
Redis> Rpush MyKey a b c D
(integer) 4
Redis> lrange MyKey 0-1
1) "A"
2) "B"
3) "C"
4) "D"
#该键已经存在并且包含4个元素, the RPUSHX command succeeds and inserts element e into the tail of the list.
Redis> rpushx MyKey E
(integer) 5
#通过lindex命令可以看出之前的rpushx命令确实执行成功, because an element with an index value of 4 is already a new element.
Redis> lindex MyKey 4
#由于mykey2键并不存在, the command does not insert data, and its return value is 0.
Redis> rpushx Mykey2 E
(integer) 0
#在执行rpoplpush命令前, take a look at the elements of the list in MyKey and pay attention to their position.
Redis> lrange MyKey 0-1
1) "A"
2) "B"
3) "C"
4) "D"
5) "E"
#将mykey的尾部元素e弹出, and then insert into the head of the Mykey2 (the atomic completion of these two-step operation).
Redis> Rpoplpush MyKey Mykey2
Redis> lrange MyKey 0-1
1) "A"
2) "B"
3) "C"
4) "D"
Redis> lrange Mykey2 0-1
1) "E"
#将source和destination设为同一键, moves the tail element in the MyKey to its head.
Redis> Rpoplpush MyKey MyKey
Redis> lrange MyKey 0-1
1) "D"
2) "A"
3) "B"
4) "C"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.