Redis data type -- List, redis -- list

Source: Internet
Author: User

Redis data type -- List, redis -- list

The Redis list is a simple string list sorted by insertion order.

You can add an element to the header (left) or tail (right) of the list)

The LPUSH command inserts a new element into the header,

Insert a new element to the end of RPUSH.

When these two operations are executed on an empty Key, a new list is created.

Similarly, if all elements in the linked list are removed, the key is also deleted from the database. This is very convenient syntax, because they are called to use an empty list, just like they are called to use a nonexistent key value (CAN) as a parameter.

A list can contain up to 4294967295 elements. Each list contains more than 4 billion elements. From the perspective of time complexity, the main feature of the Redis list is that the insertion and deletion of elements at the beginning and end are fixed at a fixed time, even millions of inserts .. The access element at both ends of the list is very fast, but if you try to access the element in the middle of a very large list, it is very slow because it is an O (N) operation.

Create a timeline model in a social network, use LPUSH to add new elements to the user's timeline, and use LRANGE to receive some recently inserted elements.
You can use LPUSH and LTRIM together to create a list that never exceeds the specified number of elements, but remember that it is the last N elements.
The list can be used as a message transmission primitive.


Instruction learning:

1) LPUSH is used to insert one or more values to the header of the list key. When the execution is successful, the length of the list is returned.

2) LSET can set the value of the element whose key subscript is index to value. The list key must exist and the index cannot exceed the list length range.

3) The LPOP command removes the first element from the list and returns it

4) obtain the list element. The LINDEX command can obtain the value at the specified position. When the subscript (index) is a positive number, 0 indicates the first element, and 1 indicates the second element, the subscript can be a negative number.-1 indicates the last element of the List, and-2 indicates the second-to-last element of the list.

5) LINSERT can insert the value into the list key, which is located BEFORE or AFTER the value is success. LINSERT key BEFORE | AFTER each value

If the cursor does not exist in the list key, no operation is performed.

6) use the LREM command to remove list elements. Based on the value of the count parameter, remove the elements in the list that are equal to the value of the parameter.

  • Count> 0: search from the header to the end of the table and remove the elements equal to the value. The quantity is count.
  • Count <0: Search the table header from the end of the table and remove the elements equal to the value. The number is the absolute value of count.
  • Count = 0: Remove all values equal to the value in the table.
7) The LLEN command can get the length of the list.

8) LTRIM can trim a list so that the list retains only the elements within the specified range. All elements not within the specified range will be deleted.


Http://www.cnblogs.com/stephen-liu74/archive/2012/03/16/2351859.html





Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.