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

Redis list List slow operation

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "Redis list List slow operation". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The list of Redis is a double-chain structure. Like the LinkedList in java, the time complexity of inserting data into the linked list is very fast, but the time complexity of the query is O (n). For double-linked lists, it can be traversed from beginning to end or two-way traversal from end to end. This structure is very similar to our queue and stack. This structure is often used as asynchronous queues. Serialize the task structure that needs to be deferred into a string, put it into the Redis list, and another thread polls from this list for processing. Of course, our list also provides such an operation. Let's try some common command operations.

Queue (first-in, first-out)

A list is a first-in, first-out data structure commonly used in message queuing and asynchronous logic processing, which ensures the access order of elements

> rpush name hello world (integer) 2 > llen name (integer) 2 > lpop name "hello" > lpop name "world" > lpop name (nil) stack (first in, then out)

Stack is a first-in-first-out data structure, contrary to the order of queue traversal, our common badminton bucket

> rpush name hello world (integer) 2 > rpop name "world" > rpop name "hello" List slow operation

Lindex traverses the entire list, just like the get (int index) method of the linked list in java, which obtains the value of the current position according to index. The higher the index value, the worse the performance, and the execution time efficiency is O (n).

The ltrim command takes two parameters to get a list of interval ranges, and the ltrim command cleans up elements outside this range. We use this command to get a fixed-length list.

Lrange returns the elements in the specified interval in the list. Unlike ltrim, ltrim directly intercepts the data of a certain interval, and lrange returns the data of a certain interval.

Note: the index parameter can be negative, if it is-1, then get the penultimate element,-2 is the penultimate element, and so on, a bit like a Joseph ring

If ltrim name 1 0 means that there are no elements in this acquired interval, then the entire list will be cleared

> rpush name hello world i am mango (integer) 5 > lindex name 1 "world" > lindex name-1 "mango"

> ltrim name 0 1OK > lrange name 0 1 "hello"world" > ltrim name 1 0OK > llen name (integer) 0 Quick list

Let's think about a problem: if the structure of list is an object type, each object holds the address of the previous element and the address of the next element in a different disk space, and each object retains a pointer. During the search, we are bound to scan the disk back and forth every time, which is very expensive, and it becomes very troublesome to defragment the disk when the list is collected. So we can imagine that if our object is stored in contiguous space, whether it is very fast to find the element every time we query the next pointer, there is no need to scan the disk back and forth, and when recycling, we only need to reclaim this memory mark to reduce disk defragmentation.

In fact, redis also made such optimization, here we call the fast linked list (quicklist) structure, if the list elements are few, it will use a continuous memory storage called ziplist that is compressed list, when the amount of data is relatively large, ziplist will be converted to quicklist. Redis combines linked lists with ziplist to form quicklist, that is, ziplist uses two-way pointers to string together. Quicklist not only meets the performance of fast insertion and deletion, but also does not have too much spatial redundancy. The underlying principles of list will be mentioned in future articles.

So much for the introduction of "Redis list List slow Operation". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report