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

How to use the linked list command in redis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article will explain in detail about the use of linked list commands in redis. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

I. Overview:

List is a linked list structure, and its main functions are push, pop, getting all values in a range, and so on. The key in the operation is understood as the name of the linked list. The definition and implementation of list is in the source file adlist.h/adlist.c.

In Redis, the List type is a linked list of strings sorted in the order of insertion. Like a normal linked list in a data structure, we can add new elements to its header (left) and tail (right). On insertion, if the key does not exist, Redis creates a new linked list for the key.

In contrast, if all elements in the linked list are removed, the key will also be deleted from the database. The maximum number of elements that can be contained in a List is 4294967295.

From the perspective of the efficiency of element insertion and deletion, it will be very efficient if we insert or delete elements at both ends of the linked list, even if millions of records have been stored in the linked list.

It is important to note, however, that if the element insert or delete operation acts in the middle of the linked list, it will be very inefficient. I believe that for developers with a good data structure foundation, this is not difficult to understand.

2. List of commands:

The command prototype time complexity command describes all the Values given by the return value LPUSH key value [value...] O (1) in the header insertion parameter of the List Value associated with the specified Key. If the Key does not exist, the command creates an empty linked list associated with the Key before insertion, and then inserts the data from the header of the linked list. If the Value of the key is not a linked list type, the command returns the relevant error message. The number of elements in the linked list after insertion. LPUSHX key valueO (1) only if the Key specified in the parameter exists, the command inserts the Value given in the parameter in the header of the List Value it is associated with, otherwise nothing will happen. The number of elements in the linked list after insertion. The S in the LRANGE key start stopO time complexity is the offset represented by the start parameter, and N represents the number of elements. The parameters start and end of this command are both 0-based. That is, 0 represents the first element of the linked list header (leftmost). Where the value of start can also be negative,-1 will represent the last element in the linked list, the tail element,-2 will represent the penultimate element, and so on. When this command fetches elements, the elements in the start and end positions are also taken out. If the value of start is greater than the number of elements in the linked list, the empty linked list will be returned. If the value of end is greater than the number of elements, the command gets all the remaining elements in the linked list starting with start (including start). Returns a list of elements within the specified range. LPOP keyO (1) returns and pops up the first element in the linked list associated with the specified Key, the header element. If the Key is not saved, return nil. The element at the head of the linked list. LLEN keyO (1) returns the number of elements in the linked list associated with the specified Key, or 0 if the Key does not exist. If the type of Value associated with the Key is not a linked list, the relevant error message is returned. The number of elements in the list. N in LREM key count valueO (N) time complexity represents the number of elements in the linked list. Delete elements whose previous count values are equal to value in the linked list associated with the specified Key. If count is greater than 0, iterate through and delete, if count is less than 0, traverse and delete from tail to head. If count equals 0, all elements in the linked list that are equal to value are deleted. If the specified Key does not exist, 0 is returned directly. Returns the number of elements deleted. N in LSET key index valueO (N) time complexity represents the number of elements in the linked list. However, when setting the element of the head or tail, the time complexity is O (1). Sets the value of the specified position in the linked list to the new value, where 0 represents the first element, the header element, and-1 represents the trailing element. If the index value Index exceeds the number of elements in the linked list, the command returns the relevant error message. N in LINDEX key indexO (N) time complexity represents the number of elements that need to be traversed when the element is found. For the header or tail element, the time complexity is O (1). This command returns the element at the specified position (index) in the linked list. Index is 0-based, representing the header element, and if index is-1, the trailing element. If the Key is not associated with a linked list, the command returns the relevant error message. Returns the requested element, or nil if index is out of range. LTRIM key start stopO (N) N represents the number of elements that have been deleted. This command retains only the elements within the specified range, ensuring that the number of elements in the link is relatively constant. The start and stop parameters are both 0 words based on 0 indicating the header element. Like other commands, start and stop can also be negative values, with-1 representing the trailing element. If the start is greater than the tail of the linked list, or if the start is greater than stop, the command returns an empty linked list, and the Key is deleted. If the stop is greater than the number of elements, all remaining elements from start are retained. LINSERT key BEFORE | AFTER pivot valueO (N) time complexity N represents the number of elements that need to be traversed before finding the element pivot. This means that if pivot is at the head or tail of the linked list, the time complexity of the command is O (1). The function of this command is to insert the element value in the parameter before or after the pivot element. If Key does not exist, the command does nothing. If the Value type associated with Key is not a linked list, the associated error message will be returned. The number of elements in the linked list after successful insertion, returns-1 if pivot is not found, and 0 if key does not exist. RPUSH key value [value...] O (1) inserts all the Values given in the tail parameter of the List Value associated with the specified Key. If the Key does not exist, the command creates an empty linked list associated with the Key before insertion, and then inserts the data from the tail of the linked list. If the Value of the key is not a linked list type, the command returns the relevant error message. The number of elements in the linked list after insertion. RPUSHX key valueO (1) only if the Key specified in the parameter exists, the command inserts the Value given in the parameter at the end of its associated List Value, otherwise no action will occur. The number of elements in the linked list after insertion. RPOP keyO (1) returns and pops up the last element in the linked list associated with the specified Key, the tail element. If the Key is not saved, return nil. The element at the end of the list. RPOPLPUSH source destinationO (1) atomically pops an element from the end of the linked list associated with the source key, while inserting the pop-up element into the head of the linked list associated with the destination key. If the source key does not exist, the command returns nil without doing anything else. If source and destination are the same key, it is equivalent to atomically moving the tail element of its associated linked list to the head of the linked list. Returns pop-up and inserted elements.

