four, List type
The list type is also a type that we use very long. For example, we send a blog, to use the blog list. If there is no list we can only traverse the key to get all articles or a part of the article, this syntax is the keys, but this command needs to traverse all the keys in the database, in terms of performance considerations, in the production environment is not recommended.
A list type can store an ordered list of strings, often by adding, deleting, getting elements, or a fragment to both ends of the list. In Redis, it is actually implemented using a two-way list, so the time complexity of adding a delete element at both ends of the list is O (1), and the closer you get to both ends, the faster the element gets. But accessing elements through the index is slow, especially if the list is very long. But if you just get the first number of elements at the beginning or end of the queue, this has nothing to do with the queue length. So here the queue is very suitable for our comment function, as well as some new features such as the development of the record log is also very useful.
1. Add element command
Add elements to both ends of the list: Shell redis>lpush key value [value ...] redis>rpush key value [value ...]
1 2 |
Redis & gt; Lpush key value [value ...] Redis & gt; Rpush key value [value ...] |
It's also easy to see that Lpush's L is left, adding elements to the left-hand side of the list, and r naturally means right. Shell redis>lpush numbers 0 (integer) 1 redis>rpush numbers 1 (integer) 2 Redis>lpush numbers-1-2 ( Integer) 4
1 2 3 4 5 6 |
Redis & gt; Lpush numbers 0 (integer) 1 Redis & gt; Rpush numbers 1 (integer) 2 Redis & gt; Lpush numbers-1-2 (integer) 4 |
The return value is the length of the list. The value of the push can be multiple, but there is one thing to explain. Like Physiognomy. The left push two values-1,-2, in fact, now the list value is so sorted,-2,-1, 0, 1, so it is even push multiple, is actually a list of inserts, but these operations will be atomic.
2, pop-up element command Shell redis>lpop key Redis>rpop key
1 2 |
Redis & gt; Lpop key Redis & gt; Rpop Key |
Here is a good understanding, respectively, from the left pop-up, from the right pop-up, the return value is a pop-up value. (pop-up means to remove the value and return the value)
Combined with the above four commands, you can implement stacks and queues.
Stacks: Lpush and Lpop or using Rpush and Rpop.
Queues: Lpush and Rpop or using Rpush and Lpop.
3, get the number of elements in the list Shell Redis>llen Key
When the key does not exist is to return 0, this if all the way to learn can be guessed. Here Redis the time complexity of executing this command is O (1), unlike O (N) in a relational database.
4. Get list Fragment Shell redis>lrange key Start stop
1 |
Redis & gt; Lrange Key Start stop |
The Lrange command is more commonly used to return a list of all the elements from start to stop, starting and stop from 0. Shell redis>lrange numbers 0 2 1) "-2" 2) "-1" 3 "" 0 "
1 2 3 4 |
Redis & gt; Lrange numbers 0 2 1) "-2" 2) "-1" 3 "" 0 " |
Lrange also supports negative indexes, here are negative values you can directly interpret the minus sign as the number from the right.
For example, now numbers values are-2,-1, 0, 1, and if we execute Lrange numbers-2-1, we get the last two values. and Lrange numbers 0-1 is one of the most common commands we use to get a full list.
Here are two points to note:
(1) An empty list (empty list or set) is returned if the start index is higher than the Stop index position (where it says the location, not the size of the index value).
(2) If the stop is greater than the actual index range, the element that is the last variable in the list is returned.
5, delete the specified value in the list Shell Redis>lrem Key Count value
1 |
Redis & gt; Lrem Key Count value |
The Lrem command deletes the first count of value from the list, returning the number of elements that were actually deleted. The actual count size is different and executes differently.
(1) Count > 0: Deletes the element with the previous count of value from the left of the list
(2) Count < 0: Remove from right start
(3) Count = 0: Delete all
The return value is the number of deleted values.
6. Get/Set the element value of the specified index Shell redis>lindex key index redis>lset key index value
1 2 |
Redis & gt; Lindex key index Redis & gt; LSet Key index value |
This should be very well understood, especially if the index is negative, the index is computed from the right, and the negative value of the lrange is a meaning.
7. Keep only the fragments specified in the list Shell Redis>ltrim Key Start end
1 |
Redis & gt; LTrim Key Start end |
There is no special explanation here.
8. Inserting elements into the list
Now the values in our list are-2,-1, 0, 1 Shell