Redis base data type (list)

Source: Internet
Author: User
Tags redis
Overview:

The Redis list is a simple list of strings sorted by the insertion order. You can add the head (left) or tail (right) of an element's Guide list. In Redis, the list type is a list of strings sorted in the order of insertion. As with the regular linked list in the data structure, we can add new elements to the head (left) and tail (right). When inserted, if the key does not exist, Redis creates a new linked list for the key. Conversely, if all elements in a 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 efficiency perspective of element insertion and deletion, if we insert or delete elements at both ends of the list, this can be a very efficient operation, even if the millions record is already stored in the list, and the operation is completed in constant time. It is, however, important to note that if the element insertion or deletion is acting on the middle of the list, it will be very inefficient.

Original:

Themax length of a list is 232-1elements (4294967295, than 4 billion of elements/list).

A list can contain up to 232-1 elements (4294967295, each list has more than 4 billion elements). List of related commands:

Command Prototypes

Complexity of Time

Command Description

return value

lpush key 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 head of the list. If the value of the key is not a linked list type, the command returns the associated error message.

The number of elements in the linked list after insertion.

lpushx Key value

O (1)

Only if the key specified in the argument exists, the command will be given value in the header of its associated list value, otherwise no action will occur.

The number of elements in the linked list after insertion.

Lrange key Start stop

O (S+n)

The offset of s in the time complexity represented by the start parameter, and n represents the number of elements. The parameter start and end of the command are 0-based. 0 represents the first element of a linked list header (leftmost). Where the value of start can also be negative,-1 will represent the last element in the list, the tail element,-2 for the penultimate and so on. When the command gets 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 list, the empty list is returned. If the value of end is greater than the number of elements, the command gets all the remaining elements in the list starting with start, including start.

Returns a list of elements within the specified range.

LpopKey

O (1)

Returns and pops the first element in the linked list associated with the specified key, that is, the header element. If the key does not exist, return to nil.

The element of the head of the linked list.

LlenKey

