[Redis data structure] list type

Source: Internet
Author: User

The List Set Data Type in redis is a two-way linked list structure. The list element is of any string type. So how does redis manage and operate the list set data? The following describes the basic syntax usage of the redis list set:

1. Add/create a list object

# Syntax 1: lpush [list set] [element] # stack-based add elements. The list set is regarded as a stack type, which is advanced and later developed. Example: lpush list1 Hello lpush list1 world: the result of traversing the set is: World, hello; that is, the advanced and backward syntax 2: rpush [list set] [element] # adding an element in the queue, example of first-in-first-out: rpush list2 Hello rpush list2 world: traversal set result: Hello, world, first-in-first-out, keep the insert order

2. Traverse list set Elements

# Process syntax for traversal from the header to the end: lrange [set] [start Index] [end Index] example: lrange list1 0-1 # The result is world, hello lrange list2 0-1 # The result is hello. World states that the index position starts from 0 and-1 indicates the last element index.

3. Obtain the set elements of an index.

Syntax: lindex [list set] [Index] example: lindex list3 0 # if it does not exist, Nil is returned; otherwise, the element at the index location is returned.

4. Modify the set element of an index

# Modify the element syntax of the specified index location: lset [list set] [Index] [new element] example: lset list1 0 Hello lrange list1 0-1 # The position 0 before is world, now changes to hello

5. Get the size of the list set

Syntax: llen [list set] example: llen list1 # returns the set size, 2

6. insert an element

# The insert element is not completed by indexing, but by specifying the pre-and post-insert Syntax of an element value: linsert [list set] [before | after] [element] [element to be inserted] # example of list4 = {'hello', 'World', 'Hello: linsert list4 before Hello wang1 linsert list4 after hello wang2 Description: There are two Hello elements in the list4 set, each matching only one, matching starting from list. When executing before, the result is {'hang1', 'Hello', 'World', 'Hello'}; when executing after, {'hang1', 'Hello ', 'ang2', 'World', 'Hello '}.

7. Get the element list of a certain range

# Intercept the elements that are combined with the specified index range. syntax for deleting elements outside the range: ltrim [list set] [start Index] [end Index] example: ltrim list4 0 1 Description: returns the size of the captured set element. The traversal result is {'hang1', 'Hello '}

8. Auxiliary Methods

Syntax: rpoplpush [list set 1] [list set 2] example: rpoplpush list1 list2 Description: delete an element in list1 from the end and insert its stack to list2, and return the elements deleted by list1. Equivalent to executing rpop list1, lpush list2

9. Overall Structure Analysis

Because the list structure is a two-way linked list structure, it is OK to insert the header or to insert the table from the tail, lpush header insertion, lpop header deletion, rpush tail insertion, rpop tail deletion, and lrange are all traversal methods from the header to the end.

The structure is as follows:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/48/67/wKiom1QHxWaiGmiEAADBLxhKA4Y187.jpg "Title =" redis.png "alt =" wkiom1qhxwaigmieaadblxhka4y187.jpg "/>


This article from the "Java program" blog, please be sure to keep this source http://793404905.blog.51cto.com/6179428/1548586

[Redis data structure] list 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.