Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of commands related to list data types of redis and how to use

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will introduce redis's list data type related command analysis and how to use it. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.

Introduction to list list

A list is a simple list of strings (popular points, stored or strings), sorted in the order in which they are inserted. You can add an element to the head (left) or tail (right) of a list, which can contain up to ^ 32-1 elements (each list has more than 4 billion elements).

List in Redis is very similar to LinkedList in Java, the underlying layer is a linked list structure, list insert and delete operations are very fast, time complexity is 0 (1), unlike array structure insert and delete operations need to move data. Like an image, but the underlying list in redis is not as simple as a two-way linked list.

When the amount of data is small, its underlying storage structure is a piece of continuous memory, called ziplist (compressed list), which stores all elements next to each other and allocates a continuous piece of memory; when there is a large amount of data, it will become a quicklist (Quick linked list) structure.

But the simple linked list is also defective, the front and back pointers prev and next of the linked list will occupy more memory, will waste space, and will aggravate the fragmentation of memory. After redis 3.2, the mixed structure of ziplist+ linked list is used, which is called quicklist (Fast linked list).

Common commands add commands

Lpush key value

Insert elements from the left (insert one or more values into the list header)

127.0.0.1 lpush ids > lrange ids 1 (integer) 1127.0.0.1 > lrange ids 0-11) "1" 127.0.0.1 > lpush ids 2 (integer) 2127.0.1 > lrange ids 0-11) "2" 2) "1"

Rpush key value

Insert elements from the right (insert one or more values at the end of the list (rightmost))

127.0.0.1 lrange ids 6379 > rpush ids 3 (integer) 3127.0.0.1 lrange ids 0-11) "2" 2) "1" 3) "3"

Linsert key BEFORE | AFTER pivot value

Inserts an element before / after an element and returns the current list length. Note that no action is performed when the list does not exist or the specified element does not exist in the list.

/ / insert 0127.0.0.1 lrange ids 6379 > linsert ids before 30 (integer) 4127.0.0.1 lrange ids 0-11) "2" 2) "1" 3) "0" 4) "3" / / insert 0127.0.1) before element 3 insert 0127.0.1) > lrange ids 0-11) "2" 2) "1" 3) "0" 4) "3" 5) "4" query command

Lrange key start end

Gets the list of elements in the specified range of the list; returns an empty list if the start value is greater than the end value of the list

It has been shown above.

Lindex key index

Gets the element of the specified index subscript of the list

127.0.0.1 lindex ids 6379 > lindex ids-1 "4"

Llen key

Gets the length of the list; returns 0 if the list does not exist

127.0.0.1 6379 > llen ids (integer) 5 pop-up / delete command

Lpop key

Pop up the element from the left side of the list and return the header element

127.0.0.1 lpop ids 6379 > lrange ids 0-11) 1 "2) 0" 3) "3" 4) "4"

Rpop key

Pop up the element from the right side of the list and return the trailing element

127.0.0.1 rpop ids 6379 > lrange ids 0-11) "1" 2) "0" 3) "3"

Lrem key count value

Find an element equal to value from the list and delete it, which can be divided into three cases according to the difference of count:

Count > 0, starting from the header to the footer, removing the number of count elements

Count

< 0,从表尾开始向表头,移除数量为count的绝对值个元素; count = 0,移除表中所有与 value 相等的值 127.0.0.1:6379>

Lrem ids 0 3 (integer) 1127.0.0.1) lrange ids 0-11) "1" 2) "0"

Ltrim key start end

Trim a list, that is, let the list retain only the elements within the specified range, and all elements that are not within the specified range will be deleted

127.0.0.1 lrange ids 6379 > ltrim ids 0 0OK127.0.0.1:6379 > lrange ids 0-11) "1" modify command

Lset key index value

Change the value of the element of the specified subscript to value

127.0.0.1 0OK127.0.0.1:6379 6379 > lset ids 0 0OK127.0.0.1:6379 > lrange ids 0-11) "0" blocking pop-up command

Blpop key [key...] Timeout

Move out and get the first element of the list, and if there are no elements in the list, it blocks the list until the wait timeout (in seconds) or until a popup element is found.

Brpop key [key...] Timeout

Move out and get the last element of the list. If there are no elements in the list, it blocks the list until the wait times out or until a popup element is found.

Demo:

Open three reids connection windows, the first to execute blpop, the second to execute brpop, and the third to add:

You can see that windows 1 and 2 have been blocked here after execution, because there are no elements in the ids

Window 3 add: lpush ids 1 2 3 4 5 6

You can see that windows 1 and 2 immediately pop up the corresponding elements:

Application scenario

Message queues: lpop and rpush (or vice versa, lpush and rpop) can implement the functions of queues

Like list, comment list and ranking list on WeChat moments: the lpush command and lrange command can achieve the function of the latest list, insert new elements into the list each time through the lpush command, and then read the latest element list through the lrange command.

The above is the redis list data type related command analysis and how to use all the content, more and redis list data type related command analysis and how to use the related content can search the previous article or browse the following article to learn ha! I believe the editor will add more knowledge to you. I hope you can support it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report