O (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 associated error message is returned.

The number of elements in the linked list.

LremKey Count value

O (N)

In time complexity, n represents the number of elements in a linked list. Deletes the element with the previous count value equal to value in the linked list associated with the specified key. If count is greater than 0, traverse and delete from head to tail, and if count is less than 0, traverse and delete from the tail head. If count is equal to 0, all elements equal to value in the linked list are deleted. If the specified key does not exist, it returns 0 directly.

Returns the number of elements that were deleted.

LSETKey index value

O (N)

In time complexity, n represents the number of elements in a linked list. But when the head or tail element is set, its time complexity is O (1). The value of the specified position in the list is the new value, where 0 represents the first element, the head element, and-1 represents the tail element. If the index value index exceeds the number of elements in the list, the command returns the associated error message.

lindex Key index

O (N)

The time complexity of n indicates the number of elements that need to be traversed when the element is found. For the head or tail element, the time complexity is O (1). The command returns the element of the specified position in the list (index), which is 0-based, which represents the head element, and if index is-1, the tail element. If the key is not associated with a linked list, the command returns the associated error message.

Returns the requested element and returns nil if Index is out of range.

LTRIMkey Start stop

O (N)

n represents the number of elements that are deleted. This command retains only the elements within the specified range, ensuring that the number of elements in the link is relatively constant. Both start and stop parameters are 0-based,0 representing the head element. As with other commands, start and stop can also be negative,-1 for the tail element. If start is greater than the end of the list, or start is greater than stop, the command is a good error, but returns an empty list, while the key is also deleted. If the stop is greater than the number of elements, all elements remaining from start are retained.

Linsert Key before| After pivot value

O (N)

The time complexity n indicates the number of elements that need to be traversed before the element pivot is found. This means that the time complexity of the command is O (1) If the pivot is in the head or tail of the linked list. The function of this command is to insert the element value in the argument before or after the pivot element. If key does not exist, the command will not perform any action. If the value type associated with the key is not a linked list, the associated error message is returned.

After successfully inserting the number of elements in the linked list, if no pivot is found, return-1, and return 0 if key does not exist.

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 it, 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 the associated error message.

The 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 value in the end of its associated list value, otherwise no action will occur.

The number of elements in the linked list after insertion.

RpopKey

O (1)

Returns and pops the last element in the linked list associated with the specified key, that is, the trailing element. If the key does not exist, return to nil.

The element at the tail of the list.

rpoplpush Source destination

O (1)

atomically pops an element from the tail of the linked list associated with the source key, and then inserts 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 and no other action is done. If source and destination are the same key, the equivalent of atomicity moves the tail element in its associated list to the head of the list.

Returns the elements that are ejected and inserted.

Command Example:



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 the list associated with it, and then inserts values from left to right in the parameter.
Redis 127.0.0.1:6379> Lpush MyKey a b c D
(integer) 4
#取从位置0开始到位置2结束的3个元素.
Redis 127.0.0.1:6379> Lrange mykey0 2
1) "D"
2) "C"
3) "B"
#取链表中的全部元素, where 0 represents the first element,-1 represents the last element.
Redis 127.0.0.1:6379> Lrange mykey0-1
1) "D"
2) "C"
3) "B"
4) "A"
#mykey2键此时并不存在, the command will not take any action and the return value is 0.
Redis 127.0.0.1:6379> lpushx mykey2e
(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 the current element in the list.
Redis 127.0.0.1:6379> lpushx Mykeye
(integer) 5
#获取该键的List the head element of value.
Redis 127.0.0.1:6379> lrange mykey0 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 in the head of the list have been ejected, 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 da c
(integer) 6
#从头部 (left) to the tail (right) variable list, delete 2 values equal to a, the return value is the actual number of deletes.
Redis 127.0.0.1:6379> Lrem MyKey 2a
(integer) 2
#看出删除后链表中的全部元素.
Redis 127.0.0.1:6379> Lrange mykey0-1
1) "C"
2) "D"
3) "C"
4) "B"
The element value of the #获取索引值为1 (the second element of the head).
Redis 127.0.0.1:6379> lindex Mykey1
"D"
The element value of the #将索引值为1 (the second element of the head) is set to the new value E.
Redis 127.0.0.1:6379> LSet MyKey 1e
Ok
#查看是否设置成功.
Redis 127.0.0.1:6379> lindex Mykey1
E
#索引值6超过了链表中元素的数量, this command returns nil.
Redis 127.0.0.1:6379> lindex Mykey6
(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 the No. 0 and 2nd elements are preserved.
Redis 127.0.0.1:6379> LTrim MyKey 02
Ok
#查看trim后的结果.
Redis 127.0.0.1:6379> Lrange mykey0-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 de
(integer) 5
#在a的前面插入新元素a1.
Redis 127.0.0.1:6379> Linsert Mykeybefore a A1
(integer) 6
#查看是否插入成功, from the result look has been inserted. Note that the index value of Lindex is 0-based.
Redis 127.0.0.1:6379> lindex MyKey 0
E
#在e的后面插入新元素e2, from the return result look has been inserted successfully.
Redis 127.0.0.1:6379> Linsert mykeyafter e E2
(integer) 7
#再次查看是否插入成功.
Redis 127.0.0.1:6379> lindex Mykey1
"E2"
#在不存在的元素之前或之后插入新元素, the command operation failed and returned-1.
Redis 127.0.0.1:6379> Linsert MyKey Afterk A
(integer)-1
#为不存在的Key插入新元素, the command operation failed, returning 0.
Redis 127.0.0.1:6379> Linsertmykey1 after a A2
(integer) 0

5. Rpush/rpushx/rpop/rpoplpush:
#删除该键 to facilitate 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 mykey0-1
1) "A"
2) "B"
3) "C"
4) "D"
#该键已经存在并且包含4个元素, the RPUSHX command succeeds and inserts element e into the tail of the 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键并不存在, 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 list in MyKey and pay attention to their position.
Redis 127.0.0.1:6379> lrange MyKey 0-1
1) "A"
2) "B"
3) "C"
4) "D"
5) "E"
#将

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.