3. Examples of commands:

1 、 LPUSH/LPUSHX/LRANGE:

/ > redis-cli # starts the redis client tool at the Shell prompt. The redis 127.0.0.1 values 6379 > del mykey (integer) 1 # mykey key does not exist. The command creates the key and the List associated with it, then inserts the key in the parameter from left to right. Redis 127.0.0.1 redis 6379 > lpush mykey a b c d (integer) 4 # takes the three elements from position 0 to position 2. Redis 127.0.1 lrange mykey 021) "d" 2) "c" 3) "b" # fetches all the elements in the linked list, where 0 represents the first element and-1 represents the last element. Redis 127.0.0.1 lrange mykey 0-11) "d" 2) "c" 3) "b" 4) the a "# mykey2 key does not exist at this time, so the command will do nothing and its return value is 0. Redis 127.0.0.1 List Value 6379 > lpushx mykey2 e (integer) 0 # you can see that mykey2 is not associated with any List Value. The redis 127.0.0.1 empty list or set 6379 > lrange mykey2 0-1 (empty list or set) # mykey key already exists, so the command inserts successfully and returns the number of current elements in the linked list. Redis 127.0.0.1 List Value 6379 > lpushx mykey e (integer) 5 # gets the header element of the key's List Value. Redis 127.0.0.1 6379 > lrange mykey 0 01) "e"

2 、 LPOP/LLEN:

Redis 127.0.0.1 redis 6379 > lpush mykey a b c d (integer) 4 redis 127.0.0.1 integer 6379 > lpop mykey "d" redis 127.0.0.1 lpop mykey 6379 > lpop mykey "c" # after executing the lpop command twice, the two elements in the header of the linked list have been popped up, and the number of elements in the linked list is 2 redis 127.0.0.16379 > llen mykey (integer) 2

3 、 LINSERT:

