Iii. List-related operations of redis

Source: Internet
Author: User
=======================================Three types: list type =================================
Introduction: List type and operations
List is the result of a chain table, as long as the function is push, Pop, get all values in a range, etc,
The operation key is interpreted as the name of the linked list. The list type of redis is actually a two-way linked list with each sub-element being a string type.
We can add and delete elements from the head or tail of the linked list through the push and pop operations, so that the list can be both a stack and a queue.

1: lpush:
Add a string element to the header of the list corresponding to the key (advanced, similar to stack)
For example, lpush list1 "hello"
Lpush list1 "world"
Value: lrange list1 0-1 --> retrieve from the beginning to the end, and retrieve all the items in list1.
2: rpush
Add a string element at the end of the list corresponding to the key (first, similar to the queue)
Example: rpush list2 "Le"
Rpush list2 "yangjun"
Take: lrange list2 0-1 --> le yangjun Value
3: linsert:
Add a string before or after a specific position of the list corresponding to the key
For example, lpush list3 "one"
Lpush list3 "two"
Linsert list3 before one three ---> press a three between one and two
Lrange list2 0-1
4: lset:
Set the value of the specified subscript element in the list.
For example, lpush list5 one
Lpush list5 two
Lset list5 1 "four" ----> change the value of 1 in the following table to four.
5: lrem: Delete the value of the same element
Delete n elements with the same value from the list corresponding to the key (n <0 is deleted from the end, n = 0 all are deleted)
For example, lpush list5 one
Lpush list5 one
Lrem lise5 1 "one" ---> 1: delete several items. delete one element with the same one from list5.
Lrem lise5 0 "one" ---> delete all
6: ltrim:
Retain data within the value range of a specified key
For example, lpush list6 one
Lpush list6 two
Lpush list6 three
Ltrim list6 1-1 --> retain the value of subscript 1-1, delete all others
Ltrim list6 1 2 ---> retain the subscripts 1 and 2, and delete other
7: lpop:
Deletes an element from the list header and returns the deletion element.
Example: rpush list8 one
Rpush list8 two
Lpop list8 --> pop up one, that is, delete it, leaving only two
8: rpop:
Deletes an element from the end of the list and returns the deletion element.
Example: rpush list8 one
Rpush list8 two
Rpop list8 ---> two pops up at the end, with only one
9: rpoplpush:
Removes elements from the end of the first list and adds them to the header of the second list.
Example: list9: Three, hello list10: Hello, foo
Rpoplpush list9 list10 ---> hello, from the end of list9, and hello, pushed from the header to list10.
List10 = Hello, Hello, foo
10: lindex:
Returns the index position element in the list named key.
Example: list11: Three, two, one
Lindex list11 0 ---> Find the element 0 in the following table-> three
11: llen:
Returns the length of the list corresponding to the key.
For example, llen list11 --> Returns a few elements. For example, if one there is returned, 1 is returned.
========================================================== End ===================================================== =

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.