Using Redis (List List type operations)

Source: Internet
Author: User
Tags key string

List data type listing type

The list type is a bidirectional operation that adds a delete element from the head or tail of the linked list

List can be used as a stack or as a queue

Types of list lists application scenarios:

Get information on the latest 10 users

SELECT * from the user order by logintime desc limit 10;

Data is not complex, but the load on the database is relatively large

If you set the index to the keyword Logintime resource-consuming

Using Redis to achieve the above functions through list lists

Keep only the latest 10 data in list lists

Every time a new data comes in, it deletes an old piece of data.

Thus saving data resources and reducing server load pressure

List type operation

The Lpush key string adds a string element to the key corresponding to the list header

The Rpush key string adds a string element to the key corresponding to the list tail

Rpop key removes the element from the list tail and returns the deleted element

Lpop key removes the element from the list header and returns the deleted element

Llen key returns key corresponding to the list length key does not exist return 0 if key corresponds to a type other than list return error

Lrange Key start end returns the elements in the specified range starting with subscript 0

Ltrin key start end intercept list retains elements of the specified interval

Attention

Lpush and Rpop used together

Rpush and Lpop used together

Eg: Save only 5 data

Principle (Queue) List Namelogin Save the latest 3 data

Head >>> Add new users >>> laowen9

Laowen8

Laowen7

Laowen6

Tail >>> kick out old user >>> Laowen5

The operation starts as follows

Each update of the head deletes the trailing one and ends with only 5 data.

Lpush Namelogin laowen1//OK means add OK

Lpush Namelogin laowen2//OK means add OK

Lpush Namelogin Laowen3//OK means add OK

Lpush Namelogin laowen4//OK means add OK

Lpush Namelogin laowen5//OK means add OK

Lpush Namelogin laowen6//OK means add OK

Rpop Namelogin//Return to "laowen1" to delete the last data of list table

Lrange Namelogin 0 4

The return value is

1) "Laowen6"

2) "Laowen5"

3) "Laowen4"

4) "Laowen3"

5) "Laowen2"

Llen Namelogin//Returns (integer) 5//Returns the length of the data

Intercept Lsit preserves elements within a specified range

LTrim Namelogin 2 4//Return OK

Lrange Namelogin 0 2

The return value is

1) "Laowen5"

2) "Laowen4"

3) "Laowen3"

Summarize queue Stack Differences

Queue >>> Header Add data tail delete data

Add new users to the header >>> Laowen9

Laowen8

Laowen7

Laowen6

Tail kick out old users >>> Laowen5

Stack >>> head Add data header Delete data

Add new users to the header >>> Laowen9

Kick the head out of the old user >>> Laowen5

Laowen8

Laowen7

Laowen6

Using Redis (List List type operations)

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.