# remove this key for later testing. Redis 127.0.0.1 redis 6379 > del mykey (integer) 1 # prepares the test data for the following example. Redis 127.0.0.1 redis 6379 > lpush mykey a b c d e (integer) 5 # inserts a new element A1 before a. Redis 127.0.0.1 integer 6379 > linsert mykey before an A1 (integer) 6 # to see if the insert is successful. The result shows that it has been inserted. Notice that the index value of lindex is 0-based. Redis 127.0.0.1 redis 6379 > lindex mykey 0 "e" # inserts a new element e2 after e, and the result returned shows that the insertion has been successful. Redis 127.0.0.1 integer 6379 > linsert mykey after e e2 (integer) 7 # check again to see if the insert is successful. Redis 127.0.0.1 redis 6379 > lindex mykey 1 "e2" # inserts a new element before or after an element that does not exist, the command operation fails and returns-1. Redis 127.0.0.1 integer 6379 > linsert mykey after k a (integer)-1 # inserts a new element for a Key that does not exist. The command operation failed with a return of 0. Redis 127.0.0.1 integer 6379 > linsert mykey1 after an a2 (integer) 0 5. RPUSH/RPUSHX/RPOP/RPOPLPUSH: # remove the key for later testing. Redis 127.0.0.1 values 6379 > del mykey (integer) 1 # the values given in the insertion parameters at the end of the linked list, in order of insertion from left to right. Redis 127.0.0.1 integer 6379 > rpush mykey a b c d (integer) 4 # you can learn the insertion order of rpush when inserting multiple values through lrange. Redis 127.0.0.1 lrange mykey 0-11) "a" 2) "b" 3) "c" 4) "d" # the key already exists and contains four elements, the rpushx command will be executed successfully and the element e will be inserted at the end of the linked list. Redis 127.0.0.1 integer 6379 > rpushx mykey e (integer) 5 # you can see from the lindex command that the previous rpushx command was indeed successful, because the element with an index value of 4 is already a new element. Redis 127.0.0.1 lindex mykey 6379 > 4 "e" # because the mykey2 key does not exist, the command does not insert data and its return value is 0. Redis 127.0.1 integer 6379 > rpushx mykey2 e (integer) 0 # before executing the rpoplpush command, take a look at the elements of the linked list in mykey and note their positional relationships. Redis 127.0.0.1 lrange mykey 0-11) "a" 2) "b" 3) "c" 4) "d" 5) "e" # eject the tail element e of mykey and insert it into the head of mykey2 at the same time (atomically complete these two steps). Redis 127.0.0.1 redis 6379 > rpoplpush mykey mykey2 "e" # use the lrange command to see the result of mykey after the tail element pops up. Redis 127.0.1 mykey2 6379 > lrange mykey 0-11) "a" 2) "b" 3) "c" 4) "d" # use the lrange command to view the result of mykey2 after inserting the element. Redis 127.0.0.1 lrange mykey2 6379 > source 0-11) "e" # sets source and destination to the same key, moving the tail element in the mykey to its header. Redis 127.0.0.1 6379 > rpoplpush mykey mykey "d" # to view the result of the move. Redis 127.0.0.1 lrange mykey 0-1 1) "d" 2) "a" 3) "b" 4) "c"

4 、 LREM/LSET/LINDEX/LTRIM:

# prepare test data for the following example. Redis 127.0.0.1 integer 6379 > lpush mykey a b c d a c (integer) 6 # variable list from head (left) to tail (right), delete two elements whose value is equal to a, and return the number of actual deletions. Redis 127.0.0.1 integer 6379 > lrem mykey 2 a (integer) 2 # see all the elements in the linked list after deletion. Redis 127.0.0.1 lrange mykey 0-1 1) "c" 2) "d" 3) "c" 4) "b" # gets the element value with an index of 1 (the second element of the header). Redis 127.0.0.1 redis 6379 > lindex mykey 1 "d" # sets the element value of index 1 (the second element of the header) to the new value e. Redis 127.0.0.1 OK 6379 > lset mykey 1 e check to see if the setting is successful. Redis 127.0.0.1 redis 6379 > lindex mykey 1 "e" # index value 6 exceeds the number of elements in the linked list, which returns nil. The index value set by redis 127.0.0.1 nil 6379 > lindex mykey 6 (nil) # exceeds the number of elements in the linked list. The setting fails, and the command returns an error message. Redis 127.0.1 hh 6379 > lset mykey 6 hh (error) ERR index out of range # retains only three elements with index values between 0 and 2. Note that both the zero and second elements are retained. Redis 127.0.0.1 OK 6379 > ltrim mykey 0 2 OK # View the result after trim. Redis 127.0.0.1 6379 > lrange mykey 0-11) "c" 2) "e" 3) "c"

4. Tips on linked list structure:

Value,Redis for linked list structure gives some practical tips in its official documentation, such as the RPOPLPUSH command, which is explained in detail below.

Redis linked lists are often used for message queuing services to complete message exchange between multiple programs.

Suppose an application is performing a LPUSH operation to add new elements to the linked list. We usually call such a program "Producer", while another application is performing a RPOP operation to extract elements from the linked list. We call such a program "Consumer".

If at this time, the consumer program crashes immediately after fetching the message element, because the message has been retrieved and has not been processed normally, then we can assume that the message has been lost, which may lead to the loss of business data. or business state inconsistency and other phenomena occur.

However, by using the RPOPLPUSH command, the consumer program takes the message from the main message queue and then inserts it into the backup queue until the consumer program completes the normal processing logic before removing the message from the backup queue.

At the same time, we can also provide a daemon that, when a message in the backup queue is found to have expired, can be put back into the main message queue so that other consumer programs can continue to process.

On the use of linked list commands in redis to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Database

Wechat

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

12
Report