Redis Tutorial (iii): list data type

Source: Internet
Author: User
Tags redis tutorial
First, overview:

In Redis, the list type is a linked list of strings sorted by insertion order. Like a regular list in a data structure, we can add new elements to their head (left) and tail (right). When inserting, if the key does not exist, Redis creates a new linked list for that key. Conversely, if all the elements in the list are removed, the key will be removed from the database as well. The maximum number of elements that can be contained in a list is 4294967295.
From an efficiency perspective of element insertion and deletion, if we are inserting or deleting elements at both ends of the list, this will be a very efficient operation, even if the millions record is already stored in the linked list, and the operation can be done in constant time. However, it is important to note that if an element insert or delete operation is in the middle of the list, it will be very inefficient. This is not difficult to understand for developers with a good data structure base.

Ii. List of related commands:


Command prototypes Complexity of Time Command description return value
Lpushkey value [value ...] O (1) All values given in the header insert parameter of the list value associated with the specified key. If the key does not exist, the command creates an empty list associated with the key before inserting it, and then inserts the data from the list's head. If the value of the key is not a linked list type, the command returns a related error message. The number of elements in the linked list after the insert.
Lpushx Key value O (1) Only if the key specified in the parameter exists, the command will be given the value in the header of its associated list value, otherwise no action will occur. The number of elements in the linked list after the insert.
Lrange Key Start stop O (S+n) s in the time complexity are the offsets represented by the start parameter, and n represents the number of elements. The arguments for the command start and end are 0-based. That is, 0 represents the first element of the list header (leftmost). Where the value of start can also be negative, 1 will represent the last element in the list, that is, the trailing element, 2 means the penultimate and so on. When the command acquires an element, the elements at 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 list will be returned. If the value of end is greater than the number of elements, the command obtains all the elements remaining in the list starting with start (including start). Returns a list of elements within a specified range.
Lpopkey O (1) Returns and pops the first element, the head element, in the linked list of the specified key association. If the key does not exist, returns nil. The element of the linked list head.
llenkey o (1) number of elements in the list
lremkey count value o (N) Returns the number of deleted elements.
lsetkey index value o (N)
lindex key index o (N) Returns the requested element if index is out of range, Then nil is returned.
ltrimkey start Stop o (N) n indicates the number of elements being deleted. This command preserves only the elements within the specified range, ensuring that the number of elements in the link is relatively constant. Both the start and stop parameters are 0-based,0 representing the head element. As with other commands, start and stop can also be negative, and 1 indicates a trailing element. If start is greater than the end of the list, or start is greater than stop, the command returns an empty list, and the key is deleted as well. If stop is greater than the number of elements, all remaining elements from start are preserved.
linsert key before| After pivot value o (N) The number of elements in the linked list after successful insertion, if no pivot is found, return-1, If key does not exist, returns 0.
Rpush key value [value ...] O (1) All values given in the trailing insert parameter of the list value associated with the specified key. If the key does not exist, the command creates an empty list associated with the key before inserting, and then inserts the data from the end of the list. If the value of the key is not a linked list type, the command returns a related error message. Number of elements in the linked list after insertion
Rpushx Key value O (1) Only if the key specified in the parameter exists, the command will be given the value in the end of its associated list value, otherwise no action will occur. The number of elements in the linked list after the insert.
Rpopkey O (1) Returns and pops the last element, the trailing element, in the linked list associated with the specified key. If the key does not exist, returns nil. The element at the tail of the linked list.
Rpoplpushsource destination O (1) atomically pops an element from the tail of the linked list associated with the source key, and then inserts the popup element into the head of the linked list associated with the destination key. If the source key does not exist, the command returns nil and does not do any other operations at the same time. If source and destination are the same key, then the equivalent of atomicity moves the trailing element in its associated list to the head of the linked list. Returns the popup and inserted elements.

Examples of commands:

1. Lpush/lpushx/lrange:

/> Redis-cli    #在Shell提示符下启动redis客户端工具.    redis 127.0.0.1:6379> del MyKey    (integer) 1    #mykey键并不存在, the command creates the key and its associated list, and then inserts values from left to right in the argument.    Redis 127.0.0.1:6379> Lpush MyKey a b c D    (integer) 4    #取从位置0开始到位置2结束的3个元素.    redis 127.0.0.1:6379> lrange mykey 0 2    1) "D"    2) "C"    3) "B"    #取链表中的全部元素, where 0 represents the first element, and 1 represents the last element.    redis 127.0.0.1:6379> lrange mykey 0-1    1) "D"    2) "C"    3) "B"    4) "A"    #mykey2键此时并不存在, Therefore, the command will not take any action and its return value is 0.    Redis 127.0.0.1:6379> lpushx mykey2 e    (integer) 0    #可以看到mykey2没有关联任何List Value.    redis 127.0.0.1:6379> lrange mykey2 0-1    (empty list or set)    #mykey键此时已经存在, the command is inserted successfully and returns the number of current elements in the linked list.    Redis 127.0.0.1:6379> lpushx mykey e    (integer) 5    #获取该键的List The header element of value.    redis 127.0.0.1:6379> lrange mykey 0 0    1) "E"

2. Lpop/llen:

   Redis 127.0.0.1:6379> Lpush MyKey a b c d    (integer) 4    redis 127.0.0.1:6379> lpop mykey    "D"    Redis 127 .0.0.1:6379> lpop MyKey    "C"    #在执行lpop命令两次后, two elements of the list header have been popped, and the number of elements in the list is 2    redis 127.0.0.1:6379> Llen MyKey    (integer) 2

3. Lrem/lset/lindex/ltrim:

#为后面的示例准备测试数据.    Redis 127.0.0.1:6379> Lpush MyKey a b c d a C    (integer) 6    #从头部 (left) to the tail (right) variable list, delete the 2 value equals a of the element, the return value is the number actually deleted.    Redis 127.0.0.1:6379> lrem MyKey 2 a    (integer) 2    #看出删除后链表中的全部元素.    redis 127.0.0.1:6379> lrange mykey 0-1    1) "C"    2) "D"    3) "C"    4) "B"    #获取索引值为1 (second element of the head) The element value.    the element value for Redis 127.0.0.1:6379> lindex MyKey 1    "D"    #将索引值为1 (second element of the head) is set to the new value E.    Redis 127.0.0.1:6379> LSet MyKey 1 E    OK    #查看是否设置成功.    Redis 127.0.0.1:6379> lindex mykey 1    "E"    #索引值6超过了链表中元素的数量, this command returns nil.    Redis 127.0.0.1:6379> lindex MyKey 6    (nil)    #设置的索引值6超过了链表中元素的数量, the setting fails, and the command returns an error message.    Redis 127.0.0.1:6379> lset MyKey 6 hh    (Error) ERR index out of range    #仅保留索引值0到2之间的3个元素, note that both No. 0 and 2nd elements are preserved.    redis 127.0.0.1:6379> ltrim mykey 0 2    OK    #查看trim后的结果.    redis 127.0.0.1:6379> lrange mykey 0-1    1) "C"    2) "E"    3) "C"

4. Linsert:

#删除该键便于后面的测试.    redis 127.0.0.1:6379> del MyKey    (integer) 1    #为后面的示例准备测试数据.    Redis 127.0.0.1:6379> Lpush MyKey a b c d e    (integer) 5    #在a的前面插入新元素a1.    Redis 127.0.0.1:6379> Linsert MyKey before a A1    (integer) 6    #查看是否插入成功, which is inserted from the result. Note that the index value of Lindex is 0-based.    redis 127.0.0.1:6379> lindex mykey 0    "E"    #在e的后面插入新元素e2, from the returned results have been inserted successfully.    Redis 127.0.0.1:6379> Linsert MyKey after e E2    (integer) 7    #再次查看是否插入成功.    Redis 127.0.0.1:6379> lindex mykey 1    "E2"    #在不存在的元素之前或之后插入新元素, the command operation failed and returned-1.    Redis 127.0.0.1:6379> Linsert MyKey after k a    (integer)-1    #为不存在的Key插入新元素, the command operation failed and returned 0.    Redis 127.0.0.1:6379> Linsert mykey1 after a A2    (integer) 0


5. Rpush/rpushx/rpop/rpoplpush:
 #删除该键 for the subsequent test.    Redis 127.0.0.1:6379> del mykey (integer) 1 #从链表的尾部插入参数中给出的values, the insertion order is inserted from left to right.    Redis 127.0.0.1:6379> Rpush MyKey a b C d (integer) 4 #通过lrange的可以获悉rpush在插入多值时的插入顺序. Redis 127.0.0.1:6379> lrange mykey 0-1 1) "a" 2) "B" 3) "C" 4) "D" #该键已经存在并且包含4个元素, the RPUSHX command will execute successfully and insert element E    into the tail of the linked list.    Redis 127.0.0.1:6379> rpushx mykey E (integer) 5 #通过lindex命令可以看出之前的rpushx命令确实执行成功 because an element with an index value of 4 is already a new element.    Redis 127.0.0.1:6379> lindex MyKey 4 "E" #由于mykey2键并不存在, so the command does not insert data and its return value is 0.    Redis 127.0.0.1:6379> rpushx mykey2 E (integer) 0 #在执行rpoplpush命令前, take a look at the elements of the linked list in MyKey, and note their positional relationships. Redis 127.0.0.1:6379> lrange mykey 0-1 1) "a" 2) "B" 3) "C" 4) "D" 5) "E" #将mykey的尾部元素e弹出, and then inserted into the Myke    Y2 's head (atomic completion of these two steps).    Redis 127.0.0.1:6379> rpoplpush mykey mykey2 "E" #通过lrange命令查看mykey在弹出尾部元素后的结果. Redis 127.0.0.1:6379> lrange mykey 0-1 1) "a" 2) "B" 3) "C" 4) "D" #通过lrange命令查看mykey2在插入The result after the element.    Redis 127.0.0.1:6379> lrange mykey2 0-1 1) "E" #将source和destination设为同一键, moves the trailing element in the MyKey to its head.    Redis 127.0.0.1:6379> rpoplpush mykey mykey "D" #查看移动结果. Redis 127.0.0.1:6379> lrange mykey 0-1 1) "D" 2) "a" 3) "B" 4) "C"


Four, link list structure of small tricks:


The Value,redis of the linked list structure gives some practical tips in its official documentation, such as the Rpoplpush command, which gives a specific explanation.
Redis linked lists are often used for Message Queuing services to complete the exchange of messages between multiple programs. Suppose an application is performing a lpush operation to add a new element to a linked list, and we usually call such a program "producer (Producer)", while another application is performing rpop operations to remove elements from the list, we call this program "consumer (Consumer) "。 If, at this point, the consumer program crashes immediately after the message element is removed and is not processed properly, we can assume that the message has been lost, which could result in loss of business data or inconsistent business status. However, by using the Rpoplpush command, the consumer program takes the message out of the main message queue and then inserts it into the backup queue until the consumer program finishes the normal processing logic and removes the message from the backup queue. At the same time, we can provide a daemon, when the message in the backup queue is found to expire, you can re-put it back to the main message queue, so that other consumer programs continue processing.

The above is the Redis tutorial (iii): List data type content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.