[Redis Chapter 6] List

Source: Internet
Author: User

List is an internally adopted doubly linked list (double linked list) structure that adds elements at both ends of the list with an O (1) time complexity. The main function is push, pop, get all the values of a range, etc., key in the operation is understood as the name of the list.

The maximum length of a linked list is (2 of 32). We can add delete elements from the head or tail of the list by Push,pop operation. This allows the list to be used as either a stack or as a queue .


The pop operation of List has a blocking version, and when we [Lr]pop a list object, if the list is empty or does not exist, it returns nil immediately. However, the blocked version of B[lr]pop can be blocked, of course, can be added to the timeout period, after the timeout will also return nil. Why block versions of Pop, primarily to avoid polling.


As a simple example, if we use list to implement a Task Force column. The thread that performs the task can invoke the blocked version of the pop to get the task so that the polling can be avoided to check for the presence of a task. The worker thread can return immediately when the task comes, or it can avoid the delay caused by polling.


Lpush Key value Left insert

Rpush Key Value Right Insert

Lpop Key Left Popup

Rpop Key Right Popup

Blpop,brpop blocking left/right pop-up


Lpush

129.223.248.154:6379> Lpush members Ben
(integer) 1
129.223.248.154:6379> Lpush Members Jeff
(integer) 2
129.223.248.154:6379> Lpush members Mike Jeme
(integer) 6


Lpop

129.223.248.154:6379> Lpop Members
"Raymond"
129.223.248.154:6379> Rpop Members
"Ben."


Llen

129.223.248.154:6379> Llen Members
(integer) 4


Lrange (Lrange firstqueue 0-1 lists all element values in the list)

129.223.248.154:6379> Lrange Members 0 2
1) "Richard"
2) "Jemery"
3) "Mike"
129.223.248.154:6379> Llen Members
(integer) 4
129.223.248.154:6379> Lrange Members 0 3
1) "Richard"
2) "Jemery"
3) "Mike"
4) "Jeff"
129.223.248.154:6379> Lrange Members 0 4
1) "Richard"
2) "Jemery"
3) "Mike"
4) "Jeff"
129.223.248.154:6379> Lrange Members 0-1
1) "Richard"
2) "Jemery"
3) "Mike"
4) "Jeff"
5) "Derek"


Rpop

129.223.248.154:6379> Rpop Members
"Derek"
129.223.248.154:6379> Lpop Members
"Richard"
129.223.248.154:6379> Lrange Members 0-1
1) "Jemery"
2) "Mike"
3) "Jeff"


Lindex

129.223.248.154:6379> Lindex Members 1
"Mike."
129.223.248.154:6379> Llen Members
(integer) 3
129.223.248.154:6379> Rpush firstqueue 3 2 1
(integer) 3
129.223.248.154:6379> Lrange Firstqueue 0-1
1) "3"
2) "2"
3) "1"
129.223.248.154:6379> Lpush Secqueue 3 2
(integer) 2
129.223.248.154:6379> Lrange Secqueue 0-1
1) "2"
2) "3"


Rpoplpush Removes the element from the end of the first list and adds it to the head of the second list, and finally returns the value of the removed element, the entire operation being atomic. If the first list is empty or does not exist return nil


129.223.248.154:6379> Rpoplpush Firstqueue Secqueue
"1"
129.223.248.154:6379> Lrange Firstqueue 0-1
1) "3"
2) "2"
129.223.248.154:6379> Lrange Secqueue 0-1
1) "1"
2) "2"
3) "3"
129.223.248.154:6379>

This article is from the "Software Design and Development" blog, please be sure to keep this source http://yuanzhitang.blog.51cto.com/2769219/1789582

[Redis Chapter 6] List

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: info-contact@alibabacloud.com 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.