The Redis list

Source: Internet
Author: User

Tag: New str means IDT intercepts 16px use img alt

The Redis list is a doubly linked list that can be used either as a stack or as a queue. Fortunately, the university has learned the data structure, but also the impression.

Stack: Advanced out, queue: FIFO

Redis Linked list operations:

Application Scenario Learning list link list: To obtain the latest 10 logged-in user information, if using sql: SELECT * from the user order by logintime desc limit 10; But the amount of data is large, this SQL down, the table all the data to be affected, the load on the database is relatively high, the need for the key field (ID or logintime) to set the index, too much set index is more expensive system resources.

Now with the Redis list: only 5 data is kept in the list, and an old data is deleted each time a new data is added. Each time can be directly from the linked list to obtain the required data, greatly saving all aspects of resources.

1. Start the Redis operating terminal and switch to the second database, which is empty:

,

2,Lpush operation: execute command Lpush (L is the left shorthand) command to add 5 data to the list, the list named Newlogin, indicating the newly logged on user, you can use the type Newlogin To see the type of Newlogin.

Now Redis's second database has a key is Newlogin list, which in turn saved Xiaoming, Jack, Jim, Zhangsan, Lisi five data.

3,Rpop operation: now to add a new data, delete a piece of data from the tail of the list, add a Xiaoli, and execute Rpop (R is shorthand for right) to remove the first added data (xiaoming) from the tail:

4,Llen, lrange operation:llen Newlogin Returns the length of the list. Lrange Newlogin 0 4 Returns the element of the list corresponding to the range, and returns all elements if the maximum subscript exceeds the length of the list.

Rpush is the addition of an element from the tail, Lpop is an element removed from the head, Rpush and Lpop are used together; Lpush and Rpop are used together.

5,ltrim action: Intercepts a subset of the elements in the list:

The Redis 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.