Introduction to list types
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 linked 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:
redis> Lpush key value [value ...]
redis> Rpush key value [value ...]
It's also easy to see that lpush
the L is left, adding elements to the left-hand side of the list. ( lpush
, rpush
similar to the operation queue, to the left push
, to the right push
)
C:\Program files\redis>redis-cli.exe
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> Lpush mylist 0
(integer) 1
127.0.0.1:6379> Rpush mylist 1
(integer) 2
127.0.0.1:6379> Lpush Mylist-1-2
(integer) 4
127.0.0.1:6379> lrange mylist 0-1
1) "-2"
2) "-1"
3 ""
0 "4" 1 "
The return value is the length of the list. The value of the push can be multiple, but there is one thing to explain. It's like this. Left push
two values-1,-2, in fact, now the list value is sorted, 2,-1, 0, 1, so it is push
more than one, in fact, is also an insert list, but these operations will be atomic.
2. Popup element command
Redis> lpop key
redis> 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)
127.0.0.1:6379> lrange mylist 0-1
1) "-2"
2) "-1"
3 "" 0 "
4)" 1 "
127.0.0.1:6379> Lpop MyList
"-2"
127.0.0.1:6379> rpop mylist
"1"
127.0.0.1:6379> lrange mylist
-0-1 1) "-1"
2) "0"
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
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.
127.0.0.1:6379> llen mylist
(integer) 2
4. Get list Fragment
Redis> lrange key Start stop
lrange
Commands are more commonly used, returning a list of all the elements from start to stop, beginning and stop starting at 0.
127.0.0.1:6379> lrange mylist 0-1
1) "-1"
2 "0"
lrange
Negative indexes are also supported, which is negative. You can read the minus plus value directly from the right number.
For example, now the numbers values are-2,-1, 0, 1, and if we do lrange numbers -2 -1
, we get the last two values. There are also the lrange numbers 0 -1
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
Redis> Lrem Key Count value
lrem
The 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
: From the left of the list to delete the previous count value of the element
(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
redis> lindex key index
redis> 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.
127.0.0.1:6379> lrange mylist 0-1
1 "0"
2) "-1"
3 "-2" 4 "
-3"
127.0.0.1:6379> lindex MyList 0
"0"
127.0.0.1:6379> lindex mylist 3
"-3"
127.0.0.1:6379> lindex mylist-1
"-3"
7. Keep only the fragments specified in the list
Redis> LTrim key Start end
There is no special explanation here.
127.0.0.1:6379> lrange mylist 0-1
1 "0"
2) "-1"
3 "-2" 4 "
-3"
127.0.0.1:6379> lindex MyList 0
"0"
127.0.0.1:6379> lindex mylist 3
"-3"
127.0.0.1:6379> lindex
-mylist-1 "-3 "
127.0.0.1:6379> ltrim mylist 0 2
OK
127.0.0.1:6379> lrange mylist 0-1
1)" 0 "
2" "-1" C32/>3) "-2"
8. Inserting elements into the list
Now the value in our list is 0,-1,-2
redis> linsert key Before|after Pivot value
127.0.0.1:6379> Linsert mylist before 0 1
(integer) 4//Before 0 value insert 1 127.0.0.1:6379> lrange mylist 0-1
1) " 1 "
2" "0"
3) "-1"
4 "-2"
127.0.0.1:6379> Linsert mylist after 1 1
(integer) 5//Insert after value 1 1
127.0.0.1:6379> lrange mylist 0-1
1 "1"
2) "1"
3) "0" 4) "-1" 5 ""
-2
" 127.0.0.1:6379> Linsert mylist after-1 -1.5
(integer) 6//After the value-1 is inserted -1.5 127.0.0.1:6379> lrange mylist
0-1
1) "1"
2) "1"
3) "0" 4
) "-1"
5) " -1.5"
6 ""-2 "
The value in the list now is 1,1,0,-1,-1.5,-2. The Linsert command first looks pivot
up the element in the list and then adds the element based on whether it is before or after.
9. Dump elements from one list to another list
redis> Rpoplpush Source Destination
This is a very interesting command to perform before execution rpop
lpush
. This command will first source
eject an element from the right and insert it to the left of the destination
list and return the value of the element. The whole process is also atomic.
Summarize
Here we list the type of commands are all introduced, is not very simple ~ hope this article for everyone's study or work can bring some help, if there are questions you can message exchange.