Redis Learning Note (iv)-list type of data type

Source: Internet
Author: User

  the Redis list type is actually a doubly linked list in which each child element is a string type. so [Lr]push and [Lr]pop] the algorithm time complexity of the command is O (1). The list will also record the length of the linked list. Therefore the Llen operation is also O (1). The maximum length of a linked list is (2 of 32 square-1). 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 interesting thing is that the pop operation of the list is also blocked version. When we [Lr]pop a list object is, 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.

  List related commands:

Lpush key string adds a string element to the header of the key corresponding to the list, returns 1 for success, 0 for key exists and is not a list type

  Rpop Key removes the element from the tail of the list and returns the element that was deleted. If key corresponds to list does not exist or is null returns NIL if key corresponding value is not list returned error

  Llen Key Returns the length of the list, key does not exist return 0, if the key corresponding type is not a list return error

  

Rpush key string adds a string element to the end of the key corresponding to the list, returns 1 for success, 0 for key exists and is not a list type

  Lpop Key removes the element from the head of the list and returns the deleted element. If key corresponds to list does not exist or is null returns NIL if key corresponding value is not list returned error

  

  Lrange Key Start End Returns the element within the specified interval, the subscript starts at 0, the negative value is calculated from the back, 1 is the last element of the countdown, and key does not exist. Returns an empty list

  LTrim key start end intercepts list, retains elements within specified interval, returns 1,key without return error

  

  LSet Key Index value sets the value of the element specified in the list, returns Ok,key successfully or the subscript does not have a return error

  

  Lrem Key count value removes the same element as count and value from the key corresponding list.

  

  blpop Key1...keyn timeout a left-to-right scan returns a LPOP operation on the first non-empty list and returns, such as Blpop List1 list2 list3 0, if the list does not exist, LIST2,LIST3 is non-null LIST2 and returns the element deleted from Lpop.

  

  If all the lists are empty or nonexistent, the timeout second is blocked, and a timeout of 0 means that it is blocked. When blocking, if a client has a push operation on any key in Key1...keyn, the first client that is blocked on that key will return immediately. If the timeout occurs, nil is returned. Sort of like a Unix select or poll.

  

  

  

  Brpop with Blpop not to repeat, one is to remove from the head one is removed from the tail.

rpoplpush srckey destkey removes the element from the tail of the srckey corresponding list and adds it to the header of the destkey corresponding to the list, and finally returns the value of the element being removed. The entire operation is atomic. If Srckey is empty or does not exist return nil.

Redis Learning Note (iv)-list type of data type

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.